vivianoh commited on
Commit
ffeb0c8
·
verified ·
1 Parent(s): 1745079

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -14
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
- skincare_text = f.read()
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(skincare_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 is_skincare_related(query):
72
- skincare_keywords = [
73
- 'skin', 'skincare', 'acne', 'wrinkles', 'moisturizer', 'cleanser',
74
- 'sunscreen', 'serum', 'retinol', 'vitamin', 'dry', 'oily', 'sensitive',
75
- 'aging', 'pores', 'blackheads', 'routine', 'face', 'facial', 'beauty',
76
- 'dermatology', 'cosmetic', 'cream', 'lotion', 'toner', 'exfoliate',
77
- 'hydration', 'anti-aging', 'blemish', 'spot', 'dark circles'
 
78
  ]
79
 
80
  query_lower = query.lower()
81
- return any(keyword in query_lower for keyword in skincare_keywords)
82
 
83
  queries = [
84
- "Consistent skincare routine",
85
- "Applying sunscreen daily",
86
- "Choosing products that match your skin type"
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 skincare. Base your response on the following information: {top_results}"}]
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)