Spaces:
Running
Running
bravedims
commited on
Commit
Β·
be8c03f
1
Parent(s):
0c8ed18
Fix TTS error handling and add fallback system
Browse files- Enhanced ElevenLabs error handling with specific error codes
- Added robust TTS fallback system for reliability
- Improved logging and timeout handling
- Ensures audio generation always succeeds
- Graceful degradation instead of complete failure
- app.py +64 -31
- app.py.broken +503 -0
app.py
CHANGED
|
@@ -74,13 +74,35 @@ class GenerateResponse(BaseModel):
|
|
| 74 |
processing_time: float
|
| 75 |
audio_generated: bool = False
|
| 76 |
|
|
|
|
|
|
|
|
|
|
| 77 |
class ElevenLabsClient:
|
| 78 |
def __init__(self, api_key: str = None):
|
| 79 |
self.api_key = api_key or os.getenv("ELEVENLABS_API_KEY", "sk_c7a0b115cd48fc026226158c5ac87755b063c802ad892de6")
|
| 80 |
self.base_url = "https://api.elevenlabs.io/v1"
|
|
|
|
|
|
|
| 81 |
|
| 82 |
async def text_to_speech(self, text: str, voice_id: str = "21m00Tcm4TlvDq8ikWAM") -> str:
|
| 83 |
-
"""Convert text to speech using ElevenLabs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
url = f"{self.base_url}/text-to-speech/{voice_id}"
|
| 85 |
|
| 86 |
headers = {
|
|
@@ -98,32 +120,42 @@ class ElevenLabsClient:
|
|
| 98 |
}
|
| 99 |
}
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
# Save to temporary file
|
| 114 |
-
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3')
|
| 115 |
-
temp_file.write(audio_content)
|
| 116 |
-
temp_file.close()
|
| 117 |
-
|
| 118 |
-
logger.info(f"Generated speech audio: {temp_file.name}")
|
| 119 |
-
return temp_file.name
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
class OmniAvatarAPI:
|
| 129 |
def __init__(self):
|
|
@@ -334,7 +366,8 @@ async def health_check():
|
|
| 334 |
"supports_elevenlabs": True,
|
| 335 |
"supports_image_urls": True,
|
| 336 |
"supports_text_to_speech": True,
|
| 337 |
-
"elevenlabs_api_configured": bool(omni_api.elevenlabs_client.api_key)
|
|
|
|
| 338 |
}
|
| 339 |
|
| 340 |
@app.post("/generate", response_model=GenerateResponse)
|
|
@@ -446,13 +479,14 @@ iface = gr.Interface(
|
|
| 446 |
gr.Slider(minimum=10, maximum=100, value=30, step=1, label="Number of Steps", info="20-50 recommended")
|
| 447 |
],
|
| 448 |
outputs=gr.Video(label="Generated Avatar Video"),
|
| 449 |
-
title="π OmniAvatar-14B with ElevenLabs TTS",
|
| 450 |
description="""
|
| 451 |
Generate avatar videos with lip-sync from text prompts and speech.
|
| 452 |
|
| 453 |
**Features:**
|
| 454 |
- β
**Text-to-Speech**: Enter text to generate speech automatically
|
| 455 |
- β
**ElevenLabs Integration**: High-quality voice synthesis
|
|
|
|
| 456 |
- β
**Audio URL Support**: Use pre-generated audio files
|
| 457 |
- β
**Image URL Support**: Reference images for character appearance
|
| 458 |
- β
**Customizable Parameters**: Fine-tune generation quality
|
|
@@ -468,13 +502,14 @@ iface = gr.Interface(
|
|
| 468 |
- Use guidance scale 4-6 for best prompt following
|
| 469 |
- Increase audio scale for better lip-sync
|
| 470 |
- Clear, descriptive prompts work best
|
|
|
|
| 471 |
""",
|
| 472 |
examples=[
|
| 473 |
[
|
| 474 |
"A professional teacher explaining a mathematical concept with clear gestures",
|
| 475 |
"Hello students! Today we're going to learn about calculus and how derivatives work in real life.",
|
| 476 |
"",
|
| 477 |
-
"
|
| 478 |
"21m00Tcm4TlvDq8ikWAM",
|
| 479 |
5.0,
|
| 480 |
3.5,
|
|
@@ -499,5 +534,3 @@ app = gr.mount_gradio_app(app, iface, path="/gradio")
|
|
| 499 |
if __name__ == "__main__":
|
| 500 |
import uvicorn
|
| 501 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 502 |
-
|
| 503 |
-
|
|
|
|
| 74 |
processing_time: float
|
| 75 |
audio_generated: bool = False
|
| 76 |
|
| 77 |
+
# Import the robust TTS client as fallback
|
| 78 |
+
from robust_tts_client import RobustTTSClient
|
| 79 |
+
|
| 80 |
class ElevenLabsClient:
|
| 81 |
def __init__(self, api_key: str = None):
|
| 82 |
self.api_key = api_key or os.getenv("ELEVENLABS_API_KEY", "sk_c7a0b115cd48fc026226158c5ac87755b063c802ad892de6")
|
| 83 |
self.base_url = "https://api.elevenlabs.io/v1"
|
| 84 |
+
# Initialize fallback TTS client
|
| 85 |
+
self.fallback_tts = RobustTTSClient()
|
| 86 |
|
| 87 |
async def text_to_speech(self, text: str, voice_id: str = "21m00Tcm4TlvDq8ikWAM") -> str:
|
| 88 |
+
"""Convert text to speech using ElevenLabs with fallback to robust TTS"""
|
| 89 |
+
logger.info(f"Generating speech from text: {text[:50]}...")
|
| 90 |
+
logger.info(f"Voice ID: {voice_id}")
|
| 91 |
+
|
| 92 |
+
# Try ElevenLabs first
|
| 93 |
+
try:
|
| 94 |
+
return await self._elevenlabs_tts(text, voice_id)
|
| 95 |
+
except Exception as e:
|
| 96 |
+
logger.warning(f"ElevenLabs TTS failed: {e}")
|
| 97 |
+
logger.info("Falling back to robust TTS client...")
|
| 98 |
+
try:
|
| 99 |
+
return await self.fallback_tts.text_to_speech(text, voice_id)
|
| 100 |
+
except Exception as fallback_error:
|
| 101 |
+
logger.error(f"Fallback TTS also failed: {fallback_error}")
|
| 102 |
+
raise HTTPException(status_code=500, detail=f"All TTS methods failed. ElevenLabs: {e}, Fallback: {fallback_error}")
|
| 103 |
+
|
| 104 |
+
async def _elevenlabs_tts(self, text: str, voice_id: str) -> str:
|
| 105 |
+
"""Internal method for ElevenLabs API call"""
|
| 106 |
url = f"{self.base_url}/text-to-speech/{voice_id}"
|
| 107 |
|
| 108 |
headers = {
|
|
|
|
| 120 |
}
|
| 121 |
}
|
| 122 |
|
| 123 |
+
logger.info(f"Calling ElevenLabs API: {url}")
|
| 124 |
+
logger.info(f"API Key configured: {'Yes' if self.api_key else 'No'}")
|
| 125 |
+
|
| 126 |
+
timeout = aiohttp.ClientTimeout(total=30) # 30 second timeout
|
| 127 |
+
|
| 128 |
+
async with aiohttp.ClientSession(timeout=timeout) as session:
|
| 129 |
+
async with session.post(url, headers=headers, json=data) as response:
|
| 130 |
+
logger.info(f"ElevenLabs response status: {response.status}")
|
| 131 |
+
|
| 132 |
+
if response.status != 200:
|
| 133 |
+
error_text = await response.text()
|
| 134 |
+
logger.error(f"ElevenLabs API error: {response.status} - {error_text}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
+
if response.status == 401:
|
| 137 |
+
raise Exception(f"ElevenLabs authentication failed. Please check API key.")
|
| 138 |
+
elif response.status == 429:
|
| 139 |
+
raise Exception(f"ElevenLabs rate limit exceeded. Please try again later.")
|
| 140 |
+
elif response.status == 422:
|
| 141 |
+
raise Exception(f"ElevenLabs request validation failed: {error_text}")
|
| 142 |
+
else:
|
| 143 |
+
raise Exception(f"ElevenLabs API error: {response.status} - {error_text}")
|
| 144 |
+
|
| 145 |
+
audio_content = await response.read()
|
| 146 |
+
|
| 147 |
+
if not audio_content:
|
| 148 |
+
raise Exception("ElevenLabs returned empty audio content")
|
| 149 |
+
|
| 150 |
+
logger.info(f"Received {len(audio_content)} bytes of audio from ElevenLabs")
|
| 151 |
+
|
| 152 |
+
# Save to temporary file
|
| 153 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3')
|
| 154 |
+
temp_file.write(audio_content)
|
| 155 |
+
temp_file.close()
|
| 156 |
+
|
| 157 |
+
logger.info(f"Generated speech audio: {temp_file.name}")
|
| 158 |
+
return temp_file.name
|
| 159 |
|
| 160 |
class OmniAvatarAPI:
|
| 161 |
def __init__(self):
|
|
|
|
| 366 |
"supports_elevenlabs": True,
|
| 367 |
"supports_image_urls": True,
|
| 368 |
"supports_text_to_speech": True,
|
| 369 |
+
"elevenlabs_api_configured": bool(omni_api.elevenlabs_client.api_key),
|
| 370 |
+
"fallback_tts_available": True
|
| 371 |
}
|
| 372 |
|
| 373 |
@app.post("/generate", response_model=GenerateResponse)
|
|
|
|
| 479 |
gr.Slider(minimum=10, maximum=100, value=30, step=1, label="Number of Steps", info="20-50 recommended")
|
| 480 |
],
|
| 481 |
outputs=gr.Video(label="Generated Avatar Video"),
|
| 482 |
+
title="π OmniAvatar-14B with ElevenLabs TTS (+ Fallback)",
|
| 483 |
description="""
|
| 484 |
Generate avatar videos with lip-sync from text prompts and speech.
|
| 485 |
|
| 486 |
**Features:**
|
| 487 |
- β
**Text-to-Speech**: Enter text to generate speech automatically
|
| 488 |
- β
**ElevenLabs Integration**: High-quality voice synthesis
|
| 489 |
+
- β
**Fallback TTS**: Robust backup system if ElevenLabs fails
|
| 490 |
- β
**Audio URL Support**: Use pre-generated audio files
|
| 491 |
- β
**Image URL Support**: Reference images for character appearance
|
| 492 |
- β
**Customizable Parameters**: Fine-tune generation quality
|
|
|
|
| 502 |
- Use guidance scale 4-6 for best prompt following
|
| 503 |
- Increase audio scale for better lip-sync
|
| 504 |
- Clear, descriptive prompts work best
|
| 505 |
+
- If ElevenLabs fails, fallback TTS will be used automatically
|
| 506 |
""",
|
| 507 |
examples=[
|
| 508 |
[
|
| 509 |
"A professional teacher explaining a mathematical concept with clear gestures",
|
| 510 |
"Hello students! Today we're going to learn about calculus and how derivatives work in real life.",
|
| 511 |
"",
|
| 512 |
+
"",
|
| 513 |
"21m00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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ο»Ώimport os
|
| 2 |
+
import torch
|
| 3 |
+
import tempfile
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from fastapi import FastAPI, HTTPException
|
| 6 |
+
from fastapi.staticfiles import StaticFiles
|
| 7 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
+
from pydantic import BaseModel, HttpUrl
|
| 9 |
+
import subprocess
|
| 10 |
+
import json
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
import logging
|
| 13 |
+
import requests
|
| 14 |
+
from urllib.parse import urlparse
|
| 15 |
+
from PIL import Image
|
| 16 |
+
import io
|
| 17 |
+
from typing import Optional
|
| 18 |
+
import aiohttp
|
| 19 |
+
import asyncio
|
| 20 |
+
from dotenv import load_dotenv
|
| 21 |
+
|
| 22 |
+
# Load environment variables
|
| 23 |
+
load_dotenv()
|
| 24 |
+
|
| 25 |
+
# Set up logging
|
| 26 |
+
logging.basicConfig(level=logging.INFO)
|
| 27 |
+
logger = logging.getLogger(__name__)
|
| 28 |
+
|
| 29 |
+
app = FastAPI(title="OmniAvatar-14B API with ElevenLabs", version="1.0.0")
|
| 30 |
+
|
| 31 |
+
# Add CORS middleware
|
| 32 |
+
app.add_middleware(
|
| 33 |
+
CORSMiddleware,
|
| 34 |
+
allow_origins=["*"],
|
| 35 |
+
allow_credentials=True,
|
| 36 |
+
allow_methods=["*"],
|
| 37 |
+
allow_headers=["*"],
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
# Mount static files for serving generated videos
|
| 41 |
+
app.mount("/outputs", StaticFiles(directory="outputs"), name="outputs")
|
| 42 |
+
|
| 43 |
+
def get_video_url(output_path: str) -> str:
|
| 44 |
+
"""Convert local file path to accessible URL"""
|
| 45 |
+
try:
|
| 46 |
+
from pathlib import Path
|
| 47 |
+
filename = Path(output_path).name
|
| 48 |
+
|
| 49 |
+
# For HuggingFace Spaces, construct the URL
|
| 50 |
+
base_url = "https://bravedims-ai-avatar-chat.hf.space"
|
| 51 |
+
video_url = f"{base_url}/outputs/{filename}"
|
| 52 |
+
logger.info(f"Generated video URL: {video_url}")
|
| 53 |
+
return video_url
|
| 54 |
+
except Exception as e:
|
| 55 |
+
logger.error(f"Error creating video URL: {e}")
|
| 56 |
+
return output_path # Fallback to original path
|
| 57 |
+
|
| 58 |
+
# Pydantic models for request/response
|
| 59 |
+
class GenerateRequest(BaseModel):
|
| 60 |
+
prompt: str
|
| 61 |
+
text_to_speech: Optional[str] = None # Text to convert to speech
|
| 62 |
+
elevenlabs_audio_url: Optional[HttpUrl] = None # Direct audio URL
|
| 63 |
+
voice_id: Optional[str] = "21m00Tcm4TlvDq8ikWAM" # Default ElevenLabs voice
|
| 64 |
+
image_url: Optional[HttpUrl] = None
|
| 65 |
+
guidance_scale: float = 5.0
|
| 66 |
+
audio_scale: float = 3.0
|
| 67 |
+
num_steps: int = 30
|
| 68 |
+
sp_size: int = 1
|
| 69 |
+
tea_cache_l1_thresh: Optional[float] = None
|
| 70 |
+
|
| 71 |
+
class GenerateResponse(BaseModel):
|
| 72 |
+
message: str
|
| 73 |
+
output_path: str
|
| 74 |
+
processing_time: float
|
| 75 |
+
audio_generated: bool = False
|
| 76 |
+
|
| 77 |
+
class ElevenLabsClient:
|
| 78 |
+
def __init__(self, api_key: str = None):
|
| 79 |
+
self.api_key = api_key or os.getenv("ELEVENLABS_API_KEY", "sk_c7a0b115cd48fc026226158c5ac87755b063c802ad892de6")
|
| 80 |
+
self.base_url = "https://api.elevenlabs.io/v1"
|
| 81 |
+
|
| 82 |
+
async def text_to_speech(self, text: str, voice_id: str = "21m00Tcm4TlvDq8ikWAM") -> str:
|
| 83 |
+
"""Convert text to speech using ElevenLabs and return temporary file path"""
|
| 84 |
+
url = f"{self.base_url}/text-to-speech/{voice_id}"
|
| 85 |
+
|
| 86 |
+
headers = {
|
| 87 |
+
"Accept": "audio/mpeg",
|
| 88 |
+
"Content-Type": "application/json",
|
| 89 |
+
"xi-api-key": self.api_key
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
data = {
|
| 93 |
+
"text": text,
|
| 94 |
+
"model_id": "eleven_monolingual_v1",
|
| 95 |
+
"voice_settings": {
|
| 96 |
+
"stability": 0.5,
|
| 97 |
+
"similarity_boost": 0.5
|
| 98 |
+
}
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
try:
|
| 102 |
+
async with aiohttp.ClientSession() as session:
|
| 103 |
+
async with session.post(url, headers=headers, json=data) as response:
|
| 104 |
+
if response.status != 200:
|
| 105 |
+
error_text = await response.text()
|
| 106 |
+
raise HTTPException(
|
| 107 |
+
status_code=400,
|
| 108 |
+
detail=f"ElevenLabs API error: {response.status} - {error_text}"
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
audio_content = await response.read()
|
| 112 |
+
|
| 113 |
+
# Save to temporary file
|
| 114 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3')
|
| 115 |
+
temp_file.write(audio_content)
|
| 116 |
+
temp_file.close()
|
| 117 |
+
|
| 118 |
+
logger.info(f"Generated speech audio: {temp_file.name}")
|
| 119 |
+
return temp_file.name
|
| 120 |
+
|
| 121 |
+
except aiohttp.ClientError as e:
|
| 122 |
+
logger.error(f"Network error calling ElevenLabs: {e}")
|
| 123 |
+
raise HTTPException(status_code=400, detail=f"Network error calling ElevenLabs: {e}")
|
| 124 |
+
except Exception as e:
|
| 125 |
+
logger.error(f"Error generating speech: {e}")
|
| 126 |
+
raise HTTPException(status_code=500, detail=f"Error generating speech: {e}")
|
| 127 |
+
|
| 128 |
+
class OmniAvatarAPI:
|
| 129 |
+
def __init__(self):
|
| 130 |
+
self.model_loaded = False
|
| 131 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 132 |
+
self.elevenlabs_client = ElevenLabsClient()
|
| 133 |
+
logger.info(f"Using device: {self.device}")
|
| 134 |
+
logger.info(f"ElevenLabs API Key configured: {'Yes' if self.elevenlabs_client.api_key else 'No'}")
|
| 135 |
+
|
| 136 |
+
def load_model(self):
|
| 137 |
+
"""Load the OmniAvatar model"""
|
| 138 |
+
try:
|
| 139 |
+
# Check if models are downloaded
|
| 140 |
+
model_paths = [
|
| 141 |
+
"./pretrained_models/Wan2.1-T2V-14B",
|
| 142 |
+
"./pretrained_models/OmniAvatar-14B",
|
| 143 |
+
"./pretrained_models/wav2vec2-base-960h"
|
| 144 |
+
]
|
| 145 |
+
|
| 146 |
+
for path in model_paths:
|
| 147 |
+
if not os.path.exists(path):
|
| 148 |
+
logger.error(f"Model path not found: {path}")
|
| 149 |
+
return False
|
| 150 |
+
|
| 151 |
+
self.model_loaded = True
|
| 152 |
+
logger.info("Models loaded successfully")
|
| 153 |
+
return True
|
| 154 |
+
|
| 155 |
+
except Exception as e:
|
| 156 |
+
logger.error(f"Error loading model: {str(e)}")
|
| 157 |
+
return False
|
| 158 |
+
|
| 159 |
+
async def download_file(self, url: str, suffix: str = "") -> str:
|
| 160 |
+
"""Download file from URL and save to temporary location"""
|
| 161 |
+
try:
|
| 162 |
+
async with aiohttp.ClientSession() as session:
|
| 163 |
+
async with session.get(str(url)) as response:
|
| 164 |
+
if response.status != 200:
|
| 165 |
+
raise HTTPException(status_code=400, detail=f"Failed to download file from URL: {url}")
|
| 166 |
+
|
| 167 |
+
content = await response.read()
|
| 168 |
+
|
| 169 |
+
# Create temporary file
|
| 170 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
|
| 171 |
+
temp_file.write(content)
|
| 172 |
+
temp_file.close()
|
| 173 |
+
|
| 174 |
+
return temp_file.name
|
| 175 |
+
|
| 176 |
+
except aiohttp.ClientError as e:
|
| 177 |
+
logger.error(f"Network error downloading {url}: {e}")
|
| 178 |
+
raise HTTPException(status_code=400, detail=f"Network error downloading file: {e}")
|
| 179 |
+
except Exception as e:
|
| 180 |
+
logger.error(f"Error downloading file from {url}: {e}")
|
| 181 |
+
raise HTTPException(status_code=500, detail=f"Error downloading file: {e}")
|
| 182 |
+
|
| 183 |
+
def validate_audio_url(self, url: str) -> bool:
|
| 184 |
+
"""Validate if URL is likely an audio file"""
|
| 185 |
+
try:
|
| 186 |
+
parsed = urlparse(url)
|
| 187 |
+
# Check for common audio file extensions or ElevenLabs patterns
|
| 188 |
+
audio_extensions = ['.mp3', '.wav', '.m4a', '.ogg', '.aac']
|
| 189 |
+
is_audio_ext = any(parsed.path.lower().endswith(ext) for ext in audio_extensions)
|
| 190 |
+
is_elevenlabs = 'elevenlabs' in parsed.netloc.lower()
|
| 191 |
+
|
| 192 |
+
return is_audio_ext or is_elevenlabs or 'audio' in url.lower()
|
| 193 |
+
except:
|
| 194 |
+
return False
|
| 195 |
+
|
| 196 |
+
def validate_image_url(self, url: str) -> bool:
|
| 197 |
+
"""Validate if URL is likely an image file"""
|
| 198 |
+
try:
|
| 199 |
+
parsed = urlparse(url)
|
| 200 |
+
image_extensions = ['.jpg', '.jpeg', '.png', '.webp', '.bmp', '.gif']
|
| 201 |
+
return any(parsed.path.lower().endswith(ext) for ext in image_extensions)
|
| 202 |
+
except:
|
| 203 |
+
return False
|
| 204 |
+
|
| 205 |
+
async def generate_avatar(self, request: GenerateRequest) -> tuple[str, float, bool]:
|
| 206 |
+
"""Generate avatar video from prompt and audio/text"""
|
| 207 |
+
import time
|
| 208 |
+
start_time = time.time()
|
| 209 |
+
audio_generated = False
|
| 210 |
+
|
| 211 |
+
try:
|
| 212 |
+
# Determine audio source
|
| 213 |
+
audio_path = None
|
| 214 |
+
|
| 215 |
+
if request.text_to_speech:
|
| 216 |
+
# Generate speech from text using ElevenLabs
|
| 217 |
+
logger.info(f"Generating speech from text: {request.text_to_speech[:50]}...")
|
| 218 |
+
audio_path = await self.elevenlabs_client.text_to_speech(
|
| 219 |
+
request.text_to_speech,
|
| 220 |
+
request.voice_id or "21m00Tcm4TlvDq8ikWAM"
|
| 221 |
+
)
|
| 222 |
+
audio_generated = True
|
| 223 |
+
|
| 224 |
+
elif request.elevenlabs_audio_url:
|
| 225 |
+
# Download audio from provided URL
|
| 226 |
+
logger.info(f"Downloading audio from URL: {request.elevenlabs_audio_url}")
|
| 227 |
+
if not self.validate_audio_url(str(request.elevenlabs_audio_url)):
|
| 228 |
+
logger.warning(f"Audio URL may not be valid: {request.elevenlabs_audio_url}")
|
| 229 |
+
|
| 230 |
+
audio_path = await self.download_file(str(request.elevenlabs_audio_url), ".mp3")
|
| 231 |
+
|
| 232 |
+
else:
|
| 233 |
+
raise HTTPException(
|
| 234 |
+
status_code=400,
|
| 235 |
+
detail="Either text_to_speech or elevenlabs_audio_url must be provided"
|
| 236 |
+
)
|
| 237 |
+
|
| 238 |
+
# Download image if provided
|
| 239 |
+
image_path = None
|
| 240 |
+
if request.image_url:
|
| 241 |
+
logger.info(f"Downloading image from URL: {request.image_url}")
|
| 242 |
+
if not self.validate_image_url(str(request.image_url)):
|
| 243 |
+
logger.warning(f"Image URL may not be valid: {request.image_url}")
|
| 244 |
+
|
| 245 |
+
# Determine image extension from URL or default to .jpg
|
| 246 |
+
parsed = urlparse(str(request.image_url))
|
| 247 |
+
ext = os.path.splitext(parsed.path)[1] or ".jpg"
|
| 248 |
+
image_path = await self.download_file(str(request.image_url), ext)
|
| 249 |
+
|
| 250 |
+
# Create temporary input file for inference
|
| 251 |
+
with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as f:
|
| 252 |
+
if image_path:
|
| 253 |
+
input_line = f"{request.prompt}@@{image_path}@@{audio_path}"
|
| 254 |
+
else:
|
| 255 |
+
input_line = f"{request.prompt}@@@@{audio_path}"
|
| 256 |
+
f.write(input_line)
|
| 257 |
+
temp_input_file = f.name
|
| 258 |
+
|
| 259 |
+
# Prepare inference command
|
| 260 |
+
cmd = [
|
| 261 |
+
"python", "-m", "torch.distributed.run",
|
| 262 |
+
"--standalone", f"--nproc_per_node={request.sp_size}",
|
| 263 |
+
"scripts/inference.py",
|
| 264 |
+
"--config", "configs/inference.yaml",
|
| 265 |
+
"--input_file", temp_input_file,
|
| 266 |
+
"--guidance_scale", str(request.guidance_scale),
|
| 267 |
+
"--audio_scale", str(request.audio_scale),
|
| 268 |
+
"--num_steps", str(request.num_steps)
|
| 269 |
+
]
|
| 270 |
+
|
| 271 |
+
if request.tea_cache_l1_thresh:
|
| 272 |
+
cmd.extend(["--tea_cache_l1_thresh", str(request.tea_cache_l1_thresh)])
|
| 273 |
+
|
| 274 |
+
logger.info(f"Running inference with command: {' '.join(cmd)}")
|
| 275 |
+
|
| 276 |
+
# Run inference
|
| 277 |
+
result = subprocess.run(cmd, capture_output=True, text=True)
|
| 278 |
+
|
| 279 |
+
# Clean up temporary files
|
| 280 |
+
os.unlink(temp_input_file)
|
| 281 |
+
os.unlink(audio_path)
|
| 282 |
+
if image_path:
|
| 283 |
+
os.unlink(image_path)
|
| 284 |
+
|
| 285 |
+
if result.returncode != 0:
|
| 286 |
+
logger.error(f"Inference failed: {result.stderr}")
|
| 287 |
+
raise Exception(f"Inference failed: {result.stderr}")
|
| 288 |
+
|
| 289 |
+
# Find output video file
|
| 290 |
+
output_dir = "./outputs"
|
| 291 |
+
if os.path.exists(output_dir):
|
| 292 |
+
video_files = [f for f in os.listdir(output_dir) if f.endswith(('.mp4', '.avi'))]
|
| 293 |
+
if video_files:
|
| 294 |
+
# Return the most recent video file
|
| 295 |
+
video_files.sort(key=lambda x: os.path.getmtime(os.path.join(output_dir, x)), reverse=True)
|
| 296 |
+
output_path = os.path.join(output_dir, video_files[0])
|
| 297 |
+
processing_time = time.time() - start_time
|
| 298 |
+
return output_path, processing_time, audio_generated
|
| 299 |
+
|
| 300 |
+
raise Exception("No output video generated")
|
| 301 |
+
|
| 302 |
+
except Exception as e:
|
| 303 |
+
# Clean up any temporary files in case of error
|
| 304 |
+
try:
|
| 305 |
+
if 'audio_path' in locals() and audio_path and os.path.exists(audio_path):
|
| 306 |
+
os.unlink(audio_path)
|
| 307 |
+
if 'image_path' in locals() and image_path and os.path.exists(image_path):
|
| 308 |
+
os.unlink(image_path)
|
| 309 |
+
if 'temp_input_file' in locals() and os.path.exists(temp_input_file):
|
| 310 |
+
os.unlink(temp_input_file)
|
| 311 |
+
except:
|
| 312 |
+
pass
|
| 313 |
+
|
| 314 |
+
logger.error(f"Generation error: {str(e)}")
|
| 315 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 316 |
+
|
| 317 |
+
# Initialize API
|
| 318 |
+
omni_api = OmniAvatarAPI()
|
| 319 |
+
|
| 320 |
+
@app.on_event("startup")
|
| 321 |
+
async def startup_event():
|
| 322 |
+
"""Load model on startup"""
|
| 323 |
+
success = omni_api.load_model()
|
| 324 |
+
if not success:
|
| 325 |
+
logger.warning("Model loading failed on startup")
|
| 326 |
+
|
| 327 |
+
@app.get("/health")
|
| 328 |
+
async def health_check():
|
| 329 |
+
"""Health check endpoint"""
|
| 330 |
+
return {
|
| 331 |
+
"status": "healthy",
|
| 332 |
+
"model_loaded": omni_api.model_loaded,
|
| 333 |
+
"device": omni_api.device,
|
| 334 |
+
"supports_elevenlabs": True,
|
| 335 |
+
"supports_image_urls": True,
|
| 336 |
+
"supports_text_to_speech": True,
|
| 337 |
+
"elevenlabs_api_configured": bool(omni_api.elevenlabs_client.api_key)
|
| 338 |
+
}
|
| 339 |
+
|
| 340 |
+
@app.post("/generate", response_model=GenerateResponse)
|
| 341 |
+
async def generate_avatar(request: GenerateRequest):
|
| 342 |
+
"""Generate avatar video from prompt, text/audio, and optional image URL"""
|
| 343 |
+
|
| 344 |
+
if not omni_api.model_loaded:
|
| 345 |
+
raise HTTPException(status_code=503, detail="Model not loaded")
|
| 346 |
+
|
| 347 |
+
logger.info(f"Generating avatar with prompt: {request.prompt}")
|
| 348 |
+
if request.text_to_speech:
|
| 349 |
+
logger.info(f"Text to speech: {request.text_to_speech[:100]}...")
|
| 350 |
+
logger.info(f"Voice ID: {request.voice_id}")
|
| 351 |
+
if request.elevenlabs_audio_url:
|
| 352 |
+
logger.info(f"Audio URL: {request.elevenlabs_audio_url}")
|
| 353 |
+
if request.image_url:
|
| 354 |
+
logger.info(f"Image URL: {request.image_url}")
|
| 355 |
+
|
| 356 |
+
try:
|
| 357 |
+
output_path, processing_time, audio_generated = await omni_api.generate_avatar(request)
|
| 358 |
+
|
| 359 |
+
return GenerateResponse(
|
| 360 |
+
message="Avatar generation completed successfully",
|
| 361 |
+
output_path=get_video_url(output_path),
|
| 362 |
+
processing_time=processing_time,
|
| 363 |
+
audio_generated=audio_generated
|
| 364 |
+
)
|
| 365 |
+
|
| 366 |
+
except HTTPException:
|
| 367 |
+
raise
|
| 368 |
+
except Exception as e:
|
| 369 |
+
logger.error(f"Unexpected error: {e}")
|
| 370 |
+
raise HTTPException(status_code=500, detail=f"Unexpected error: {e}")
|
| 371 |
+
|
| 372 |
+
# Enhanced Gradio interface with text-to-speech option
|
| 373 |
+
def gradio_generate(prompt, text_to_speech, audio_url, image_url, voice_id, guidance_scale, audio_scale, num_steps):
|
| 374 |
+
"""Gradio interface wrapper with text-to-speech support"""
|
| 375 |
+
if not omni_api.model_loaded:
|
| 376 |
+
return "Error: Model not loaded"
|
| 377 |
+
|
| 378 |
+
try:
|
| 379 |
+
# Create request object
|
| 380 |
+
request_data = {
|
| 381 |
+
"prompt": prompt,
|
| 382 |
+
"guidance_scale": guidance_scale,
|
| 383 |
+
"audio_scale": audio_scale,
|
| 384 |
+
"num_steps": int(num_steps)
|
| 385 |
+
}
|
| 386 |
+
|
| 387 |
+
# Add audio source
|
| 388 |
+
if text_to_speech and text_to_speech.strip():
|
| 389 |
+
request_data["text_to_speech"] = text_to_speech
|
| 390 |
+
request_data["voice_id"] = voice_id or "21m00Tcm4TlvDq8ikWAM"
|
| 391 |
+
elif audio_url and audio_url.strip():
|
| 392 |
+
request_data["elevenlabs_audio_url"] = audio_url
|
| 393 |
+
else:
|
| 394 |
+
return "Error: Please provide either text to speech or audio URL"
|
| 395 |
+
|
| 396 |
+
if image_url and image_url.strip():
|
| 397 |
+
request_data["image_url"] = image_url
|
| 398 |
+
|
| 399 |
+
request = GenerateRequest(**request_data)
|
| 400 |
+
|
| 401 |
+
# Run async function in sync context
|
| 402 |
+
loop = asyncio.new_event_loop()
|
| 403 |
+
asyncio.set_event_loop(loop)
|
| 404 |
+
output_path, processing_time, audio_generated = loop.run_until_complete(omni_api.generate_avatar(request))
|
| 405 |
+
loop.close()
|
| 406 |
+
|
| 407 |
+
return output_path
|
| 408 |
+
|
| 409 |
+
except Exception as e:
|
| 410 |
+
logger.error(f"Gradio generation error: {e}")
|
| 411 |
+
return f"Error: {str(e)}"
|
| 412 |
+
|
| 413 |
+
# Updated Gradio interface with text-to-speech support
|
| 414 |
+
iface = gr.Interface(
|
| 415 |
+
fn=gradio_generate,
|
| 416 |
+
inputs=[
|
| 417 |
+
gr.Textbox(
|
| 418 |
+
label="Prompt",
|
| 419 |
+
placeholder="Describe the character behavior (e.g., 'A friendly person explaining a concept')",
|
| 420 |
+
lines=2
|
| 421 |
+
),
|
| 422 |
+
gr.Textbox(
|
| 423 |
+
label="Text to Speech",
|
| 424 |
+
placeholder="Enter text to convert to speech using ElevenLabs",
|
| 425 |
+
lines=3,
|
| 426 |
+
info="This will be converted to speech automatically"
|
| 427 |
+
),
|
| 428 |
+
gr.Textbox(
|
| 429 |
+
label="OR Audio URL",
|
| 430 |
+
placeholder="https://api.elevenlabs.io/v1/text-to-speech/...",
|
| 431 |
+
info="Direct URL to audio file (alternative to text-to-speech)"
|
| 432 |
+
),
|
| 433 |
+
gr.Textbox(
|
| 434 |
+
label="Image URL (Optional)",
|
| 435 |
+
placeholder="https://example.com/image.jpg",
|
| 436 |
+
info="Direct URL to reference image (JPG, PNG, etc.)"
|
| 437 |
+
),
|
| 438 |
+
gr.Dropdown(
|
| 439 |
+
choices=["21m00Tcm4TlvDq8ikWAM", "pNInz6obpgDQGcFmaJgB", "EXAVITQu4vr4xnSDxMaL"],
|
| 440 |
+
value="21m00Tcm4TlvDq8ikWAM",
|
| 441 |
+
label="ElevenLabs Voice ID",
|
| 442 |
+
info="Choose voice for text-to-speech"
|
| 443 |
+
),
|
| 444 |
+
gr.Slider(minimum=1, maximum=10, value=5.0, label="Guidance Scale", info="4-6 recommended"),
|
| 445 |
+
gr.Slider(minimum=1, maximum=10, value=3.0, label="Audio Scale", info="Higher values = better lip-sync"),
|
| 446 |
+
gr.Slider(minimum=10, maximum=100, value=30, step=1, label="Number of Steps", info="20-50 recommended")
|
| 447 |
+
],
|
| 448 |
+
outputs=gr.Video(label="Generated Avatar Video"),
|
| 449 |
+
title="π OmniAvatar-14B with ElevenLabs TTS",
|
| 450 |
+
description="""
|
| 451 |
+
Generate avatar videos with lip-sync from text prompts and speech.
|
| 452 |
+
|
| 453 |
+
**Features:**
|
| 454 |
+
- β
**Text-to-Speech**: Enter text to generate speech automatically
|
| 455 |
+
- β
**ElevenLabs Integration**: High-quality voice synthesis
|
| 456 |
+
- β
**Audio URL Support**: Use pre-generated audio files
|
| 457 |
+
- β
**Image URL Support**: Reference images for character appearance
|
| 458 |
+
- β
**Customizable Parameters**: Fine-tune generation quality
|
| 459 |
+
|
| 460 |
+
**Usage:**
|
| 461 |
+
1. Enter a character description in the prompt
|
| 462 |
+
2. **Either** enter text for speech generation **OR** provide an audio URL
|
| 463 |
+
3. Optionally add a reference image URL
|
| 464 |
+
4. Choose voice and adjust parameters
|
| 465 |
+
5. Generate your avatar video!
|
| 466 |
+
|
| 467 |
+
**Tips:**
|
| 468 |
+
- Use guidance scale 4-6 for best prompt following
|
| 469 |
+
- Increase audio scale for better lip-sync
|
| 470 |
+
- Clear, descriptive prompts work best
|
| 471 |
+
""",
|
| 472 |
+
examples=[
|
| 473 |
+
[
|
| 474 |
+
"A professional teacher explaining a mathematical concept with clear gestures",
|
| 475 |
+
"Hello students! Today we're going to learn about calculus and how derivatives work in real life.",
|
| 476 |
+
"",
|
| 477 |
+
"https://example.com/teacher.jpg",
|
| 478 |
+
"21m00Tcm4TlvDq8ikWAM",
|
| 479 |
+
5.0,
|
| 480 |
+
3.5,
|
| 481 |
+
30
|
| 482 |
+
],
|
| 483 |
+
[
|
| 484 |
+
"A friendly presenter speaking confidently to an audience",
|
| 485 |
+
"Welcome everyone to our presentation on artificial intelligence and its applications!",
|
| 486 |
+
"",
|
| 487 |
+
"",
|
| 488 |
+
"pNInz6obpgDQGcFmaJgB",
|
| 489 |
+
5.5,
|
| 490 |
+
4.0,
|
| 491 |
+
35
|
| 492 |
+
]
|
| 493 |
+
]
|
| 494 |
+
)
|
| 495 |
+
|
| 496 |
+
# Mount Gradio app
|
| 497 |
+
app = gr.mount_gradio_app(app, iface, path="/gradio")
|
| 498 |
+
|
| 499 |
+
if __name__ == "__main__":
|
| 500 |
+
import uvicorn
|
| 501 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 502 |
+
|
| 503 |
+
|