Spaces:
Sleeping
Sleeping
Alexandra Zapko-Willmes
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,41 +1,74 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import pipeline
|
3 |
import pandas as pd
|
4 |
import io
|
5 |
|
6 |
-
|
7 |
-
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
response_table = []
|
10 |
|
11 |
-
def
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
if not labels or not questions:
|
16 |
-
return "Please
|
17 |
|
|
|
18 |
global response_table
|
19 |
response_table = []
|
20 |
output_lines = []
|
21 |
|
22 |
for i, question in enumerate(questions, 1):
|
23 |
result = classifier(question, labels, multi_label=False)
|
24 |
-
|
25 |
-
|
26 |
output_lines.append(f"{i}. {question}")
|
27 |
-
for label in labels:
|
28 |
-
|
|
|
29 |
output_lines.append("")
|
30 |
-
|
31 |
-
row = {"Item #": i, "Item": question}
|
32 |
-
row.update({label: round(probs.get(label, 0.0), 3) for label in labels})
|
33 |
response_table.append(row)
|
34 |
|
35 |
return "\n".join(output_lines), None
|
36 |
|
37 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
global response_table
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
if not response_table:
|
40 |
return None
|
41 |
df = pd.DataFrame(response_table)
|
@@ -43,22 +76,37 @@ def download_csv():
|
|
43 |
df.to_csv(csv_buffer, index=False)
|
44 |
return csv_buffer.getvalue()
|
45 |
|
46 |
-
# Gradio UI
|
47 |
with gr.Blocks() as demo:
|
48 |
-
gr.Markdown("# 🧠
|
49 |
-
gr.Markdown("
|
|
|
|
|
50 |
|
51 |
with gr.Row():
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import pandas as pd
|
3 |
import io
|
4 |
|
5 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification, AutoModelForCausalLM
|
6 |
+
import torch
|
7 |
|
8 |
+
# Model lists
|
9 |
+
zero_shot_models = {
|
10 |
+
"EN: deberta-v3-large-zeroshot": "MoritzLaurer/deberta-v3-large-zeroshot-v2.0",
|
11 |
+
"MULTI: mDeBERTa-v3-base-xnli": "MoritzLaurer/mDeBERTa-v3-base-xnli-multilingual-nli-2mil7",
|
12 |
+
"MULTI: xlm-roberta-large-xnli": "joeddav/xlm-roberta-large-xnli"
|
13 |
+
}
|
14 |
+
|
15 |
+
text_gen_models = {
|
16 |
+
"Mixtral-8x7B-Instruct": "mistralai/Mixtral-8x7B-Instruct-v0.1",
|
17 |
+
"DeepSeek-Qwen3-8B": "deepseek-ai/DeepSeek-R1-0528-Qwen3-8B",
|
18 |
+
"DeepSeek-52B": "deepseek-ai/DeepSeek-R1-0528",
|
19 |
+
"LLaMA-3.1-8B-Instruct": "meta-llama/Llama-3.1-8B-Instruct"
|
20 |
+
}
|
21 |
+
|
22 |
+
# Shared storage for results
|
23 |
response_table = []
|
24 |
|
25 |
+
def run_classification(questions_text, labels_text, model_name):
|
26 |
+
labels = [label.strip() for label in labels_text.split(",") if label.strip()]
|
27 |
+
questions = [q.strip() for q in questions_text.split("\n") if q.strip()]
|
|
|
28 |
if not labels or not questions:
|
29 |
+
return "Please enter both items and response options.", None
|
30 |
|
31 |
+
classifier = pipeline("zero-shot-classification", model=model_name)
|
32 |
global response_table
|
33 |
response_table = []
|
34 |
output_lines = []
|
35 |
|
36 |
for i, question in enumerate(questions, 1):
|
37 |
result = classifier(question, labels, multi_label=False)
|
38 |
+
row = {"Item #": i, "Item": question}
|
|
|
39 |
output_lines.append(f"{i}. {question}")
|
40 |
+
for label, score in zip(result["labels"], result["scores"]):
|
41 |
+
row[label] = round(score, 3)
|
42 |
+
output_lines.append(f"→ {label}: {round(score, 3)}")
|
43 |
output_lines.append("")
|
|
|
|
|
|
|
44 |
response_table.append(row)
|
45 |
|
46 |
return "\n".join(output_lines), None
|
47 |
|
48 |
+
def run_generation(questions_text, model_name):
|
49 |
+
questions = [q.strip() for q in questions_text.split("\n") if q.strip()]
|
50 |
+
if not questions:
|
51 |
+
return "Please enter at least one item.", None
|
52 |
+
|
53 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
54 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32)
|
55 |
+
model.eval()
|
56 |
+
|
57 |
global response_table
|
58 |
+
response_table = []
|
59 |
+
output_lines = []
|
60 |
+
|
61 |
+
for i, question in enumerate(questions, 1):
|
62 |
+
prompt = f"Please respond to the following item as if you were a survey participant:\n\"{question}\""
|
63 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
64 |
+
outputs = model.generate(**inputs, max_new_tokens=60)
|
65 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True).split("\n")[-1]
|
66 |
+
output_lines.append(f"{i}. {question}\n→ {response.strip()}\n")
|
67 |
+
response_table.append({"Item #": i, "Item": question, "Response": response.strip()})
|
68 |
+
|
69 |
+
return "\n".join(output_lines), None
|
70 |
+
|
71 |
+
def download_csv():
|
72 |
if not response_table:
|
73 |
return None
|
74 |
df = pd.DataFrame(response_table)
|
|
|
76 |
df.to_csv(csv_buffer, index=False)
|
77 |
return csv_buffer.getvalue()
|
78 |
|
|
|
79 |
with gr.Blocks() as demo:
|
80 |
+
gr.Markdown("# 🧠 LLM Questionnaire Response Tool")
|
81 |
+
gr.Markdown("Choose between **zero-shot classification** and **text generation**. Enter your questionnaire items, select a model, and view responses. You can download all results as a CSV.")
|
82 |
+
|
83 |
+
task_type = gr.Radio(["Zero-shot classification", "Text generation"], label="Task Type", value="Zero-shot classification")
|
84 |
|
85 |
with gr.Row():
|
86 |
+
model_selector = gr.Dropdown(label="Choose Model")
|
87 |
+
labels_input = gr.Textbox(label="Response Options (comma-separated)", visible=True)
|
88 |
+
|
89 |
+
questions_input = gr.Textbox(label="Questionnaire Items (one per line)", lines=10)
|
90 |
+
output_box = gr.Textbox(label="Model Output", lines=20)
|
91 |
+
submit_btn = gr.Button("Run")
|
92 |
+
download_btn = gr.Button("📥 Download CSV")
|
93 |
+
file_output = gr.File(label="CSV", visible=False)
|
94 |
+
|
95 |
+
def update_model_and_labels(task):
|
96 |
+
if task == "Zero-shot classification":
|
97 |
+
return gr.Dropdown.update(choices=list(zero_shot_models.keys()), value=list(zero_shot_models.keys())[0]), gr.Textbox.update(visible=True)
|
98 |
+
else:
|
99 |
+
return gr.Dropdown.update(choices=list(text_gen_models.keys()), value=list(text_gen_models.keys())[0]), gr.Textbox.update(visible=False)
|
100 |
+
|
101 |
+
task_type.change(fn=update_model_and_labels, inputs=task_type, outputs=[model_selector, labels_input])
|
102 |
+
|
103 |
+
def route_task(questions, labels, model_ui_name, task):
|
104 |
+
if task == "Zero-shot classification":
|
105 |
+
return run_classification(questions, labels, zero_shot_models[model_ui_name])
|
106 |
+
else:
|
107 |
+
return run_generation(questions, text_gen_models[model_ui_name])
|
108 |
+
|
109 |
+
submit_btn.click(fn=route_task, inputs=[questions_input, labels_input, model_selector, task_type], outputs=[output_box, file_output])
|
110 |
+
download_btn.click(fn=download_csv, inputs=[], outputs=file_output)
|
111 |
|
112 |
demo.launch()
|