Update apis/chat_api.py
Browse files- apis/chat_api.py +11 -3
apis/chat_api.py
CHANGED
@@ -5,6 +5,7 @@ import time
|
|
5 |
import uvicorn
|
6 |
import requests
|
7 |
import asyncio
|
|
|
8 |
|
9 |
from pathlib import Path
|
10 |
from fastapi import FastAPI, Depends, HTTPException
|
@@ -192,11 +193,18 @@ class ChatAPIApp:
|
|
192 |
headers = {"Authorization": f"Bearer {api_key}"}
|
193 |
response = requests.post(api_url, headers=headers, json={"inputs": input_text})
|
194 |
result = response.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
195 |
if isinstance(result, list) and len(result) > 0 and isinstance(result[0], list):
|
196 |
-
return [item for sublist in result for item in sublist] # Flatten
|
197 |
-
elif "error" in result:
|
198 |
-
raise RuntimeError("The model is currently loading, please re-run the query.")
|
199 |
else:
|
|
|
200 |
raise RuntimeError("Unexpected response format.")
|
201 |
|
202 |
async def embedding(self, request: QueryRequest, api_key: str = Depends(extract_api_key)):
|
|
|
5 |
import uvicorn
|
6 |
import requests
|
7 |
import asyncio
|
8 |
+
import logging
|
9 |
|
10 |
from pathlib import Path
|
11 |
from fastapi import FastAPI, Depends, HTTPException
|
|
|
193 |
headers = {"Authorization": f"Bearer {api_key}"}
|
194 |
response = requests.post(api_url, headers=headers, json={"inputs": input_text})
|
195 |
result = response.json()
|
196 |
+
|
197 |
+
# Improved error handling and logging
|
198 |
+
if "error" in result:
|
199 |
+
logging.error(f"Error from Hugging Face API: {result['error']}")
|
200 |
+
# More detailed error message
|
201 |
+
error_detail = result.get('error', 'No detailed error message provided.')
|
202 |
+
raise RuntimeError(f"The model is currently loading, please re-run the query. Detail: {error_detail}")
|
203 |
+
|
204 |
if isinstance(result, list) and len(result) > 0 and isinstance(result[0], list):
|
205 |
+
return [item for sublist in result for item in sublist] # Flatten list of lists
|
|
|
|
|
206 |
else:
|
207 |
+
logging.error(f"Unexpected response format: {result}")
|
208 |
raise RuntimeError("Unexpected response format.")
|
209 |
|
210 |
async def embedding(self, request: QueryRequest, api_key: str = Depends(extract_api_key)):
|