Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request, Response, HTTPException
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
import httpx
|
| 4 |
+
import uvicorn
|
| 5 |
+
from typing import Dict, Any, Optional
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
app = FastAPI(title="LLM7.io API Proxy")
|
| 9 |
+
|
| 10 |
+
# Configure CORS for all origins
|
| 11 |
+
app.add_middleware(
|
| 12 |
+
CORSMiddleware,
|
| 13 |
+
allow_origins=["*"],
|
| 14 |
+
allow_credentials=True,
|
| 15 |
+
allow_methods=["*"],
|
| 16 |
+
allow_headers=["*"],
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# Target API URL
|
| 20 |
+
TARGET_API_URL = "https://api.llm7.io"
|
| 21 |
+
|
| 22 |
+
@app.get("/")
|
| 23 |
+
async def root():
|
| 24 |
+
return {
|
| 25 |
+
"status": "online",
|
| 26 |
+
"message": "LLM7.io API Proxy is running. Send requests to /v1/... endpoints.",
|
| 27 |
+
"usage": "This proxy forwards requests to api.llm7.io. No API key is required."
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH"])
|
| 31 |
+
async def proxy(path: str, request: Request):
|
| 32 |
+
"""
|
| 33 |
+
Proxy all requests to the LLM7.io API.
|
| 34 |
+
This handles any path and HTTP method, forwarding the request to the target API.
|
| 35 |
+
"""
|
| 36 |
+
# Construct target URL
|
| 37 |
+
target_url = f"{TARGET_API_URL}/{path}"
|
| 38 |
+
|
| 39 |
+
# Get request body if present
|
| 40 |
+
body = None
|
| 41 |
+
if request.method in ["POST", "PUT", "PATCH"]:
|
| 42 |
+
try:
|
| 43 |
+
body = await request.json()
|
| 44 |
+
except Exception:
|
| 45 |
+
try:
|
| 46 |
+
body = await request.body()
|
| 47 |
+
except Exception:
|
| 48 |
+
body = None
|
| 49 |
+
|
| 50 |
+
# Get request headers, excluding host
|
| 51 |
+
headers = dict(request.headers)
|
| 52 |
+
headers.pop("host", None)
|
| 53 |
+
|
| 54 |
+
# Create httpx client with timeout
|
| 55 |
+
async with httpx.AsyncClient(timeout=120.0) as client:
|
| 56 |
+
try:
|
| 57 |
+
# Forward the request to the target API
|
| 58 |
+
response = await client.request(
|
| 59 |
+
method=request.method,
|
| 60 |
+
url=target_url,
|
| 61 |
+
headers=headers,
|
| 62 |
+
json=body if isinstance(body, dict) else None,
|
| 63 |
+
content=body if not isinstance(body, dict) and body is not None else None,
|
| 64 |
+
params=request.query_params,
|
| 65 |
+
follow_redirects=True
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
# Create response with same status code, headers, and content
|
| 69 |
+
return Response(
|
| 70 |
+
content=response.content,
|
| 71 |
+
status_code=response.status_code,
|
| 72 |
+
headers=dict(response.headers),
|
| 73 |
+
)
|
| 74 |
+
except httpx.RequestError as exc:
|
| 75 |
+
raise HTTPException(status_code=503, detail=f"Error forwarding request: {str(exc)}")
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
# Get port from environment variable or use default
|
| 79 |
+
port = int(os.environ.get("PORT", 7860))
|
| 80 |
+
|
| 81 |
+
# Run the server
|
| 82 |
+
uvicorn.run(app, host="0.0.0.0", port=port)
|