Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -12,6 +12,10 @@ from langchain_community.vectorstores import FAISS
|
|
12 |
from langchain.prompts import PromptTemplate
|
13 |
import os
|
14 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
|
15 |
|
16 |
# Load environment variables
|
17 |
load_dotenv()
|
@@ -61,25 +65,37 @@ class DarijaTherapist:
|
|
61 |
device=self.device
|
62 |
)
|
63 |
|
64 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
self.llm = HuggingFaceEndpoint(
|
66 |
-
endpoint_url="https://api-inference.huggingface.co/models/MBZUAI-Paris/Atlas-Chat-
|
67 |
task="text-generation",
|
68 |
temperature=0.7,
|
69 |
do_sample=True,
|
70 |
return_full_text=False,
|
71 |
model_kwargs={
|
72 |
"max_length": 512,
|
|
|
73 |
},
|
74 |
-
huggingfacehub_api_token=os.getenv("HUGGINGFACE_API_TOKEN")
|
|
|
75 |
)
|
76 |
|
77 |
-
#
|
78 |
self.embeddings = HuggingFaceBgeEmbeddings(
|
79 |
model_name="BAAI/bge-large-en"
|
80 |
)
|
81 |
|
82 |
-
# Vector store setup
|
83 |
self.vectorstore = FAISS.from_texts(
|
84 |
["Initial therapeutic context"],
|
85 |
self.embeddings
|
@@ -145,15 +161,23 @@ class DarijaTherapist:
|
|
145 |
st.session_state.audio_buffer = []
|
146 |
|
147 |
def get_ai_response(self, user_input):
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
|
158 |
def run(self):
|
159 |
st.set_page_config(page_title="Darija AI Therapist", page_icon="🧠")
|
|
|
12 |
from langchain.prompts import PromptTemplate
|
13 |
import os
|
14 |
from dotenv import load_dotenv
|
15 |
+
from requests.adapters import HTTPAdapter
|
16 |
+
from requests.packages.urllib3.util.retry import Retry
|
17 |
+
import requests
|
18 |
+
import time
|
19 |
|
20 |
# Load environment variables
|
21 |
load_dotenv()
|
|
|
65 |
device=self.device
|
66 |
)
|
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-9B",
|
82 |
task="text-generation",
|
83 |
temperature=0.7,
|
84 |
do_sample=True,
|
85 |
return_full_text=False,
|
86 |
model_kwargs={
|
87 |
"max_length": 512,
|
88 |
+
"timeout": 300 # increase timeout to 5 minutes
|
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 |
)
|
98 |
|
|
|
99 |
self.vectorstore = FAISS.from_texts(
|
100 |
["Initial therapeutic context"],
|
101 |
self.embeddings
|
|
|
161 |
st.session_state.audio_buffer = []
|
162 |
|
163 |
def get_ai_response(self, user_input):
|
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):
|
183 |
st.set_page_config(page_title="Darija AI Therapist", page_icon="🧠")
|