shainaraza commited on
Commit
9476a50
1 Parent(s): 5ce4100

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import AutoTokenizer, pipeline
3
+ import transformers
4
+ import torch
5
+ import pandas as pd
6
+
7
+ # Model setup
8
+ model = "newsmediabias/UnBIAS-LLama2-Debiaser-Chat-QLoRA"
9
+ tokenizer = AutoTokenizer.from_pretrained(model)
10
+ debias_pipeline = transformers.pipeline(
11
+ "text-generation",
12
+ model=model,
13
+ torch_dtype=torch.float16,
14
+ device_map="auto",
15
+ )
16
+
17
+ # Sample Instruction
18
+ instruction = ("Instruction: As a helpful, respectful and trustworthy debiasing assistant, your "
19
+ "task is to receive a text and return its unbiased version, without adding any unrelated content "
20
+ "or additional outputs.")
21
+
22
+ def get_debiased_sequence(prompt):
23
+ """Generate a debiased version of the provided text using the debiasing pipeline."""
24
+ input_text = f"<s> <<SYS>> {instruction} <</SYS>> [INST]{prompt} [/INST]"
25
+ sequences = debias_pipeline(
26
+ input_text,
27
+ do_sample=True,
28
+ top_k=10,
29
+ num_return_sequences=1,
30
+ eos_token_id=tokenizer.eos_token_id,
31
+ max_length=len(prompt)+100,
32
+ )
33
+ res = sequences[0]['generated_text']
34
+ result_part = res.split('[/INST]')[-1]
35
+ clean_result = ''.join(c for c in result_part if c.isprintable())
36
+ return clean_result.strip()
37
+
38
+ # Streamlit interface
39
+ st.title('Text Debiasing App')
40
+ input_text = st.text_area("Enter text to debias:", height=150)
41
+ if st.button("Debias Text"):
42
+ if input_text:
43
+ debiased_text = get_debiased_sequence(input_text)
44
+ st.write("Debiased Text:", debiased_text)
45
+ else:
46
+ st.write("Please enter some text to debias.")
47
+