Download main.py from emee/proxy: direct link, hf CLI and curl.
- Browser
- Download file 1.44 kB
-
https://huggingface.co/spaces/emee/proxy/resolve/main/main.py
- Command line
-
hf download hf://spaces/emee/proxy/main.py
-
curl -L -o main.py https://huggingface.co/spaces/emee/proxy/resolve/main/main.py
1.44 kB
| from fastapi.responses import StreamingResponse, JSONResponse | |
| from fastapi import FastAPI, Response, Request, Query | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from dotenv import load_dotenv | |
| import requests | |
| import os | |
| load_dotenv() | |
| app = FastAPI() | |
| # uvicorn main:app --reload | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| async def redirect(request:Request, path: str): | |
| try: | |
| base_url = os.environ.get("BASE_URL") | |
| query_params = request.query_params | |
| provider = query_params.get("provider") | |
| base_url = base_url.replace("<provider>", provider) | |
| full_url = f"{base_url}/{path}" | |
| if query_params: | |
| full_url += "?" + "&".join([f"{key}={value}" for key, value in query_params.items()]) | |
| auth_token = os.environ.get("API_KEY") | |
| headers = { "Authorization": f"Bearer {auth_token}" } | |
| response = requests.get(full_url, stream=True, headers=headers) | |
| if response.status_code == 404: | |
| return JSONResponse({ "msg": "bro, i think you provider is wrong" }) | |
| def stream_content(): | |
| for chunk in response.iter_content(chunk_size=1024): | |
| yield chunk | |
| content_type = response.headers.get('content-type', 'application/octet-stream') | |
| return StreamingResponse(stream_content(), media_type=content_type) | |
| except Exception as err: | |
| print(f"{err} [ERROR]") | |
| return JSONResponse({ "msg": "try hard bro" }) | |