Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,73 +1,51 @@
|
|
| 1 |
-
# import gradio as gr
|
| 2 |
-
# from transformers import pipeline
|
| 3 |
-
|
| 4 |
-
# # Load your model from Hugging Face
|
| 5 |
-
# classifier = pipeline("text-classification", model="coldnasser/depression-anxiety-mindscape")
|
| 6 |
-
|
| 7 |
-
# # Define the prediction function
|
| 8 |
-
# def predict(text):
|
| 9 |
-
# return classifier(text)
|
| 10 |
-
|
| 11 |
-
# # Create the Gradio interface
|
| 12 |
-
# gr.Interface(
|
| 13 |
-
# fn=predict,
|
| 14 |
-
# inputs="text",
|
| 15 |
-
# outputs="label",
|
| 16 |
-
# title="Mindscape AI Therapist"
|
| 17 |
-
# ).launch()
|
| 18 |
-
|
| 19 |
-
# import gradio as gr
|
| 20 |
-
# from transformers import pipeline
|
| 21 |
-
|
| 22 |
-
# classifier = pipeline("text-classification", model="coldnasser/depression-anxiety-mindscape")
|
| 23 |
-
|
| 24 |
-
# def predict(text):
|
| 25 |
-
# try:
|
| 26 |
-
# return classifier(text)
|
| 27 |
-
# except Exception as e:
|
| 28 |
-
# return f"Error: {str(e)}"
|
| 29 |
-
|
| 30 |
-
# gr.Interface(fn=predict, inputs="text", outputs="text", title="Mindscape").launch()
|
| 31 |
import gradio as gr
|
| 32 |
from transformers import pipeline
|
| 33 |
-
import
|
| 34 |
-
# def clean_text(text):
|
| 35 |
-
# # Remove mentions (@username)
|
| 36 |
-
# text = re.sub(r'@\w+', '', text)
|
| 37 |
-
|
| 38 |
-
# # Remove URLs
|
| 39 |
-
# text = re.sub(r'http\S+|www\S+', '', text)
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
# # Remove special characters, numbers, and extra spaces
|
| 43 |
-
# text = re.sub(r'[^a-zA-Z\s]', '', text) # Keep only letters and spaces
|
| 44 |
-
# text = re.sub(r'\s+', ' ', text).strip() # Remove extra spaces
|
| 45 |
|
| 46 |
-
#
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
# Define the mapping from label to human-readable status
|
| 50 |
label_mapping = {
|
| 51 |
"LABEL_0": "Normal",
|
| 52 |
"LABEL_1": "Depression",
|
| 53 |
"LABEL_2": "Anxiety"
|
| 54 |
}
|
| 55 |
|
|
|
|
| 56 |
classifier = pipeline("text-classification", model="coldnasser/depression-anxiety-mindscape")
|
| 57 |
|
| 58 |
-
def predict(
|
| 59 |
try:
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
| 71 |
|
| 72 |
-
|
|
|
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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="coldnasser/depression-anxiety-mindscape")
|
| 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.inputs.Textbox(lines=10, placeholder="Enter one or more texts (one per line)", label="Input Texts"),
|
| 49 |
+
outputs="json",
|
| 50 |
+
title="Mindscape AI Therapist (Multi-text Support)"
|
| 51 |
+
).launch()
|