jaafarhh commited on
Commit
8133539
·
verified ·
1 Parent(s): 197dd0d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -3
app.py CHANGED
@@ -164,19 +164,39 @@ class DarijaTherapist:
164
  max_retries = 3
165
  for attempt in range(max_retries):
166
  try:
 
 
 
 
 
 
 
 
167
  response = self.conversation_chain({
168
  "question": user_input,
169
- "chat_history": self.memory.chat_memory.messages
170
  })
171
  return response['answer']
 
 
 
 
 
 
 
 
 
 
 
172
  except requests.exceptions.ReadTimeout:
173
  if attempt < max_retries - 1:
174
  st.warning(f"Attempt {attempt + 1} timed out, retrying...")
175
- time.sleep(2 ** attempt) # exponential backoff
176
  continue
177
  return "عذراً، الخادم بطيء حالياً. حاول مرة أخرى."
 
178
  except Exception as e:
179
- st.error(f"Error getting AI response: {str(e)}")
180
  return "عذراً، كاين شي مشكل. حاول مرة أخرى."
181
 
182
  def run(self):
 
164
  max_retries = 3
165
  for attempt in range(max_retries):
166
  try:
167
+ # Validate and clean input
168
+ if not user_input or len(user_input.strip()) == 0:
169
+ return "عذراً، ما فهمتش السؤال ديالك. عاود من فضلك."
170
+
171
+ # Limit input length to prevent tensor size issues
172
+ if len(user_input) > 512:
173
+ user_input = user_input[:512]
174
+
175
  response = self.conversation_chain({
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):