File size: 1,600 Bytes
9476a50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import streamlit as st
from transformers import AutoTokenizer, pipeline
import transformers
import torch
import pandas as pd

# Model setup
model = "newsmediabias/UnBIAS-LLama2-Debiaser-Chat-QLoRA"
tokenizer = AutoTokenizer.from_pretrained(model)
debias_pipeline = transformers.pipeline(
    "text-generation",
    model=model,
    torch_dtype=torch.float16,
    device_map="auto",
)

# Sample Instruction
instruction = ("Instruction: As a helpful, respectful and trustworthy debiasing assistant, your "
               "task is to receive a text and return its unbiased version, without adding any unrelated content "
               "or additional outputs.")

def get_debiased_sequence(prompt):
    """Generate a debiased version of the provided text using the debiasing pipeline."""
    input_text = f"<s> <<SYS>> {instruction} <</SYS>> [INST]{prompt} [/INST]"
    sequences = debias_pipeline(
        input_text,
        do_sample=True,
        top_k=10,
        num_return_sequences=1,
        eos_token_id=tokenizer.eos_token_id,
        max_length=len(prompt)+100,
    )
    res = sequences[0]['generated_text']
    result_part = res.split('[/INST]')[-1]
    clean_result = ''.join(c for c in result_part if c.isprintable())
    return clean_result.strip()

# Streamlit interface
st.title('Text Debiasing App')
input_text = st.text_area("Enter text to debias:", height=150)
if st.button("Debias Text"):
    if input_text:
        debiased_text = get_debiased_sequence(input_text)
        st.write("Debiased Text:", debiased_text)
    else:
        st.write("Please enter some text to debias.")