File size: 1,450 Bytes
ce47d0c
 
f84c4ad
3c89f2b
f84c4ad
ee40b81
 
 
 
 
 
f84c4ad
7c6f639
 
ce47d0c
f84c4ad
b5825cb
f84c4ad
 
b15f93d
f84c4ad
f0d03d0
f84c4ad
 
 
f0d03d0
f84c4ad
 
 
 
ce47d0c
f84c4ad
 
ee40b81
f84c4ad
 
 
 
 
 
 
 
 
 
 
 
 
be8cfaf
 
 
 
 
 
 
 
 
 
f84c4ad
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import gradio as gr
from transformers import pipeline
from collections import defaultdict

# Label mapping
label_mapping = {
    "LABEL_0": "Normal",
    "LABEL_1": "Depression",
    "LABEL_2": "Anxiety"
}

# Load classifier
classifier = pipeline("text-classification", model="coldnasser/mindscape-v2")


def predict(texts):
    try:
        if isinstance(texts, str):
            texts = [texts]

        results = classifier(texts)

        # Initialize score aggregator
        score_sums = defaultdict(float)
        count = len(texts)

        for res in results:
            label = res['label']
            score = res['score']
            score_sums[label] += score

        # Calculate average scores
        avg_scores = {label_mapping.get(label, label): score_sums[label] / count for label in score_sums}

        # Get final predicted label (highest average)
        final_label = max(avg_scores.items(), key=lambda x: x[1])[0]

        return {
            "Predicted Status": final_label,
            "Average Scores": avg_scores
        }

    except Exception as e:
        return {"Error": str(e)}

# Gradio interface
gr.Interface(
     fn=predict,
     inputs=gr.Textbox(
         lines=10,
         placeholder="Enter one or more texts (one per line)",
         label="Input Texts"
     ),
     outputs=gr.JSON(
         label="Predicted Status & Scores"
     ),
     title="Mindscape AI Therapist (Multi-text Support)"
).launch()