Spaces:
Running
Running
import os | |
import gradio as gr | |
import uvicorn | |
from sound_generator import generate_sound, generate_music | |
from fastapi import FastAPI, HTTPException | |
from fastapi.middleware.cors import CORSMiddleware | |
from fastapi.responses import FileResponse, RedirectResponse, HTMLResponse | |
from pydantic import BaseModel | |
# Create the FastAPI app with custom docs URL | |
app = FastAPI(docs_url="/api/docs") | |
# Configuraci贸n de CORS | |
app.add_middleware( | |
CORSMiddleware, | |
allow_origins=["*"], | |
allow_credentials=True, | |
allow_methods=["*"], | |
allow_headers=["*"], | |
) | |
# Define a Pydantic model to handle the input prompt | |
class AudioRequest(BaseModel): | |
prompt: str | |
# Prueba para verificar si la API funciona - la dejamos por ahora para debugging | |
def status(): | |
return {"status": "API running", "version": "1.0"} | |
async def generate_sound_endpoint(request: AudioRequest): | |
try: | |
# Llamada a la funci贸n para generar el sonido | |
audio_file_path = generate_sound(request.prompt) | |
# Verifica si el archivo se ha generado correctamente | |
if not os.path.exists(audio_file_path): | |
raise HTTPException( | |
status_code=404, detail="Archivo de audio no encontrado." | |
) | |
# Regresar el archivo generado como una respuesta de descarga | |
return FileResponse( | |
audio_file_path, media_type="audio/wav", filename="generated_audio.wav" | |
) | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=str(e)) | |
async def generate_music_endpoint(request: AudioRequest): | |
try: | |
# Call the synchronous generate_music function | |
audio_file_path = generate_music(request.prompt) | |
# Verifies if the file has been generated correctly | |
if not os.path.exists(audio_file_path): | |
raise HTTPException( | |
status_code=404, detail="Archivo de audio no encontrado." | |
) | |
# Return the generated file as a download response | |
return FileResponse( | |
audio_file_path, media_type="audio/wav", filename="generated_audio.wav" | |
) | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=str(e)) | |
def home(): | |
"""P谩gina de inicio con informaci贸n de la API y estilo inspirado en Gradio.""" | |
return """ | |
<html> | |
<head> | |
<title>API de Sonidos Generativos</title> | |
<style> | |
body { | |
font-family: Arial, sans-serif; | |
background-color: #0B0F19; | |
color: white; | |
text-align: center; | |
padding: 50px; | |
} | |
h1 { | |
color: #FFFFFF; | |
} | |
a { | |
color: #FFCC00; | |
text-decoration: none; | |
font-size: 18px; | |
} | |
.container { | |
background: #1B1E29; | |
padding: 20px; | |
border-radius: 10px; | |
display: inline-block; | |
margin-top: 20px; | |
} | |
.button { | |
background-color: #FFCC00; | |
color: black; | |
padding: 10px 20px; | |
margin-top: 15px; | |
display: inline-block; | |
border-radius: 5px; | |
} | |
.button:hover { | |
background-color: #E6B800; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>API de Sonidos Generativos</h1> | |
<p>Esta API ofrece tres endpoints:</p> | |
<div class="container"> | |
<p>馃搫 <a href="/docs">/docs</a> - Documentaci贸n interactiva</p> | |
<p>馃幍 <a href="/generate-music">/generate-music</a> - Genera m煤sica basada en un prompt</p> | |
<p>馃攰 <a href="/generate-sound">/generate-sound</a> - Genera sonidos a partir de una descripci贸n</p> | |
<a class="button" href="https://paginaexterna.com/test-api" target="_blank">馃敆 Probar en nuestra web</a> | |
</div> | |
</body> | |
</html> | |
""" | |
if __name__ == "__main__": | |
uvicorn.run(app, host="0.0.0.0", port=7860) | |