Spaces:
Runtime error
Runtime error
Commit
·
9c42f3a
1
Parent(s):
1104575
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# import the main classes
|
2 |
+
import gradio as gr
|
3 |
+
import torch
|
4 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
5 |
+
|
6 |
+
|
7 |
+
# Load the model.
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-base")
|
9 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-base")
|
10 |
+
|
11 |
+
# input examples list
|
12 |
+
ex_list = [["Women are not as capable as men in leadership roles."],["Women are capable as men in leadership roles."]]
|
13 |
+
|
14 |
+
# generate_text method
|
15 |
+
def generate_text(prompt):
|
16 |
+
input_ids = tokenizer.encode(prompt, return_tensors="pt")
|
17 |
+
output = model.generate(
|
18 |
+
input_ids,
|
19 |
+
max_length=512,
|
20 |
+
temperature=0.7,
|
21 |
+
do_sample=True,
|
22 |
+
top_p=0.9,
|
23 |
+
top_k=0,
|
24 |
+
num_return_sequences=1,
|
25 |
+
)
|
26 |
+
return tokenizer.decode(output[0], skip_special_tokens=True)
|
27 |
+
|
28 |
+
def classify_gender_equality(input_sentence):
|
29 |
+
# Here goes your code to classify gender equality from input_sentence
|
30 |
+
# Return the result as a string
|
31 |
+
|
32 |
+
|
33 |
+
# sub_text = 'Gender equality is important for the progress of society.'
|
34 |
+
sub_text = input_sentence
|
35 |
+
prompt = f"Please classify the this sentence {sub_text} as promoting or not promoting gender equality:"
|
36 |
+
generated_text = generate_text(prompt)
|
37 |
+
# print("This sentence is",generated_text)
|
38 |
+
|
39 |
+
return "This sentence is " + generated_text + " for gender equality"
|
40 |
+
|
41 |
+
|
42 |
+
|
43 |
+
|
44 |
+
input_text = gr.inputs.Textbox(label="Input Sentence", default="Women deserve equal pay")
|
45 |
+
|
46 |
+
# Create the output text field
|
47 |
+
output_text = gr.outputs.Textbox(label="Gender Equality Classification")
|
48 |
+
|
49 |
+
# Create the Gradio interface
|
50 |
+
gr.Interface(fn=classify_gender_equality, inputs=[input_text], outputs=output_text, title='Gender Equality Classification', examples = ex_list).launch()
|