Alexandra Zapko-Willmes commited on
Commit
779c3f4
·
verified ·
1 Parent(s): 279e4b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -73
app.py CHANGED
@@ -1,42 +1,32 @@
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)}")
@@ -45,68 +35,25 @@ def run_classification(questions_text, labels_text, model_name):
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)
75
- csv_buffer = io.StringIO()
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()
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
  import pandas as pd
4
  import io
5
 
6
+ # Define available classification models
7
+ models = {
 
 
 
8
  "EN: deberta-v3-large-zeroshot": "MoritzLaurer/deberta-v3-large-zeroshot-v2.0",
9
+ "MULTI: mDeBERTa-v3-xnli": "MoritzLaurer/mDeBERTa-v3-base-xnli-multilingual-nli-2mil7",
10
  "MULTI: xlm-roberta-large-xnli": "joeddav/xlm-roberta-large-xnli"
11
  }
12
 
 
 
 
 
 
 
 
 
13
  response_table = []
14
 
15
+ def classify(questions_text, labels_text, model_choice):
16
+ labels = [l.strip() for l in labels_text.split(",") if l.strip()]
17
  questions = [q.strip() for q in questions_text.split("\n") if q.strip()]
18
  if not labels or not questions:
19
+ return "Please enter both questions and labels.", None
20
 
21
+ classifier = pipeline("zero-shot-classification", model=models[model_choice])
22
  global response_table
23
  response_table = []
24
  output_lines = []
25
 
26
  for i, question in enumerate(questions, 1):
27
  result = classifier(question, labels, multi_label=False)
 
28
  output_lines.append(f"{i}. {question}")
29
+ row = {"Item #": i, "Item": question}
30
  for label, score in zip(result["labels"], result["scores"]):
31
  row[label] = round(score, 3)
32
  output_lines.append(f"→ {label}: {round(score, 3)}")
 
35
 
36
  return "\n".join(output_lines), None
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  def download_csv():
 
 
39
  df = pd.DataFrame(response_table)
40
+ buffer = io.StringIO()
41
+ df.to_csv(buffer, index=False)
42
+ return buffer.getvalue()
43
 
44
  with gr.Blocks() as demo:
45
+ gr.Markdown("## 🧠 Zero-Shot Classification Interface")
46
+ gr.Markdown("Enter questionnaire items and response options. The selected model will return probabilities for each label.")
 
 
 
 
 
 
47
 
48
+ model_dropdown = gr.Dropdown(choices=list(models.keys()), label="Choose Classification Model")
49
+ labels_input = gr.Textbox(label="Response Options (comma-separated)", placeholder="Strongly disagree, Disagree, Neutral, Agree, Strongly agree")
50
  questions_input = gr.Textbox(label="Questionnaire Items (one per line)", lines=10)
51
  output_box = gr.Textbox(label="Model Output", lines=20)
52
+ run_btn = gr.Button("Run Classification")
53
  download_btn = gr.Button("📥 Download CSV")
54
+ csv_file = gr.File(label="CSV", visible=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
+ run_btn.click(fn=classify, inputs=[questions_input, labels_input, model_dropdown], outputs=[output_box, csv_file])
57
+ download_btn.click(fn=download_csv, inputs=[], outputs=csv_file)
58
 
59
  demo.launch()