Spaces:
Running
Running
Curinha
commited on
Commit
·
525c0ee
1
Parent(s):
f40d56f
Add Gradio interface and update root endpoint for sound generation API
Browse files
app.py
CHANGED
@@ -5,7 +5,7 @@ import uvicorn
|
|
5 |
from sound_generator import generate_sound, generate_music
|
6 |
from fastapi import FastAPI, HTTPException
|
7 |
from fastapi.middleware.cors import CORSMiddleware
|
8 |
-
from fastapi.responses import FileResponse,
|
9 |
from pydantic import BaseModel
|
10 |
|
11 |
# Create the FastAPI app with custom docs URL
|
@@ -69,23 +69,44 @@ async def generate_music_endpoint(request: AudioRequest):
|
|
69 |
except Exception as e:
|
70 |
raise HTTPException(status_code=500, detail=str(e))
|
71 |
|
72 |
-
# Interfaz de Gradio
|
73 |
-
demo = gr.Blocks()
|
74 |
-
|
75 |
-
with demo:
|
76 |
-
gr.Markdown("# API de Sonidos Generativos")
|
77 |
-
gr.Markdown("Bienvenido al servicio de generación de sonidos mediante IA.")
|
78 |
-
gr.Markdown("Esta API está disponible para uso a través de los siguientes endpoints:")
|
79 |
-
gr.Markdown("- Generar sonido: `/generate-sound/`")
|
80 |
-
gr.Markdown("- Generar música: `/generate-music/`")
|
81 |
-
gr.Markdown("- Documentación de la API: `/docs`")
|
82 |
-
|
83 |
-
# Montar la app de Gradio en FastAPI
|
84 |
-
app = gr.mount_gradio_app(app, demo, path="/")
|
85 |
-
|
86 |
-
# Para confirmar que el código está funcionando, podemos agregar un print
|
87 |
-
print("✅ Servidor inicializado. Gradio montado en la ruta principal '/'")
|
88 |
-
print("📄 Documentación disponible en '/docs'")
|
89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
if __name__ == "__main__":
|
91 |
-
uvicorn.run(app, host="0.0.0.0", port=
|
|
|
5 |
from sound_generator import generate_sound, generate_music
|
6 |
from fastapi import FastAPI, HTTPException
|
7 |
from fastapi.middleware.cors import CORSMiddleware
|
8 |
+
from fastapi.responses import FileResponse, RedirectResponse
|
9 |
from pydantic import BaseModel
|
10 |
|
11 |
# Create the FastAPI app with custom docs URL
|
|
|
69 |
except Exception as e:
|
70 |
raise HTTPException(status_code=500, detail=str(e))
|
71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
+
@app.get("/")
|
74 |
+
def root():
|
75 |
+
return RedirectResponse(url="/gradio")
|
76 |
+
|
77 |
+
|
78 |
+
# Interfaz con Gradio (solo documentación)
|
79 |
+
def gradio_ui():
|
80 |
+
description = """
|
81 |
+
# API de Generación de Texto
|
82 |
+
Esta API utiliza `GPT-2` para generar texto basado en un prompt.
|
83 |
+
|
84 |
+
## Uso:
|
85 |
+
1. Visita [la documentación interactiva](https://<TU-HF-SPACE>.hf.space/docs) para probar la API.
|
86 |
+
2. Endpoint principal: `GET /generate?prompt=TuTexto`
|
87 |
+
3. Parámetros:
|
88 |
+
- `prompt` (str): Texto inicial.
|
89 |
+
- `max_length` (int, opcional): Longitud máxima del texto generado.
|
90 |
+
|
91 |
+
📌 **Ejemplo de llamada en Python:**
|
92 |
+
```python
|
93 |
+
import requests
|
94 |
+
url = "https://<TU-HF-SPACE>.hf.space/generate?prompt=Hola"
|
95 |
+
response = requests.get(url)
|
96 |
+
print(response.json())
|
97 |
+
```
|
98 |
+
"""
|
99 |
+
return gr.Markdown(description)
|
100 |
+
|
101 |
+
gr_app = gr.Blocks()
|
102 |
+
with gr_app:
|
103 |
+
gradio_ui()
|
104 |
+
|
105 |
+
@app.get("/gradio")
|
106 |
+
def gradio_launch():
|
107 |
+
"""Lanza la interfaz de Gradio"""
|
108 |
+
return gr_app.launch(server_name="0.0.0.0", server_port=7860, inline=False, share=False)
|
109 |
+
|
110 |
+
# Para ejecutar en local
|
111 |
if __name__ == "__main__":
|
112 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|