Spaces:
Sleeping
Sleeping
Upload llm_assistant.py with huggingface_hub
Browse files- llm_assistant.py +80 -79
llm_assistant.py
CHANGED
@@ -124,111 +124,112 @@ class TradeAssistant:
|
|
124 |
max_retries = 3
|
125 |
retry_delay = 2 # seconds
|
126 |
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
return {
|
147 |
-
"success":
|
148 |
-
"response":
|
149 |
-
"message": "
|
150 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
else:
|
152 |
-
print(f"Unexpected response format: {result}")
|
153 |
return {
|
154 |
"success": False,
|
155 |
-
"response":
|
156 |
-
"message": "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
}
|
158 |
-
|
159 |
-
|
|
|
|
|
|
|
|
|
160 |
return {
|
161 |
"success": False,
|
162 |
-
"response":
|
163 |
-
"message":
|
164 |
}
|
165 |
-
|
166 |
-
|
167 |
-
elif response.status_code == 503:
|
168 |
-
print(f"Model is loading. Attempt {attempt+1}/{max_retries}")
|
169 |
if attempt < max_retries - 1:
|
170 |
import time
|
171 |
time.sleep(retry_delay)
|
172 |
else:
|
173 |
return {
|
174 |
"success": False,
|
175 |
-
"response": "
|
176 |
-
"message": "
|
177 |
}
|
178 |
-
|
179 |
-
|
180 |
-
else:
|
181 |
-
print(f"Request failed with status code {response.status_code}: {response.text}")
|
182 |
if attempt < max_retries - 1:
|
183 |
import time
|
184 |
time.sleep(retry_delay)
|
185 |
else:
|
186 |
return {
|
187 |
"success": False,
|
188 |
-
"response": "
|
189 |
-
"message": f"
|
190 |
}
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
else:
|
198 |
-
return {
|
199 |
-
"success": False,
|
200 |
-
"response": "The request to the AI service timed out. Please try again later.",
|
201 |
-
"message": "Request timeout"
|
202 |
-
}
|
203 |
-
|
204 |
-
except requests.exceptions.ConnectionError:
|
205 |
-
print(f"Connection error. Attempt {attempt+1}/{max_retries}")
|
206 |
-
if attempt < max_retries - 1:
|
207 |
-
import time
|
208 |
-
time.sleep(retry_delay)
|
209 |
-
else:
|
210 |
-
return {
|
211 |
-
"success": False,
|
212 |
-
"response": "I'm having trouble connecting to the server. This might be due to network restrictions in the deployment environment.",
|
213 |
-
"message": "Connection error"
|
214 |
-
}
|
215 |
-
|
216 |
-
except Exception as e:
|
217 |
-
print(f"Unexpected error: {str(e)}")
|
218 |
-
if attempt < max_retries - 1:
|
219 |
-
import time
|
220 |
-
time.sleep(retry_delay)
|
221 |
-
else:
|
222 |
-
return {
|
223 |
-
"success": False,
|
224 |
-
"response": "An unexpected error occurred while processing your request.",
|
225 |
-
"message": f"Unexpected error: {str(e)}"
|
226 |
-
}
|
227 |
-
|
228 |
except Exception as e:
|
229 |
print(f"Exception during API request: {str(e)}")
|
230 |
return {
|
231 |
-
"success":
|
232 |
"response": self.get_fallback_response(user_question),
|
233 |
"message": f"Error querying LLM: {str(e)}"
|
234 |
}
|
|
|
124 |
max_retries = 3
|
125 |
retry_delay = 2 # seconds
|
126 |
|
127 |
+
try:
|
128 |
+
for attempt in range(max_retries):
|
129 |
+
try:
|
130 |
+
print(f"Attempt {attempt+1} of {max_retries} to query LLM at {self.api_url}")
|
131 |
+
print(f"API token begins with: {self.api_token[:5]}...")
|
132 |
+
|
133 |
+
# Make the API request
|
134 |
+
response = requests.post(
|
135 |
+
self.api_url,
|
136 |
+
headers=self.headers,
|
137 |
+
json=payload,
|
138 |
+
timeout=15 # Extended timeout for Spaces environment
|
139 |
+
)
|
140 |
+
|
141 |
+
# Process successful responses
|
142 |
+
if response.status_code == 200:
|
143 |
+
try:
|
144 |
+
result = response.json()
|
145 |
+
if isinstance(result, list) and len(result) > 0:
|
146 |
+
generated_text = result[0].get("generated_text", "")
|
147 |
+
return {
|
148 |
+
"success": True,
|
149 |
+
"response": generated_text,
|
150 |
+
"message": "Successfully generated response"
|
151 |
+
}
|
152 |
+
else:
|
153 |
+
print(f"Unexpected response format: {result}")
|
154 |
+
return {
|
155 |
+
"success": False,
|
156 |
+
"response": self.get_fallback_response(user_question),
|
157 |
+
"message": "Invalid response format"
|
158 |
+
}
|
159 |
+
except Exception as e:
|
160 |
+
print(f"Error processing response: {str(e)}")
|
161 |
return {
|
162 |
+
"success": False,
|
163 |
+
"response": self.get_fallback_response(user_question),
|
164 |
+
"message": f"Error processing response: {str(e)}"
|
165 |
}
|
166 |
+
# Handle model still loading
|
167 |
+
elif response.status_code == 503:
|
168 |
+
print(f"Model is loading. Attempt {attempt+1}/{max_retries}")
|
169 |
+
if attempt < max_retries - 1:
|
170 |
+
import time
|
171 |
+
time.sleep(retry_delay)
|
172 |
else:
|
|
|
173 |
return {
|
174 |
"success": False,
|
175 |
+
"response": "The AI model is currently initializing. Please try again in a moment.",
|
176 |
+
"message": "Model loading"
|
177 |
+
}
|
178 |
+
# Handle other error status codes
|
179 |
+
else:
|
180 |
+
print(f"Request failed with status code {response.status_code}: {response.text}")
|
181 |
+
if attempt < max_retries - 1:
|
182 |
+
import time
|
183 |
+
time.sleep(retry_delay)
|
184 |
+
else:
|
185 |
+
return {
|
186 |
+
"success": False,
|
187 |
+
"response": "I'm having trouble connecting to my knowledge base. Please try again later.",
|
188 |
+
"message": f"API error: {response.status_code}"
|
189 |
}
|
190 |
+
except requests.exceptions.Timeout:
|
191 |
+
print(f"Request timed out. Attempt {attempt+1}/{max_retries}")
|
192 |
+
if attempt < max_retries - 1:
|
193 |
+
import time
|
194 |
+
time.sleep(retry_delay)
|
195 |
+
else:
|
196 |
return {
|
197 |
"success": False,
|
198 |
+
"response": "The request to the AI service timed out. Please try again later.",
|
199 |
+
"message": "Request timeout"
|
200 |
}
|
201 |
+
except requests.exceptions.ConnectionError:
|
202 |
+
print(f"Connection error. Attempt {attempt+1}/{max_retries}")
|
|
|
|
|
203 |
if attempt < max_retries - 1:
|
204 |
import time
|
205 |
time.sleep(retry_delay)
|
206 |
else:
|
207 |
return {
|
208 |
"success": False,
|
209 |
+
"response": "I'm having trouble connecting to the server. This might be due to network restrictions in the deployment environment.",
|
210 |
+
"message": "Connection error"
|
211 |
}
|
212 |
+
except Exception as e:
|
213 |
+
print(f"Unexpected error: {str(e)}")
|
|
|
|
|
214 |
if attempt < max_retries - 1:
|
215 |
import time
|
216 |
time.sleep(retry_delay)
|
217 |
else:
|
218 |
return {
|
219 |
"success": False,
|
220 |
+
"response": "An unexpected error occurred while processing your request.",
|
221 |
+
"message": f"Unexpected error: {str(e)}"
|
222 |
}
|
223 |
+
# If all retries failed and we're still here
|
224 |
+
return {
|
225 |
+
"success": False,
|
226 |
+
"response": "I was unable to get a response after multiple attempts. Please try again later.",
|
227 |
+
"message": "All retries failed"
|
228 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
229 |
except Exception as e:
|
230 |
print(f"Exception during API request: {str(e)}")
|
231 |
return {
|
232 |
+
"success": False,
|
233 |
"response": self.get_fallback_response(user_question),
|
234 |
"message": f"Error querying LLM: {str(e)}"
|
235 |
}
|