Ozziejoe commited on
Commit
a8cbfd8
·
verified ·
1 Parent(s): 32ce3ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -17
app.py CHANGED
@@ -1,30 +1,51 @@
1
  # app.py
2
 
3
  import gradio as gr
 
4
  import pandas as pd
 
 
5
 
6
- # Load cleaned dataset
7
- df = pd.read_csv("/tmp/eemm_cleaned.csv")
 
 
 
8
 
9
- # Dummy classifier function — replace with your model later
 
 
 
 
 
10
  def classify_question(text):
11
- text = text.lower()
12
- if "should" in text:
13
- return "Cognition"
14
- elif "feel" in text:
15
- return "Affect"
16
- else:
17
- return "Unknown / Other"
 
 
 
 
 
 
 
 
18
 
19
- # Gradio app
20
  demo = gr.Interface(
21
  fn=classify_question,
22
  inputs=gr.Textbox(label="Enter a question"),
23
- outputs=gr.Textbox(label="Predicted category"),
24
- examples=df["clean_question"].head().tolist(),
25
- title="EEMM Exemplar Classifier",
26
- description="This demo classifies survey questions into psychological domains. Replace this with your trained model for production.",
27
- allow_flagging="never" # ✅ Disable saving flagged examples
28
  )
29
 
30
- demo.launch(share=True, server_name="0.0.0.0")
 
 
 
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")