Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import os
|
|
| 4 |
import re
|
| 5 |
import html
|
| 6 |
import time
|
|
|
|
| 7 |
from pathlib import Path
|
| 8 |
|
| 9 |
# Import Groq API client
|
|
@@ -198,37 +199,88 @@ 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 |
-
# 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 |
-
|
| 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 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 226 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
data = all_data.get(city)
|
| 233 |
if data is None:
|
| 234 |
return "City data not found"
|
|
|
|
| 4 |
import re
|
| 5 |
import html
|
| 6 |
import time
|
| 7 |
+
import sys
|
| 8 |
from pathlib import Path
|
| 9 |
|
| 10 |
# Import Groq API client
|
|
|
|
| 199 |
return "Error: Groq API key not provided. Please enter your API key in the field above."
|
| 200 |
|
| 201 |
# Try to initialize the Groq client with the provided API key
|
| 202 |
+
# Handle potential proxy-related issues on Hugging Face with multiple fallback strategies
|
| 203 |
+
client = None
|
| 204 |
+
|
| 205 |
+
# Strategy 1: Try basic initialization
|
| 206 |
try:
|
|
|
|
| 207 |
client = Groq(api_key=api_key)
|
| 208 |
except TypeError as e:
|
| 209 |
if "proxies" in str(e):
|
| 210 |
+
# Strategy 2: Clear proxy environment variables and try again
|
| 211 |
try:
|
|
|
|
| 212 |
import os
|
| 213 |
+
proxy_vars = ['http_proxy', 'https_proxy', 'HTTP_PROXY', 'HTTPS_PROXY', 'no_proxy', 'NO_PROXY']
|
|
|
|
| 214 |
original_values = {}
|
| 215 |
+
|
| 216 |
+
# Store and clear all proxy-related env vars
|
| 217 |
for var in proxy_vars:
|
| 218 |
if var in os.environ:
|
| 219 |
original_values[var] = os.environ[var]
|
| 220 |
del os.environ[var]
|
| 221 |
|
| 222 |
+
# Also try to clear any requests-related proxy settings
|
| 223 |
+
import sys
|
| 224 |
+
if 'requests' in sys.modules:
|
| 225 |
+
import requests
|
| 226 |
+
# Clear any session-level proxy settings
|
| 227 |
+
requests.Session.proxies = {}
|
| 228 |
+
|
| 229 |
+
# Try initializing again with clean environment
|
| 230 |
client = Groq(api_key=api_key)
|
| 231 |
|
| 232 |
# Restore original environment variables
|
| 233 |
for var, value in original_values.items():
|
| 234 |
os.environ[var] = value
|
| 235 |
|
| 236 |
+
except Exception as fallback_e1:
|
| 237 |
+
# Strategy 3: Try importing and using Groq differently
|
| 238 |
+
try:
|
| 239 |
+
# Force reload the Groq module to clear any cached configurations
|
| 240 |
+
if 'groq' in sys.modules:
|
| 241 |
+
import importlib
|
| 242 |
+
importlib.reload(sys.modules['groq'])
|
| 243 |
+
|
| 244 |
+
from groq import Groq as GroqClient
|
| 245 |
+
|
| 246 |
+
# Try with explicit parameter naming
|
| 247 |
+
client = GroqClient(api_key=api_key)
|
| 248 |
+
|
| 249 |
+
except Exception as fallback_e2:
|
| 250 |
+
# Strategy 4: Try creating a minimal client configuration
|
| 251 |
+
try:
|
| 252 |
+
# Import inspect to check function signature
|
| 253 |
+
import inspect
|
| 254 |
+
groq_init_sig = inspect.signature(Groq.__init__)
|
| 255 |
+
|
| 256 |
+
# Create kwargs with only supported parameters
|
| 257 |
+
valid_kwargs = {'api_key': api_key}
|
| 258 |
+
|
| 259 |
+
# Only include parameters that exist in the constructor
|
| 260 |
+
for param_name in groq_init_sig.parameters:
|
| 261 |
+
if param_name in ['self']:
|
| 262 |
+
continue
|
| 263 |
+
if param_name == 'api_key':
|
| 264 |
+
valid_kwargs['api_key'] = api_key
|
| 265 |
+
|
| 266 |
+
client = Groq(**valid_kwargs)
|
| 267 |
+
|
| 268 |
+
except Exception as fallback_e3:
|
| 269 |
+
return (f"Error initializing Groq client after multiple attempts:\n"
|
| 270 |
+
f"Original error: {str(e)}\n"
|
| 271 |
+
f"Fallback 1 failed: {str(fallback_e1)}\n"
|
| 272 |
+
f"Fallback 2 failed: {str(fallback_e2)}\n"
|
| 273 |
+
f"Fallback 3 failed: {str(fallback_e3)}\n"
|
| 274 |
+
f"This might be due to version incompatibility or environment configuration on Hugging Face.")
|
| 275 |
else:
|
| 276 |
return f"Error initializing Groq client: {str(e)}"
|
| 277 |
except Exception as e:
|
| 278 |
return f"Error initializing Groq client: {str(e)}"
|
| 279 |
|
| 280 |
+
# Check if client was successfully created
|
| 281 |
+
if client is None:
|
| 282 |
+
return "Failed to initialize Groq client after all attempts."
|
| 283 |
+
|
| 284 |
data = all_data.get(city)
|
| 285 |
if data is None:
|
| 286 |
return "City data not found"
|