Tu Nombre commited on
Commit
d53c2e5
·
1 Parent(s): a574ff7

musica: usar gradio_client

Browse files
Files changed (1) hide show
  1. app.py +16 -40
app.py CHANGED
@@ -507,7 +507,6 @@ async def ep_musica(file: UploadFile = File(...)):
507
  @app.post("/generar-musica-ia")
508
  async def ep_musica_ia(request: Request):
509
  try:
510
- import requests as _req, json as _json
511
  d = await request.json()
512
  nicho = d.get("nicho", "espiritual")
513
  duracion = int(d.get("duracion", 20))
@@ -522,45 +521,22 @@ async def ep_musica_ia(request: Request):
522
  "naturaleza": "peaceful nature sounds, light acoustic guitar, calm",
523
  "default": "cinematic background music, neutral mood, no vocals",
524
  }
525
- prompt = next((v for k,v in PROMPTS.items() if k in nicho.lower()), PROMPTS["default"])
526
- endpoint = f"{url_fabrica}/gradio_api/call/generar_musica"
527
- r1 = _req.post(endpoint, json={"data": [prompt, duracion]}, timeout=60)
528
- r1.raise_for_status()
529
- event_id = r1.json().get("event_id")
530
- if not event_id:
531
- return JSONResponse({"error": "Sin event_id de la Fabrica"}, status_code=500)
532
- stream_url = f"{url_fabrica}/gradio_api/call/generar_musica/{event_id}"
533
- with _req.get(stream_url, stream=True, timeout=300) as stream:
534
- for line in stream.iter_lines():
535
- if not line: continue
536
- decoded = line.decode("utf-8")
537
- log(f"[Musica][raw] {decoded[:180]}")
538
- if not decoded.startswith("data:"): continue
539
- try:
540
- data = _json.loads(decoded[5:].strip())
541
- except Exception:
542
- continue
543
- if not (isinstance(data, list) and data): continue
544
- audio_url = data[0]
545
- if audio_url is None: continue
546
- if isinstance(audio_url, dict):
547
- audio_url = audio_url.get("url") or audio_url.get("path","") or ""
548
- if not isinstance(audio_url, str) or not audio_url:
549
- continue
550
- if not audio_url.startswith("http"):
551
- if "/gradio_api/file=" in audio_url:
552
- audio_url = url_fabrica + audio_url
553
- else:
554
- audio_url = url_fabrica + "/gradio_api/file=" + audio_url
555
- audio_resp = _req.get(audio_url, timeout=60)
556
- audio_resp.raise_for_status()
557
- with open("musica.mp3","wb") as f:
558
- f.write(audio_resp.content)
559
- ESTADO["musica"] = True
560
- log(f"[Musica] OK -> {audio_url[:90]}")
561
- return {"ok": True}
562
- log("[Musica] La Fabrica no devolvio audio")
563
- return JSONResponse({"error": "La Fabrica no devolvio audio"}, status_code=500)
564
  except Exception as e:
565
  log(f"[Musica] ERROR: {e}")
566
  return JSONResponse({"error": str(e)[:200]}, status_code=500)
 
507
  @app.post("/generar-musica-ia")
508
  async def ep_musica_ia(request: Request):
509
  try:
 
510
  d = await request.json()
511
  nicho = d.get("nicho", "espiritual")
512
  duracion = int(d.get("duracion", 20))
 
521
  "naturaleza": "peaceful nature sounds, light acoustic guitar, calm",
522
  "default": "cinematic background music, neutral mood, no vocals",
523
  }
524
+ prompt = next((v for k, v in PROMPTS.items() if k in nicho.lower()), PROMPTS["default"])
525
+ from gradio_client import Client
526
+ client = Client(url_fabrica)
527
+ ruta = client.predict(prompt, duracion, api_name="/generar_musica")
528
+ if isinstance(ruta, (list, tuple)) and ruta:
529
+ ruta = ruta[0]
530
+ if isinstance(ruta, dict):
531
+ ruta = ruta.get("path") or ruta.get("name") or ""
532
+ if not ruta or not os.path.exists(ruta):
533
+ log(f"[Musica] sin fichero valido: {ruta}")
534
+ return JSONResponse({"error": "La Fabrica no devolvio audio"}, status_code=500)
535
+ with open(ruta, "rb") as fi, open("musica.mp3", "wb") as fo:
536
+ fo.write(fi.read())
537
+ ESTADO["musica"] = True
538
+ log(f"[Musica] OK -> {ruta}")
539
+ return {"ok": True}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
540
  except Exception as e:
541
  log(f"[Musica] ERROR: {e}")
542
  return JSONResponse({"error": str(e)[:200]}, status_code=500)