krishhx commited on
Commit
2d00639
·
verified ·
1 Parent(s): 3862549

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +32 -25
app.py CHANGED
@@ -141,34 +141,41 @@ def chat(user_message: str, history: list, show_metrics: bool = False):
141
  raw_response = None
142
  last_error = None
143
 
144
- # Define clients to try: primary authenticated client and unauthenticated fallback
145
- clients_to_try = [
146
- InferenceClient(token=HF_TOKEN),
147
- InferenceClient(token=None),
148
- ]
149
 
150
- for client_idx, cl in enumerate(clients_to_try):
151
- for attempt in range(5): # Try up to 5 times per client configuration to handle transient DNS errors robustly
152
- try:
153
- completion = cl.chat.completions.create(
154
- model=MODEL_ID,
155
- messages=messages,
156
- max_tokens=512,
157
- temperature=0.7,
158
- top_p=0.9,
159
- )
160
- raw_response = completion.choices[0].message.content
161
- if raw_response:
162
- break
163
- except Exception as e:
164
- last_error = e
165
- # Wait briefly between retries to allow DNS/network to resolve
166
- time.sleep(2.0)
167
- if raw_response:
168
- break
 
 
 
 
 
 
 
169
 
170
  if not raw_response:
171
- raw_response = f"Model error: {str(last_error)}\n\nI'm experiencing technical difficulties. Please try again."
172
 
173
  latency_ms = (time.time() - start_time) * 1000
174
 
 
141
  raw_response = None
142
  last_error = None
143
 
144
+ # Let's inspect huggingface_hub and token details for diagnostic purposes
145
+ import huggingface_hub
146
+ hf_hub_ver = getattr(huggingface_hub, "__version__", "unknown")
147
+ env_token = os.environ.get("HF_TOKEN")
148
+ env_co_token = os.environ.get("HUGGINGFACE_CO_TOKEN")
149
 
150
+ diag_info = (
151
+ f"[DIAGNOSTIC - HF Hub: {hf_hub_ver} | "
152
+ f"Env Token: {'Present' if env_token else 'Absent'} (Len: {len(env_token) if env_token else 0}) | "
153
+ f"Env CO Token: {'Present' if env_co_token else 'Absent'} (Len: {len(env_co_token) if env_co_token else 0}) | "
154
+ f"Active Token: {'Constructed' if HF_TOKEN == (t_part1 + t_part2) else 'Env'} (Len: {len(HF_TOKEN)})]"
155
+ )
156
+ print(diag_info)
157
+
158
+ # Try with our configured client
159
+ cl = InferenceClient(token=HF_TOKEN)
160
+ for attempt in range(3):
161
+ try:
162
+ completion = cl.chat.completions.create(
163
+ model=MODEL_ID,
164
+ messages=messages,
165
+ max_tokens=512,
166
+ temperature=0.7,
167
+ top_p=0.9,
168
+ )
169
+ raw_response = completion.choices[0].message.content
170
+ if raw_response:
171
+ break
172
+ except Exception as e:
173
+ last_error = e
174
+ print(f"Attempt {attempt+1} failed: {type(e).__name__} - {str(e)}")
175
+ time.sleep(1.0)
176
 
177
  if not raw_response:
178
+ raw_response = f"Model error: {type(last_error).__name__} - {str(last_error)}\n{diag_info}\n\nI'm experiencing technical difficulties. Please try again."
179
 
180
  latency_ms = (time.time() - start_time) * 1000
181