Spaces:
Paused
Paused
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
from fastapi.responses import HTMLResponse, StreamingResponse
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
|
@@ -39,26 +40,39 @@ async def proxy_chat(request: Request):
|
|
| 39 |
timeout=180.0
|
| 40 |
)
|
| 41 |
|
|
|
|
| 42 |
if response.status_code != 200:
|
| 43 |
-
# 💯 FIX: Yahan WAF/API error ko safe tareeqe se read kiya gaya hai
|
| 44 |
error_content = b""
|
| 45 |
async for chunk in response.aiter_content():
|
| 46 |
if chunk:
|
| 47 |
error_content += chunk
|
| 48 |
-
|
| 49 |
safe_error = error_content.decode('utf-8', 'ignore').replace('"', "'").replace('\n', ' ')
|
| 50 |
-
error_msg = f"🚨
|
| 51 |
yield f'data: {{"choices":[{{"delta":{{"content":"{error_msg}"}}}}]}}\n\n'.encode('utf-8')
|
| 52 |
yield b'data: [DONE]\n\n'
|
| 53 |
return
|
| 54 |
|
| 55 |
-
#
|
|
|
|
| 56 |
async for chunk in response.aiter_content():
|
| 57 |
if chunk:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
yield chunk
|
| 59 |
|
| 60 |
except Exception as e:
|
| 61 |
-
error_msg = f"🚨
|
| 62 |
yield f'data: {{"choices":[{{"delta":{{"content":"{error_msg}"}}}}]}}\n\n'.encode('utf-8')
|
| 63 |
yield b'data: [DONE]\n\n'
|
| 64 |
|
|
|
|
| 1 |
+
import sys
|
| 2 |
from fastapi import FastAPI, Request
|
| 3 |
from fastapi.responses import HTMLResponse, StreamingResponse
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 40 |
timeout=180.0
|
| 41 |
)
|
| 42 |
|
| 43 |
+
# Agar saaf error 400 ya 401 aaye
|
| 44 |
if response.status_code != 200:
|
|
|
|
| 45 |
error_content = b""
|
| 46 |
async for chunk in response.aiter_content():
|
| 47 |
if chunk:
|
| 48 |
error_content += chunk
|
|
|
|
| 49 |
safe_error = error_content.decode('utf-8', 'ignore').replace('"', "'").replace('\n', ' ')
|
| 50 |
+
error_msg = f"🚨 API Status Error ({response.status_code}): {safe_error[:150]}..."
|
| 51 |
yield f'data: {{"choices":[{{"delta":{{"content":"{error_msg}"}}}}]}}\n\n'.encode('utf-8')
|
| 52 |
yield b'data: [DONE]\n\n'
|
| 53 |
return
|
| 54 |
|
| 55 |
+
# Agar 200 OK aaye, toh Pehla Chunk check karein (Spy Camera)
|
| 56 |
+
first_chunk = True
|
| 57 |
async for chunk in response.aiter_content():
|
| 58 |
if chunk:
|
| 59 |
+
if first_chunk:
|
| 60 |
+
# 1. Logs mein exact AgentRouter ka jawab print karega
|
| 61 |
+
print(f"\n--- 🔎 AGENTROUTER RESPONSE LOG ---\n{chunk[:500]}\n-----------------------------------\n", file=sys.stderr, flush=True)
|
| 62 |
+
first_chunk = False
|
| 63 |
+
|
| 64 |
+
# 2. Agar API Key ya Quota ka chupa hua error hai (Jo JSON form mein aata hai)
|
| 65 |
+
if chunk.lstrip().startswith(b'{') and b'"error"' in chunk:
|
| 66 |
+
safe_err = chunk.decode('utf-8', 'ignore').replace('"', "'").replace('\n', ' ')
|
| 67 |
+
yield f'data: {{"choices":[{{"delta":{{"content":"🚨 **API/Key Error:** {safe_err}"}}}}]}}\n\n'.encode('utf-8')
|
| 68 |
+
yield b'data: [DONE]\n\n'
|
| 69 |
+
return
|
| 70 |
+
|
| 71 |
+
# Normal AI Text Data
|
| 72 |
yield chunk
|
| 73 |
|
| 74 |
except Exception as e:
|
| 75 |
+
error_msg = f"🚨 Network Drop: {str(e)}"
|
| 76 |
yield f'data: {{"choices":[{{"delta":{{"content":"{error_msg}"}}}}]}}\n\n'.encode('utf-8')
|
| 77 |
yield b'data: [DONE]\n\n'
|
| 78 |
|