bestroi commited on
Commit
6c93180
·
verified ·
1 Parent(s): 6c9d68a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -0
app.py CHANGED
@@ -198,8 +198,34 @@ def generate_answer_with_groq(city, question, max_sources=3, api_key=None, tempe
198
  return "Error: Groq API key not provided. Please enter your API key in the field above."
199
 
200
  # Try to initialize the Groq client with the provided API key
 
201
  try:
 
202
  client = Groq(api_key=api_key)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
  except Exception as e:
204
  return f"Error initializing Groq client: {str(e)}"
205
 
 
198
  return "Error: Groq API key not provided. Please enter your API key in the field above."
199
 
200
  # Try to initialize the Groq client with the provided API key
201
+ # Handle potential proxy-related issues on Hugging Face
202
  try:
203
+ # First try with just the API key
204
  client = Groq(api_key=api_key)
205
+ except TypeError as e:
206
+ if "proxies" in str(e):
207
+ try:
208
+ # Try alternative initialization approaches for Hugging Face environment
209
+ import os
210
+ # Clear any proxy-related environment variables that might interfere
211
+ proxy_vars = ['http_proxy', 'https_proxy', 'HTTP_PROXY', 'HTTPS_PROXY']
212
+ original_values = {}
213
+ for var in proxy_vars:
214
+ if var in os.environ:
215
+ original_values[var] = os.environ[var]
216
+ del os.environ[var]
217
+
218
+ # Try initializing again
219
+ client = Groq(api_key=api_key)
220
+
221
+ # Restore original environment variables
222
+ for var, value in original_values.items():
223
+ os.environ[var] = value
224
+
225
+ except Exception as fallback_e:
226
+ return f"Error initializing Groq client: {str(e)}. Fallback attempt failed: {str(fallback_e)}"
227
+ else:
228
+ return f"Error initializing Groq client: {str(e)}"
229
  except Exception as e:
230
  return f"Error initializing Groq client: {str(e)}"
231