Spaces:
Running
Running
Upload 10 files
Browse files
app.py
CHANGED
|
@@ -3,6 +3,11 @@ import httpx
|
|
| 3 |
from fastapi import FastAPI, Request, HTTPException
|
| 4 |
from fastapi.responses import HTMLResponse
|
| 5 |
from fastapi.staticfiles import StaticFiles
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# --- Configuration ---
|
| 8 |
# Get the API key from Hugging Face Secrets
|
|
@@ -29,8 +34,11 @@ async def proxy_deepseek(request: Request):
|
|
| 29 |
This endpoint receives the request from our frontend (index.html),
|
| 30 |
adds the secret API key, and forwards it to the DeepSeek API.
|
| 31 |
"""
|
|
|
|
|
|
|
| 32 |
# Check if the API key is configured in HF Secrets
|
| 33 |
if not DEEPSEEK_API_KEY:
|
|
|
|
| 34 |
raise HTTPException(
|
| 35 |
status_code=500,
|
| 36 |
detail="DEEPSEEK_API_KEY is not set on the server."
|
|
@@ -39,13 +47,17 @@ async def proxy_deepseek(request: Request):
|
|
| 39 |
try:
|
| 40 |
# Get the original JSON body from the frontend
|
| 41 |
body = await request.json()
|
|
|
|
| 42 |
|
| 43 |
# Prepare the authorization headers for DeepSeek
|
|
|
|
|
|
|
| 44 |
headers = {
|
| 45 |
"Content-Type": "application/json",
|
| 46 |
-
"Authorization": f"Bearer {
|
| 47 |
}
|
| 48 |
|
|
|
|
| 49 |
# Make the asynchronous request to DeepSeek
|
| 50 |
response = await client.post(
|
| 51 |
DEEPSEEK_ENDPOINT,
|
|
@@ -57,19 +69,20 @@ async def proxy_deepseek(request: Request):
|
|
| 57 |
# Check if DeepSeek returned an error
|
| 58 |
response.raise_for_status()
|
| 59 |
|
|
|
|
| 60 |
# Return DeepSeek's successful response directly to the frontend
|
| 61 |
return response.json()
|
| 62 |
|
| 63 |
except httpx.HTTPStatusError as e:
|
| 64 |
# Handle errors from the DeepSeek API
|
| 65 |
-
|
| 66 |
raise HTTPException(
|
| 67 |
status_code=e.response.status_code,
|
| 68 |
-
detail=e.response.text
|
| 69 |
)
|
| 70 |
except Exception as e:
|
| 71 |
# Handle any other unexpected errors
|
| 72 |
-
|
| 73 |
raise HTTPException(
|
| 74 |
status_code=500,
|
| 75 |
detail=f"Internal server error: {str(e)}"
|
|
@@ -91,7 +104,8 @@ async def read_root():
|
|
| 91 |
with open(os.path.join(STATIC_DIR, "index.html")) as f:
|
| 92 |
return HTMLResponse(content=f.read(), status_code=200)
|
| 93 |
except FileNotFoundError:
|
|
|
|
| 94 |
return HTMLResponse(
|
| 95 |
content="<h1>Error: index.html not found</h1><p>Ensure index.html is in a 'static' folder.</p>",
|
| 96 |
status_code=404
|
| 97 |
-
)
|
|
|
|
| 3 |
from fastapi import FastAPI, Request, HTTPException
|
| 4 |
from fastapi.responses import HTMLResponse
|
| 5 |
from fastapi.staticfiles import StaticFiles
|
| 6 |
+
import logging
|
| 7 |
+
|
| 8 |
+
# --- Basic Logging Configuration ---
|
| 9 |
+
logging.basicConfig(level=logging.INFO)
|
| 10 |
+
logger = logging.getLogger(__name__)
|
| 11 |
|
| 12 |
# --- Configuration ---
|
| 13 |
# Get the API key from Hugging Face Secrets
|
|
|
|
| 34 |
This endpoint receives the request from our frontend (index.html),
|
| 35 |
adds the secret API key, and forwards it to the DeepSeek API.
|
| 36 |
"""
|
| 37 |
+
logger.info("Received request for /call-deepseek proxy.")
|
| 38 |
+
|
| 39 |
# Check if the API key is configured in HF Secrets
|
| 40 |
if not DEEPSEEK_API_KEY:
|
| 41 |
+
logger.error("DEEPSEEK_API_KEY is not set on the server.")
|
| 42 |
raise HTTPException(
|
| 43 |
status_code=500,
|
| 44 |
detail="DEEPSEEK_API_KEY is not set on the server."
|
|
|
|
| 47 |
try:
|
| 48 |
# Get the original JSON body from the frontend
|
| 49 |
body = await request.json()
|
| 50 |
+
logger.info(f"Request body model: {body.get('model')}")
|
| 51 |
|
| 52 |
# Prepare the authorization headers for DeepSeek
|
| 53 |
+
# Use .strip() to remove potential leading/trailing whitespace from the secret
|
| 54 |
+
stripped_api_key = DEEPSEEK_API_KEY.strip()
|
| 55 |
headers = {
|
| 56 |
"Content-Type": "application/json",
|
| 57 |
+
"Authorization": f"Bearer {stripped_api_key}"
|
| 58 |
}
|
| 59 |
|
| 60 |
+
logger.info("Forwarding request to DeepSeek API.")
|
| 61 |
# Make the asynchronous request to DeepSeek
|
| 62 |
response = await client.post(
|
| 63 |
DEEPSEEK_ENDPOINT,
|
|
|
|
| 69 |
# Check if DeepSeek returned an error
|
| 70 |
response.raise_for_status()
|
| 71 |
|
| 72 |
+
logger.info("Successfully received response from DeepSeek.")
|
| 73 |
# Return DeepSeek's successful response directly to the frontend
|
| 74 |
return response.json()
|
| 75 |
|
| 76 |
except httpx.HTTPStatusError as e:
|
| 77 |
# Handle errors from the DeepSeek API
|
| 78 |
+
logger.error(f"Error from DeepSeek API. Status: {e.response.status_code}, Response: {e.response.text}")
|
| 79 |
raise HTTPException(
|
| 80 |
status_code=e.response.status_code,
|
| 81 |
+
detail=f"Error from DeepSeek API: {e.response.text}"
|
| 82 |
)
|
| 83 |
except Exception as e:
|
| 84 |
# Handle any other unexpected errors
|
| 85 |
+
logger.error(f"An internal server error occurred: {str(e)}", exc_info=True)
|
| 86 |
raise HTTPException(
|
| 87 |
status_code=500,
|
| 88 |
detail=f"Internal server error: {str(e)}"
|
|
|
|
| 104 |
with open(os.path.join(STATIC_DIR, "index.html")) as f:
|
| 105 |
return HTMLResponse(content=f.read(), status_code=200)
|
| 106 |
except FileNotFoundError:
|
| 107 |
+
logger.error("index.html not found in static directory.")
|
| 108 |
return HTMLResponse(
|
| 109 |
content="<h1>Error: index.html not found</h1><p>Ensure index.html is in a 'static' folder.</p>",
|
| 110 |
status_code=404
|
| 111 |
+
)
|