Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,7 @@ client = InferenceClient ('Qwen/Qwen2.5-72B-Instruct')
|
|
| 9 |
|
| 10 |
# Step 1
|
| 11 |
with open("Mental health chatbot text.txt", "r", encoding="utf-8") as f:
|
| 12 |
-
|
| 13 |
|
| 14 |
# Step 2: Preprocess text into sentence chunks
|
| 15 |
def preprocess_text(text):
|
|
@@ -39,7 +39,7 @@ def preprocess_text(text):
|
|
| 39 |
print(f"Sample chunks: {final_chunks[:3]}")
|
| 40 |
return final_chunks
|
| 41 |
|
| 42 |
-
cleaned_chunks = preprocess_text(
|
| 43 |
|
| 44 |
# Step 3: Convert chunks into embeddings
|
| 45 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
|
@@ -68,22 +68,23 @@ def get_top_chunks(query, chunk_embeddings, text_chunks, top_k=3):
|
|
| 68 |
return results, top_scores[:len(results)]
|
| 69 |
|
| 70 |
# Step 5: Relevance checker
|
| 71 |
-
def
|
| 72 |
-
|
| 73 |
-
'
|
| 74 |
-
'
|
| 75 |
-
'
|
| 76 |
-
'
|
| 77 |
-
'
|
|
|
|
| 78 |
]
|
| 79 |
|
| 80 |
query_lower = query.lower()
|
| 81 |
-
return any(keyword in query_lower for keyword in
|
| 82 |
|
| 83 |
queries = [
|
| 84 |
-
"
|
| 85 |
-
"
|
| 86 |
-
"
|
| 87 |
]
|
| 88 |
|
| 89 |
for q in queries:
|
|
@@ -96,7 +97,7 @@ def respond(message, history):
|
|
| 96 |
top_results = get_top_chunks(message, chunk_embeddings, cleaned_chunks)
|
| 97 |
print(top_results)
|
| 98 |
|
| 99 |
-
messages = [{"role": "system", "content": f"You are a friendly chatbot. You give people advice about
|
| 100 |
|
| 101 |
if history:
|
| 102 |
messages.extend(history)
|
|
|
|
| 9 |
|
| 10 |
# Step 1
|
| 11 |
with open("Mental health chatbot text.txt", "r", encoding="utf-8") as f:
|
| 12 |
+
mental_health_text = f.read()
|
| 13 |
|
| 14 |
# Step 2: Preprocess text into sentence chunks
|
| 15 |
def preprocess_text(text):
|
|
|
|
| 39 |
print(f"Sample chunks: {final_chunks[:3]}")
|
| 40 |
return final_chunks
|
| 41 |
|
| 42 |
+
cleaned_chunks = preprocess_text(mental_health_text)
|
| 43 |
|
| 44 |
# Step 3: Convert chunks into embeddings
|
| 45 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
|
|
|
| 68 |
return results, top_scores[:len(results)]
|
| 69 |
|
| 70 |
# Step 5: Relevance checker
|
| 71 |
+
def is_mental_health_related(query):
|
| 72 |
+
mental_health_keywords = [
|
| 73 |
+
'anxiety', 'depression', 'stress', 'mental health', 'therapy', 'counseling',
|
| 74 |
+
'mood', 'emotions', 'feelings', 'wellbeing', 'self-care', 'mindfulness',
|
| 75 |
+
'meditation', 'coping', 'support', 'psychology', 'psychiatry', 'bipolar',
|
| 76 |
+
'trauma', 'PTSD', 'panic', 'worry', 'sad', 'happy', 'angry', 'fear',
|
| 77 |
+
'self-esteem', 'confidence', 'resilience', 'healing', 'recovery',
|
| 78 |
+
'mental', 'emotional', 'psychological', 'behavioral', 'cognitive'
|
| 79 |
]
|
| 80 |
|
| 81 |
query_lower = query.lower()
|
| 82 |
+
return any(keyword in query_lower for keyword in mental_health_keywords)
|
| 83 |
|
| 84 |
queries = [
|
| 85 |
+
"Managing daily stress and anxiety",
|
| 86 |
+
"Building healthy coping mechanisms",
|
| 87 |
+
"Practicing mindfulness and self-care"
|
| 88 |
]
|
| 89 |
|
| 90 |
for q in queries:
|
|
|
|
| 97 |
top_results = get_top_chunks(message, chunk_embeddings, cleaned_chunks)
|
| 98 |
print(top_results)
|
| 99 |
|
| 100 |
+
messages = [{"role": "system", "content": f"You are a friendly chatbot. You give people advice about mental-health. Base your response on the following information: {top_results}"}]
|
| 101 |
|
| 102 |
if history:
|
| 103 |
messages.extend(history)
|