Elimina FastAPI y deja solo Gradio para compatibilidad con Spaces y @gradio/client
Browse files
app.py
CHANGED
@@ -582,80 +582,6 @@ with gr.Blocks(title="Modelos Libres de IA", theme=gr.themes.Soft()) as demo:
|
|
582 |
)
|
583 |
|
584 |
# Configuraci贸n para Hugging Face Spaces
|
|
|
585 |
if __name__ == "__main__":
|
586 |
-
|
587 |
-
app = FastAPI(title="NTIA Space API")
|
588 |
-
|
589 |
-
# Configurar CORS
|
590 |
-
app.add_middleware(
|
591 |
-
CORSMiddleware,
|
592 |
-
allow_origins=["*"],
|
593 |
-
allow_credentials=True,
|
594 |
-
allow_methods=["*"],
|
595 |
-
allow_headers=["*"],
|
596 |
-
)
|
597 |
-
|
598 |
-
# Endpoints API
|
599 |
-
@app.get("/models")
|
600 |
-
async def api_models():
|
601 |
-
try:
|
602 |
-
return {
|
603 |
-
"text": [{"id": k, "label": v} for k, v in MODELS["text"].items()],
|
604 |
-
"image": [{"id": k, "label": v} for k, v in MODELS["image"].items()],
|
605 |
-
"video": [{"id": k, "label": v} for k, v in MODELS["video"].items()],
|
606 |
-
"chat": [{"id": k, "label": v} for k, v in MODELS["chat"].items()]
|
607 |
-
}
|
608 |
-
except Exception as e:
|
609 |
-
raise HTTPException(status_code=500, detail=str(e))
|
610 |
-
|
611 |
-
@app.post("/generate_text")
|
612 |
-
async def api_generate_text(request: TextRequest):
|
613 |
-
try:
|
614 |
-
result = generate_text(request.prompt, request.model_name, request.max_length)
|
615 |
-
return {"response": result}
|
616 |
-
except Exception as e:
|
617 |
-
raise HTTPException(status_code=500, detail=str(e))
|
618 |
-
|
619 |
-
@app.post("/generate_image")
|
620 |
-
async def api_generate_image(request: ImageRequest):
|
621 |
-
try:
|
622 |
-
result = generate_image(request.prompt, request.model_name, request.num_inference_steps)
|
623 |
-
|
624 |
-
if isinstance(result, str) and result.startswith("Error"):
|
625 |
-
raise HTTPException(status_code=500, detail=result)
|
626 |
-
|
627 |
-
# Convertir PIL Image a base64
|
628 |
-
buffer = BytesIO()
|
629 |
-
result.save(buffer, format="PNG")
|
630 |
-
img_str = base64.b64encode(buffer.getvalue()).decode()
|
631 |
-
|
632 |
-
return {"image": f"data:image/png;base64,{img_str}"}
|
633 |
-
except Exception as e:
|
634 |
-
raise HTTPException(status_code=500, detail=str(e))
|
635 |
-
|
636 |
-
@app.post("/generate_video")
|
637 |
-
async def api_generate_video(request: VideoRequest):
|
638 |
-
try:
|
639 |
-
result = generate_video(request.prompt, request.model_name, request.num_frames, request.num_inference_steps)
|
640 |
-
|
641 |
-
if isinstance(result, str) and result.startswith("Error"):
|
642 |
-
raise HTTPException(status_code=500, detail=result)
|
643 |
-
|
644 |
-
return {"video": "video_data_placeholder", "frames": len(result) if isinstance(result, list) else 0}
|
645 |
-
except Exception as e:
|
646 |
-
raise HTTPException(status_code=500, detail=str(e))
|
647 |
-
|
648 |
-
@app.post("/chat")
|
649 |
-
async def api_chat(request: ChatRequest):
|
650 |
-
try:
|
651 |
-
result = chat_with_model(request.message, request.history, request.model_name)
|
652 |
-
return {"response": result[-1]["content"] if result else "No response"}
|
653 |
-
except Exception as e:
|
654 |
-
raise HTTPException(status_code=500, detail=str(e))
|
655 |
-
|
656 |
-
# Montar Gradio en FastAPI
|
657 |
-
app = gr.mount_gradio_app(app, demo, path="/")
|
658 |
-
|
659 |
-
# Lanzar con uvicorn
|
660 |
-
import uvicorn
|
661 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
582 |
)
|
583 |
|
584 |
# Configuraci贸n para Hugging Face Spaces
|
585 |
+
# Elimina FastAPI y usa solo Gradio
|
586 |
if __name__ == "__main__":
|
587 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|