jaafarhh commited on
Commit
4dcf57d
·
verified ·
1 Parent(s): 8133539

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -23
app.py CHANGED
@@ -31,20 +31,19 @@ css = """
31
  </style>
32
  """
33
 
34
- # Prompt template
35
  PROMPT_TEMPLATE = """
36
- You are a professional therapist who speaks Moroccan Arabic (Darija).
37
- Respond with empathy and use therapeutic techniques.
38
- Always respond in Darija unless specifically asked to use another language.
39
 
40
  Previous conversation:
41
  {chat_history}
42
 
43
  User message: {question}
44
 
45
- Additional context: {context}
46
-
47
- Therapeutic response in Darija:
48
  """
49
 
50
  class DarijaTherapist:
@@ -67,31 +66,35 @@ class DarijaTherapist:
67
 
68
  # Configure retry strategy
69
  retry_strategy = Retry(
70
- total=3, # number of retries
71
- backoff_factor=1, # wait 1, 2, 4 seconds between retries
72
- status_forcelist=[429, 500, 502, 503, 504] # HTTP status codes to retry on
73
  )
74
 
75
  # Create session with retry strategy
76
  session = requests.Session()
77
  session.mount("https://", HTTPAdapter(max_retries=retry_strategy))
78
 
79
- # LLM setup with increased timeout and retry session
80
  self.llm = HuggingFaceEndpoint(
81
- endpoint_url="https://api-inference.huggingface.co/models/MBZUAI-Paris/Atlas-Chat-2B",
82
  task="text-generation",
83
  temperature=0.7,
84
  do_sample=True,
85
  return_full_text=False,
86
- timeout = 300, # increase timeout to 5 minutes
87
  model_kwargs={
88
- "max_length": 512
89
- },
 
 
 
 
90
  huggingfacehub_api_token=os.getenv("HUGGINGFACE_API_TOKEN"),
91
- client=session # use configured session
92
  )
93
 
94
- # Rest of the setup remains the same
95
  self.embeddings = HuggingFaceBgeEmbeddings(
96
  model_name="BAAI/bge-large-en"
97
  )
@@ -176,27 +179,35 @@ class DarijaTherapist:
176
  "question": user_input,
177
  "chat_history": self.memory.chat_memory.messages[-5:] # Limit context window
178
  })
 
 
 
 
 
 
 
179
  return response['answer']
180
 
181
  except requests.exceptions.HTTPError as e:
182
  if e.response.status_code == 424:
183
  if attempt < max_retries - 1:
184
  st.warning("Model error, retrying with simplified input...")
185
- # Try with shorter context
186
- user_input = user_input[:256]
187
  time.sleep(2 ** attempt)
188
  continue
189
- return "عذراً، كاين مشكل مع النموذج. جرب سؤال أقصر."
190
-
191
  except requests.exceptions.ReadTimeout:
192
  if attempt < max_retries - 1:
193
  st.warning(f"Attempt {attempt + 1} timed out, retrying...")
194
  time.sleep(2 ** attempt)
195
  continue
196
  return "عذراً، الخادم بطيء حالياً. حاول مرة أخرى."
197
-
198
  except Exception as e:
199
  st.error(f"Error: {str(e)}")
 
 
 
200
  return "عذراً، كاين شي مشكل. حاول مرة أخرى."
201
 
202
  def run(self):
@@ -241,7 +252,8 @@ class DarijaTherapist:
241
 
242
  with st.spinner("جاري التفكير..."):
243
  ai_response = self.get_ai_response(user_input)
244
- st.session_state.messages.append({"role": "assistant", "content": ai_response})
 
245
 
246
  if __name__ == "__main__":
247
  app = DarijaTherapist()
 
31
  </style>
32
  """
33
 
34
+ # Updated prompt template for Mixtral
35
  PROMPT_TEMPLATE = """
36
+ <s>[INST] You are a professional therapist who speaks Moroccan Arabic (Darija).
37
+ Act as a compassionate therapist and provide empathetic responses using therapeutic techniques.
38
+ Always respond in Darija unless specifically asked otherwise.
39
 
40
  Previous conversation:
41
  {chat_history}
42
 
43
  User message: {question}
44
 
45
+ Context: {context}
46
+ [/INST]
 
47
  """
48
 
49
  class DarijaTherapist:
 
66
 
67
  # Configure retry strategy
68
  retry_strategy = Retry(
69
+ total=3,
70
+ backoff_factor=1,
71
+ status_forcelist=[429, 500, 502, 503, 504]
72
  )
73
 
74
  # Create session with retry strategy
75
  session = requests.Session()
76
  session.mount("https://", HTTPAdapter(max_retries=retry_strategy))
77
 
78
+ # Updated LLM setup for Mixtral
79
  self.llm = HuggingFaceEndpoint(
80
+ endpoint_url="https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1",
81
  task="text-generation",
82
  temperature=0.7,
83
  do_sample=True,
84
  return_full_text=False,
85
+ timeout=300,
86
  model_kwargs={
87
+ "max_new_tokens": 2048,
88
+ "top_p": 0.9,
89
+ "repetition_penalty": 1.2,
90
+ "return_text": True,
91
+ "stop": ["</s>"]
92
+ },
93
  huggingfacehub_api_token=os.getenv("HUGGINGFACE_API_TOKEN"),
94
+ client=session
95
  )
96
 
97
+ # Embeddings setup
98
  self.embeddings = HuggingFaceBgeEmbeddings(
99
  model_name="BAAI/bge-large-en"
100
  )
 
179
  "question": user_input,
180
  "chat_history": self.memory.chat_memory.messages[-5:] # Limit context window
181
  })
182
+
183
+ if not response or 'answer' not in response:
184
+ if attempt < max_retries - 1:
185
+ time.sleep(2 ** attempt)
186
+ continue
187
+ return "عذراً، كاين مشكل. حاول مرة أخرى."
188
+
189
  return response['answer']
190
 
191
  except requests.exceptions.HTTPError as e:
192
  if e.response.status_code == 424:
193
  if attempt < max_retries - 1:
194
  st.warning("Model error, retrying with simplified input...")
 
 
195
  time.sleep(2 ** attempt)
196
  continue
197
+ return "عذراً، كاين مشكل مع النموذج. جرب سؤال أقصر."
198
+
199
  except requests.exceptions.ReadTimeout:
200
  if attempt < max_retries - 1:
201
  st.warning(f"Attempt {attempt + 1} timed out, retrying...")
202
  time.sleep(2 ** attempt)
203
  continue
204
  return "عذراً، الخادم بطيء حالياً. حاول مرة أخرى."
205
+
206
  except Exception as e:
207
  st.error(f"Error: {str(e)}")
208
+ if attempt < max_retries - 1:
209
+ time.sleep(2 ** attempt)
210
+ continue
211
  return "عذراً، كاين شي مشكل. حاول مرة أخرى."
212
 
213
  def run(self):
 
252
 
253
  with st.spinner("جاري التفكير..."):
254
  ai_response = self.get_ai_response(user_input)
255
+ if ai_response:
256
+ st.session_state.messages.append({"role": "assistant", "content": ai_response})
257
 
258
  if __name__ == "__main__":
259
  app = DarijaTherapist()