Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files
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 |
-
#
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|