Spaces:
Build error
Build error
Create classification.py
Browse files- classification.py +32 -0
classification.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
import os
|
3 |
+
|
4 |
+
# Load API key from environment variable
|
5 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
6 |
+
|
7 |
+
def generate_classification_data(prompt, n_samples=10):
|
8 |
+
response = openai.Completion.create(
|
9 |
+
engine="text-davinci-003",
|
10 |
+
prompt=f"Generate {n_samples} examples of mental health text and their categories. Categories can be depression, anxiety, stress, or happiness. Format: 'text -> category'.",
|
11 |
+
max_tokens=150
|
12 |
+
)
|
13 |
+
|
14 |
+
generated_text = response.choices[0].text.strip().split("\n")
|
15 |
+
data = [line.split(" -> ") for line in generated_text if "->" in line]
|
16 |
+
|
17 |
+
return data
|
18 |
+
|
19 |
+
def classify_text(text):
|
20 |
+
training_data = generate_classification_data("mental health text classification")
|
21 |
+
|
22 |
+
X = [item[0] for item in training_data]
|
23 |
+
y = [item[1] for item in training_data]
|
24 |
+
|
25 |
+
if "sad" in text or "hopeless" in text:
|
26 |
+
return "depression"
|
27 |
+
elif "worried" in text or "anxious" in text:
|
28 |
+
return "anxiety"
|
29 |
+
elif "stressed" in text or "overwhelmed" in text:
|
30 |
+
return "stress"
|
31 |
+
else:
|
32 |
+
return "happiness"
|