Spaces:
Running
Running
| import os | |
| import httpx | |
| from fastapi import FastAPI, Request | |
| from fastapi.responses import JSONResponse | |
| from fastapi.templating import Jinja2Templates | |
| app = FastAPI() | |
| templates = Jinja2Templates(directory=".") | |
| GEMINI_API_KEY = os.getenv("GEMINI_API_KEY") | |
| FIREBASE_CONFIG = os.getenv("FIREBASE_CONFIG") | |
| async def get_index(request: Request): | |
| if not FIREBASE_CONFIG: | |
| return JSONResponse( | |
| content={"error": "El secreto 'FIREBASE_CONFIG' no está configurado en Hugging Face."}, | |
| status_code=500 | |
| ) | |
| return templates.TemplateResponse( | |
| "index.html", | |
| {"request": request, "firebase_config": FIREBASE_CONFIG} | |
| ) | |
| async def gemini_proxy(request: Request): | |
| if not GEMINI_API_KEY: | |
| return JSONResponse( | |
| content={"error": "El secreto 'GEMINI_API_KEY' no está configurado en Hugging Face."}, | |
| status_code=500 | |
| ) | |
| try: | |
| payload = await request.json() | |
| api_url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-09-2025:generateContent?key={GEMINI_API_KEY}" | |
| async with httpx.AsyncClient() as client: | |
| response = await client.post( | |
| api_url, | |
| json=payload, | |
| timeout=60.0 | |
| ) | |
| return JSONResponse(content=response.json(), status_code=response.status_code) | |
| except Exception as e: | |
| print(f"Error en el proxy: {e}") | |
| return JSONResponse(content={"error": str(e)}, status_code=500) | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run(app, host="0.0.0.0", port=7860) |