Spaces:
Sleeping
Sleeping
Alexandra Zapko-Willmes
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,58 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import pandas as pd
|
4 |
+
import io
|
5 |
+
|
6 |
+
# Load once
|
7 |
+
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
8 |
+
|
9 |
+
LIKERT_OPTIONS = ["Strongly disagree", "Disagree", "Neutral", "Agree", "Strongly agree"]
|
10 |
+
|
11 |
+
response_table = []
|
12 |
+
|
13 |
+
def classify_likert(questions_text):
|
14 |
+
questions = [q.strip() for q in questions_text.strip().split("\n") if q.strip()]
|
15 |
+
|
16 |
+
global response_table
|
17 |
+
response_table = []
|
18 |
+
output_lines = []
|
19 |
+
|
20 |
+
for i, question in enumerate(questions, 1):
|
21 |
+
result = classifier(question, LIKERT_OPTIONS, multi_label=False)
|
22 |
+
probs = dict(zip(result['labels'], result['scores']))
|
23 |
+
output_lines.append(f"{i}. {question}")
|
24 |
+
for label in LIKERT_OPTIONS:
|
25 |
+
prob = round(probs.get(label, 0.0), 3)
|
26 |
+
output_lines.append(f"→ {label}: {prob}")
|
27 |
+
output_lines.append("")
|
28 |
+
|
29 |
+
row = {"Item #": i, "Item": question}
|
30 |
+
row.update({label: round(probs.get(label, 0.0), 3) for label in LIKERT_OPTIONS})
|
31 |
+
response_table.append(row)
|
32 |
+
|
33 |
+
return "\n".join(output_lines)
|
34 |
+
|
35 |
+
def download_csv():
|
36 |
+
global response_table
|
37 |
+
if not response_table:
|
38 |
+
return None
|
39 |
+
df = pd.DataFrame(response_table)
|
40 |
+
csv_buffer = io.StringIO()
|
41 |
+
df.to_csv(csv_buffer, index=False)
|
42 |
+
return csv_buffer.getvalue()
|
43 |
+
|
44 |
+
# Gradio interface
|
45 |
+
with gr.Blocks() as demo:
|
46 |
+
gr.Markdown("# Likert-Style Zero-Shot Classifier")
|
47 |
+
gr.Markdown("Paste questionnaire items. Each will be classified into: Strongly disagree → Strongly agree, with probabilities.")
|
48 |
+
|
49 |
+
questions_input = gr.Textbox(label="Enter multiple items (one per line)", lines=10, placeholder="e.g.\nI feel in control of my life.\nI enjoy being around others...")
|
50 |
+
output_box = gr.Textbox(label="Classification Output", lines=20)
|
51 |
+
submit_btn = gr.Button("Classify Items")
|
52 |
+
csv_btn = gr.Button("📥 Download CSV")
|
53 |
+
file_output = gr.File(label="Download CSV", visible=False)
|
54 |
+
|
55 |
+
submit_btn.click(fn=classify_likert, inputs=questions_input, outputs=output_box)
|
56 |
+
csv_btn.click(fn=download_csv, inputs=[], outputs=file_output)
|
57 |
+
|
58 |
+
demo.launch()
|