Spaces:
Paused
Paused
Update app/main.py
Browse files- app/main.py +14 -3
app/main.py
CHANGED
|
@@ -5,10 +5,22 @@ from fastapi.responses import StreamingResponse, JSONResponse
|
|
| 5 |
from pydantic import BaseModel
|
| 6 |
from gradio_client import Client
|
| 7 |
import httpx
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
client = Client(SPACE_ID)
|
| 11 |
-
# Override the default HTTPX timeout to avoid read timeouts
|
| 12 |
client.client.timeout = httpx.Timeout(timeout=GRADIO_TIMEOUT)
|
| 13 |
|
| 14 |
|
|
@@ -19,7 +31,6 @@ def chat_with_gradio(message: str, api_name: str = DEFAULT_API):
|
|
| 19 |
try:
|
| 20 |
return client.predict(message=message, api_name=api_name)
|
| 21 |
except Exception as e:
|
| 22 |
-
# Specific handling for read timeout
|
| 23 |
msg = str(e)
|
| 24 |
if "ReadTimeout" in msg:
|
| 25 |
raise RuntimeError(f"Gradio API timed out after {GRADIO_TIMEOUT}s")
|
|
|
|
| 5 |
from pydantic import BaseModel
|
| 6 |
from gradio_client import Client
|
| 7 |
import httpx
|
| 8 |
+
import time
|
| 9 |
+
import json
|
| 10 |
|
| 11 |
+
# Load environment variables
|
| 12 |
+
load_dotenv()
|
| 13 |
+
|
| 14 |
+
# Configure from environment
|
| 15 |
+
SPACE_ID = os.getenv("SPACE_ID", "prithivMLmods/SAMBANOVA")
|
| 16 |
+
DEFAULT_API = os.getenv("DEFAULT_API", "/chat")
|
| 17 |
+
GRADIO_TIMEOUT = int(os.getenv("GRADIO_TIMEOUT", "60"))
|
| 18 |
+
API_KEY = os.getenv("API_KEY")
|
| 19 |
+
if not API_KEY:
|
| 20 |
+
raise RuntimeError("Missing API_KEY in environment")
|
| 21 |
+
|
| 22 |
+
# Initialize Gradio client and set timeout
|
| 23 |
client = Client(SPACE_ID)
|
|
|
|
| 24 |
client.client.timeout = httpx.Timeout(timeout=GRADIO_TIMEOUT)
|
| 25 |
|
| 26 |
|
|
|
|
| 31 |
try:
|
| 32 |
return client.predict(message=message, api_name=api_name)
|
| 33 |
except Exception as e:
|
|
|
|
| 34 |
msg = str(e)
|
| 35 |
if "ReadTimeout" in msg:
|
| 36 |
raise RuntimeError(f"Gradio API timed out after {GRADIO_TIMEOUT}s")
|