pratik-250620 commited on
Commit
3353092
·
verified ·
1 Parent(s): 1812196

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. app.py +25 -3
  2. requirements.txt +1 -0
app.py CHANGED
@@ -1479,13 +1479,34 @@ def generate_image(prompt: str) -> dict:
1479
 
1480
 
1481
  def _elevenlabs_sfx(prompt: str, duration: float = 8.0) -> Optional[bytes]:
1482
- """Generate ambient sound effects via ElevenLabs Sound Generation API.
1483
 
1484
  Returns MP3 bytes or None on failure. Free tier: ~50 generations/month.
1485
  """
1486
  key = os.environ.get("ELEVENLABS_API_KEY", "")
1487
  if not key:
 
1488
  return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1489
  try:
1490
  resp = _requests.post(
1491
  ELEVENLABS_SFX_URL,
@@ -1501,10 +1522,11 @@ def _elevenlabs_sfx(prompt: str, duration: float = 8.0) -> Optional[bytes]:
1501
  timeout=90,
1502
  )
1503
  if resp.status_code == 200 and len(resp.content) > 1000:
 
1504
  return resp.content
1505
- logger.warning("ElevenLabs SFX returned %s: %s", resp.status_code, resp.text[:200])
1506
  except Exception as e:
1507
- logger.warning("ElevenLabs SFX failed: %s", e)
1508
  return None
1509
 
1510
 
 
1479
 
1480
 
1481
  def _elevenlabs_sfx(prompt: str, duration: float = 8.0) -> Optional[bytes]:
1482
+ """Generate ambient sound effects via ElevenLabs SDK.
1483
 
1484
  Returns MP3 bytes or None on failure. Free tier: ~50 generations/month.
1485
  """
1486
  key = os.environ.get("ELEVENLABS_API_KEY", "")
1487
  if not key:
1488
+ logger.info("No ELEVENLABS_API_KEY set — skipping ElevenLabs SFX")
1489
  return None
1490
+
1491
+ # --- Method 1: Official SDK (handles streaming + auth properly) ---
1492
+ try:
1493
+ from elevenlabs import ElevenLabs as ELClient
1494
+ client = ELClient(api_key=key)
1495
+ audio_iter = client.text_to_sound_effects.convert(
1496
+ text=prompt,
1497
+ duration_seconds=duration,
1498
+ prompt_influence=0.5,
1499
+ )
1500
+ # SDK returns a generator of bytes chunks
1501
+ audio_bytes = b"".join(audio_iter)
1502
+ if len(audio_bytes) > 1000:
1503
+ logger.info("ElevenLabs SFX generated %d bytes via SDK", len(audio_bytes))
1504
+ return audio_bytes
1505
+ logger.warning("ElevenLabs SFX SDK returned only %d bytes", len(audio_bytes))
1506
+ except Exception as e:
1507
+ logger.warning("ElevenLabs SFX SDK failed: %s", e)
1508
+
1509
+ # --- Method 2: Raw REST fallback ---
1510
  try:
1511
  resp = _requests.post(
1512
  ELEVENLABS_SFX_URL,
 
1522
  timeout=90,
1523
  )
1524
  if resp.status_code == 200 and len(resp.content) > 1000:
1525
+ logger.info("ElevenLabs SFX generated %d bytes via REST", len(resp.content))
1526
  return resp.content
1527
+ logger.warning("ElevenLabs SFX REST returned %s: %s", resp.status_code, resp.text[:300])
1528
  except Exception as e:
1529
+ logger.warning("ElevenLabs SFX REST failed: %s", e)
1530
  return None
1531
 
1532
 
requirements.txt CHANGED
@@ -13,3 +13,4 @@ pillow>=10.0.0
13
  pydantic>=2.0.0
14
  pydantic-settings>=2.0.0
15
  requests>=2.28.0
 
 
13
  pydantic>=2.0.0
14
  pydantic-settings>=2.0.0
15
  requests>=2.28.0
16
+ elevenlabs>=1.0.0