Update main.py
Browse files
main.py
CHANGED
|
@@ -2,7 +2,15 @@ import httpx
|
|
| 2 |
from fastapi import FastAPI, Request, HTTPException
|
| 3 |
from fastapi.responses import StreamingResponse
|
| 4 |
import json
|
| 5 |
-
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
|
@@ -29,39 +37,35 @@ async def proxy_deepinfra(request: Request):
|
|
| 29 |
'sec-ch-ua': request.headers.get('sec-ch-ua', '"Chromium";v="140", "Not=A?Brand";v="24", "Google Chrome";v="140"'),
|
| 30 |
'sec-ch-ua-mobile': request.headers.get('sec-ch-ua-mobile', '?0'),
|
| 31 |
'User-Agent': request.headers.get('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36'),
|
| 32 |
-
'accept': request.headers.get('accept', 'text
|
| 33 |
'X-Deepinfra-Source': request.headers.get('X-Deepinfra-Source', 'web-embed'),
|
| 34 |
'Content-Type': request.headers.get('Content-Type', 'application/json'),
|
| 35 |
}
|
| 36 |
|
| 37 |
-
# Create a randomized list of URLs for this specific request
|
| 38 |
shuffled_urls = random.sample(API_URLS, len(API_URLS))
|
| 39 |
|
| 40 |
async def stream_generator():
|
| 41 |
last_error = None
|
| 42 |
for url in shuffled_urls:
|
| 43 |
-
|
| 44 |
try:
|
| 45 |
async with httpx.AsyncClient() as client:
|
| 46 |
-
# Setting timeout=None disables client-side timeouts
|
| 47 |
async with client.stream("POST", url, headers=headers, json=body, timeout=None) as response:
|
| 48 |
response.raise_for_status()
|
| 49 |
-
|
| 50 |
-
print(f"Successfully connected. Streaming from: {url}")
|
| 51 |
async for chunk in response.aiter_bytes():
|
| 52 |
yield chunk
|
| 53 |
-
return
|
| 54 |
except (httpx.RequestError, httpx.HTTPStatusError) as e:
|
| 55 |
last_error = e
|
| 56 |
-
|
| 57 |
continue
|
| 58 |
|
| 59 |
-
# This part is reached only if all URLs in the shuffled list failed
|
| 60 |
if last_error:
|
| 61 |
-
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
return StreamingResponse(stream_generator(), media_type="text
|
| 64 |
|
| 65 |
-
if __name__ == "__main__"
|
| 66 |
-
import uvicorn
|
| 67 |
-
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
| 2 |
from fastapi import FastAPI, Request, HTTPException
|
| 3 |
from fastapi.responses import StreamingResponse
|
| 4 |
import json
|
| 5 |
+
import random
|
| 6 |
+
import logging
|
| 7 |
+
|
| 8 |
+
# Configure logging to output to stdout
|
| 9 |
+
logging.basicConfig(
|
| 10 |
+
level=logging.INFO,
|
| 11 |
+
format="%(asctime)s - %(levelname)s - %(message)s",
|
| 12 |
+
datefmt="%Y-%m-%d %H:%M:%S",
|
| 13 |
+
)
|
| 14 |
|
| 15 |
app = FastAPI()
|
| 16 |
|
|
|
|
| 37 |
'sec-ch-ua': request.headers.get('sec-ch-ua', '"Chromium";v="140", "Not=A?Brand";v="24", "Google Chrome";v="140"'),
|
| 38 |
'sec-ch-ua-mobile': request.headers.get('sec-ch-ua-mobile', '?0'),
|
| 39 |
'User-Agent': request.headers.get('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36'),
|
| 40 |
+
'accept': request.headers.get('accept', 'text-event-stream'),
|
| 41 |
'X-Deepinfra-Source': request.headers.get('X-Deepinfra-Source', 'web-embed'),
|
| 42 |
'Content-Type': request.headers.get('Content-Type', 'application/json'),
|
| 43 |
}
|
| 44 |
|
|
|
|
| 45 |
shuffled_urls = random.sample(API_URLS, len(API_URLS))
|
| 46 |
|
| 47 |
async def stream_generator():
|
| 48 |
last_error = None
|
| 49 |
for url in shuffled_urls:
|
| 50 |
+
logging.info(f"Attempting to connect to: {url}")
|
| 51 |
try:
|
| 52 |
async with httpx.AsyncClient() as client:
|
|
|
|
| 53 |
async with client.stream("POST", url, headers=headers, json=body, timeout=None) as response:
|
| 54 |
response.raise_for_status()
|
| 55 |
+
logging.info(f"Successfully connected. Streaming from: {url}")
|
|
|
|
| 56 |
async for chunk in response.aiter_bytes():
|
| 57 |
yield chunk
|
| 58 |
+
return
|
| 59 |
except (httpx.RequestError, httpx.HTTPStatusError) as e:
|
| 60 |
last_error = e
|
| 61 |
+
logging.warning(f"Failed to connect to {url}: {e}. Trying next URL.")
|
| 62 |
continue
|
| 63 |
|
|
|
|
| 64 |
if last_error:
|
| 65 |
+
logging.error(f"All API endpoints failed. Last error: {last_error}")
|
| 66 |
+
# In a streaming response, we can't easily raise an HTTPException after starting.
|
| 67 |
+
# The connection will simply close, which the client will see as a failed request.
|
| 68 |
|
| 69 |
+
return StreamingResponse(stream_generator(), media_type="text-event-stream")
|
| 70 |
|
| 71 |
+
# The `if __name__ == "__main__":` block is removed as Gunicorn will run the app.
|
|
|
|
|
|