Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,30 +1,51 @@
|
|
1 |
# app.py
|
2 |
|
3 |
import gradio as gr
|
|
|
4 |
import pandas as pd
|
|
|
|
|
5 |
|
6 |
-
#
|
7 |
-
|
|
|
|
|
|
|
8 |
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
10 |
def classify_question(text):
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
# Gradio
|
20 |
demo = gr.Interface(
|
21 |
fn=classify_question,
|
22 |
inputs=gr.Textbox(label="Enter a question"),
|
23 |
-
outputs=gr.Textbox(label="Predicted
|
24 |
-
examples=
|
25 |
-
title="EEMM
|
26 |
-
description="
|
27 |
-
allow_flagging="never"
|
28 |
)
|
29 |
|
30 |
-
|
|
|
|
|
|
1 |
# app.py
|
2 |
|
3 |
import gradio as gr
|
4 |
+
import torch
|
5 |
import pandas as pd
|
6 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
7 |
+
import torch.nn.functional as F
|
8 |
|
9 |
+
# STEP 1: Define label names and load model
|
10 |
+
label_names = [
|
11 |
+
"Cognition", "Affect", "Self", "Motivation", "Attention", "OB", "Context",
|
12 |
+
"Social", "Physical", "Psych"
|
13 |
+
]
|
14 |
|
15 |
+
model_path = "./results"
|
16 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
17 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
18 |
+
model.eval()
|
19 |
+
|
20 |
+
# STEP 2: Prediction function
|
21 |
def classify_question(text):
|
22 |
+
with torch.no_grad():
|
23 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
24 |
+
outputs = model(**inputs)
|
25 |
+
probs = torch.sigmoid(outputs.logits)[0]
|
26 |
+
predicted_labels = [label_names[i] for i, p in enumerate(probs) if p > 0.5]
|
27 |
+
if not predicted_labels:
|
28 |
+
return "No domain confidently predicted."
|
29 |
+
return ", ".join(predicted_labels)
|
30 |
+
|
31 |
+
# STEP 3: Load example data if available
|
32 |
+
try:
|
33 |
+
df = pd.read_csv("/tmp/eemm_cleaned.csv")
|
34 |
+
examples = df["clean_question"].dropna().head(5).tolist()
|
35 |
+
except:
|
36 |
+
examples = ["I must be perfect to be accepted.", "I enjoy helping others."]
|
37 |
|
38 |
+
# STEP 4: Gradio Interface
|
39 |
demo = gr.Interface(
|
40 |
fn=classify_question,
|
41 |
inputs=gr.Textbox(label="Enter a question"),
|
42 |
+
outputs=gr.Textbox(label="Predicted domains"),
|
43 |
+
examples=examples,
|
44 |
+
title="EEMM Multi-Label Classifier",
|
45 |
+
description="Classifies questions into multiple psychological domains.",
|
46 |
+
allow_flagging="never"
|
47 |
)
|
48 |
|
49 |
+
# STEP 5: Launch
|
50 |
+
if __name__ == "__main__":
|
51 |
+
demo.launch(share=True, server_name="0.0.0.0")
|