prats010 commited on
Commit
1dbe0e5
·
verified ·
1 Parent(s): 810caef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -9
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import torch
 
4
 
5
  print("Loading MindBridge model...")
6
  model_name = "prats010/mindbridge-mental-health-model"
@@ -23,6 +24,29 @@ Never diagnose. Use "it sounds like..." not "you have...".
23
  If user mentions suicide or self harm, add [CRISIS] at the end of your response.
24
  Always recommend professional help for serious symptoms."""
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  def chat(user_input):
27
  input_text = f"{SYSTEM_CONTEXT}\n\nUser: {user_input}\nCounselor:"
28
 
@@ -38,13 +62,13 @@ def chat(user_input):
38
  outputs = model.generate(
39
  inputs,
40
  attention_mask=attention_mask,
41
- max_new_tokens=200,
42
- temperature=0.8,
43
- top_p=0.85,
44
- top_k=50,
45
  do_sample=True,
46
- repetition_penalty=1.5,
47
- no_repeat_ngram_size=3,
48
  pad_token_id=tokenizer.eos_token_id
49
  )
50
 
@@ -56,9 +80,10 @@ def chat(user_input):
56
  if "You are MindBridge" in response:
57
  response = response.split("You are MindBridge")[0].strip()
58
 
59
- sentences = response.split(". ")
60
- if len(sentences) > 4:
61
- response = ". ".join(sentences[:4]) + "."
 
62
 
63
  return response
64
 
 
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import torch
4
+ import re
5
 
6
  print("Loading MindBridge model...")
7
  model_name = "prats010/mindbridge-mental-health-model"
 
24
  If user mentions suicide or self harm, add [CRISIS] at the end of your response.
25
  Always recommend professional help for serious symptoms."""
26
 
27
+ def clean_response(text):
28
+ # Remove URLs
29
+ text = re.sub(r'http\S+|www\S+', '', text)
30
+ # Remove email addresses
31
+ text = re.sub(r'\S+@\S+', '', text)
32
+ # Remove social media handles
33
+ text = re.sub(r'facebook\S*|twitter\S*|instagram\S*', '', text, flags=re.IGNORECASE)
34
+ # Remove lines with names/personal info patterns
35
+ text = re.sub(r'My name is\s+\w+', '', text, flags=re.IGNORECASE)
36
+ # Remove excessive whitespace
37
+ text = re.sub(r'\s+', ' ', text).strip()
38
+ # Cut at first URL-like pattern or organization name
39
+ bad_patterns = ['MentalHealthCanada', 'myfitnesscareer', 'facebook.com', '.ca', '.com']
40
+ for pattern in bad_patterns:
41
+ if pattern in text:
42
+ text = text[:text.index(pattern)].strip()
43
+ # Keep only first 2 sentences
44
+ sentences = [s.strip() for s in text.split('.') if s.strip()]
45
+ sentences = [s for s in sentences if len(s) > 10]
46
+ if len(sentences) > 2:
47
+ text = '. '.join(sentences[:2]) + '.'
48
+ return text
49
+
50
  def chat(user_input):
51
  input_text = f"{SYSTEM_CONTEXT}\n\nUser: {user_input}\nCounselor:"
52
 
 
62
  outputs = model.generate(
63
  inputs,
64
  attention_mask=attention_mask,
65
+ max_new_tokens=100,
66
+ temperature=0.7,
67
+ top_p=0.8,
68
+ top_k=40,
69
  do_sample=True,
70
+ repetition_penalty=2.0,
71
+ no_repeat_ngram_size=4,
72
  pad_token_id=tokenizer.eos_token_id
73
  )
74
 
 
80
  if "You are MindBridge" in response:
81
  response = response.split("You are MindBridge")[0].strip()
82
 
83
+ response = clean_response(response)
84
+
85
+ if not response or len(response) < 10:
86
+ response = "I hear you. It sounds like you're going through a difficult time. Would you like to share more about what you're feeling?"
87
 
88
  return response
89