Commit ·
2141d1e
1
Parent(s): 4343135
new model
Browse files- app.py +57 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from collections import defaultdict
|
| 4 |
+
|
| 5 |
+
# Label mapping
|
| 6 |
+
label_mapping = {
|
| 7 |
+
"LABEL_0": "Normal",
|
| 8 |
+
"LABEL_1": "Depression",
|
| 9 |
+
"LABEL_2": "Anxiety"
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
# Load classifier
|
| 13 |
+
classifier = pipeline("text-classification", model="username/mindscape-v2")
|
| 14 |
+
|
| 15 |
+
def predict(texts):
|
| 16 |
+
try:
|
| 17 |
+
if isinstance(texts, str):
|
| 18 |
+
texts = [texts]
|
| 19 |
+
|
| 20 |
+
results = classifier(texts)
|
| 21 |
+
|
| 22 |
+
# Initialize score aggregator
|
| 23 |
+
score_sums = defaultdict(float)
|
| 24 |
+
count = len(texts)
|
| 25 |
+
|
| 26 |
+
for res in results:
|
| 27 |
+
label = res['label']
|
| 28 |
+
score = res['score']
|
| 29 |
+
score_sums[label] += score
|
| 30 |
+
|
| 31 |
+
# Calculate average scores
|
| 32 |
+
avg_scores = {label_mapping.get(label, label): score_sums[label] / count for label in score_sums}
|
| 33 |
+
|
| 34 |
+
# Get final predicted label (highest average)
|
| 35 |
+
final_label = max(avg_scores.items(), key=lambda x: x[1])[0]
|
| 36 |
+
|
| 37 |
+
return {
|
| 38 |
+
"Predicted Status": final_label,
|
| 39 |
+
"Average Scores": avg_scores
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return {"Error": str(e)}
|
| 44 |
+
|
| 45 |
+
# Gradio interface
|
| 46 |
+
gr.Interface(
|
| 47 |
+
fn=predict,
|
| 48 |
+
inputs=gr.Textbox(
|
| 49 |
+
lines=10,
|
| 50 |
+
placeholder="Enter one or more texts (one per line)",
|
| 51 |
+
label="Input Texts"
|
| 52 |
+
),
|
| 53 |
+
outputs=gr.JSON(
|
| 54 |
+
label="Predicted Status & Scores"
|
| 55 |
+
),
|
| 56 |
+
title="Mindscape AI Therapist (Multi-text Support)"
|
| 57 |
+
).launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
gradio
|
| 3 |
+
torch
|