Spaces:
Sleeping
Sleeping
File size: 2,928 Bytes
7814ee2 fa56145 7814ee2 fa56145 7814ee2 149c25a fa56145 7814ee2 149c25a 7814ee2 149c25a 7814ee2 149c25a 7814ee2 75b5466 7814ee2 75b5466 149c25a 137db14 7814ee2 75b5466 7814ee2 75b5466 7814ee2 75b5466 fa56145 75b5466 383458e b40961b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
import os
import uvicorn
from sound_generator import generate_sound, generate_music
from fastapi import FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.templating import Jinja2Templates
from fastapi.responses import FileResponse, HTMLResponse
from pydantic import BaseModel
# Create the FastAPI app with custom docs URL
app = FastAPI(
title="API de Sonidos Generativos",
description="API para generar sonidos y m煤sica basados en prompts",
version="1.0.0",
docs_url="/docs",
redoc_url="/redoc",
)
# Cargar las plantillas desde la carpeta "templates"
templates = Jinja2Templates(directory="templates")
# 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
@app.get("/health")
def health_check():
"""Endpoint para verificar que el servicio est谩 funcionando correctamente"""
return {"status": "ok", "service": "Sound Generation API"}
@app.post("/generate-sound/")
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))
@app.post("/generate-music/")
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))
@app.get("/", response_class=HTMLResponse)
def home(request: Request):
"""P谩gina de inicio con informaci贸n de la API"""
return templates.TemplateResponse("home.html", {"request": request})
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860)
|