Spaces:
				
			
			
	
			
			
					
		Running
		
	
	
	
			
			
	
	
	
	
		
		๐ฅ ADD MODEL DOWNLOAD CONTROLS: Easy model installation
Browse filesBased on model-status showing 339GB free space but no models downloaded!
โ
 **MULTIPLE DOWNLOAD METHODS:**
๐ฎ **1. Gradio UI Controls:**
- Added 'Download Models' button in web interface
- Real-time model status checking
- Shows storage usage and download progress
- User-friendly progress indicators
๐ก **2. API Endpoints (already working):**
- POST /download-models - Trigger downloads
- GET /model-status - Check status
- Perfect for manual API calls
๐ฅ๏ธ **3. Manual Download Script:**
- manual_download.py - Run directly for immediate download
- Command line progress tracking
- Storage verification before download
๐ **4. Startup Script:**
- startup_download.py - Triggers download after app loads
- Background thread execution
- Automatic retry logic
๐ **STORAGE STATUS FROM YOUR SPACE:**
- Available: 339.91 GB (plenty of space!)
- Required: ~3GB for both models
- Models: ali-vilab/text-to-video-ms-1.7b + facebook/wav2vec2-base-960h
๐ฏ **HOW TO USE:**
1. Visit your space: bravedims-ai-avatar-chat.hf.space
2. Click 'Download Models' button in the interface
3. Wait for download completion (~3GB)
4. Generate actual videos!
๐ฌ **EXPECTED RESULT:**
With 339GB free space, downloads will complete successfully and enable real video generation on your HF Space!
- app.py +1 -0
 - app_enhanced_ui.py +973 -0
 - gradio_model_controls.py +34 -0
 - manual_download.py +79 -0
 - startup_download.py +101 -0
 
| 
         @@ -970,3 +970,4 @@ if __name__ == "__main__": 
     | 
|
| 970 | 
         | 
| 971 | 
         | 
| 972 | 
         | 
| 
         | 
| 
         | 
|
| 970 | 
         | 
| 971 | 
         | 
| 972 | 
         | 
| 973 | 
         
            +
             
     | 
| 
         @@ -0,0 +1,973 @@ 
     | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
| 
         | 
|
| 1 | 
         
            +
            import os
         
     | 
| 2 | 
         
            +
             
     | 
| 3 | 
         
            +
            # STORAGE OPTIMIZATION: Check if running on HF Spaces and disable model downloads
         
     | 
| 4 | 
         
            +
            IS_HF_SPACE = any([
         
     | 
| 5 | 
         
            +
                os.getenv("SPACE_ID"),
         
     | 
| 6 | 
         
            +
                os.getenv("SPACE_AUTHOR_NAME"), 
         
     | 
| 7 | 
         
            +
                os.getenv("SPACES_BUILDKIT_VERSION"),
         
     | 
| 8 | 
         
            +
                "/home/user/app" in os.getcwd()
         
     | 
| 9 | 
         
            +
            ])
         
     | 
| 10 | 
         
            +
             
     | 
| 11 | 
         
            +
            if IS_HF_SPACE:
         
     | 
| 12 | 
         
            +
                # Force TTS-only mode to prevent storage limit exceeded
         
     | 
| 13 | 
         
            +
                os.environ["DISABLE_MODEL_DOWNLOAD"] = "1"
         
     | 
| 14 | 
         
            +
                os.environ["TTS_ONLY_MODE"] = "1" 
         
     | 
| 15 | 
         
            +
                os.environ["HF_SPACE_STORAGE_OPTIMIZED"] = "1"
         
     | 
| 16 | 
         
            +
                print("?? STORAGE OPTIMIZATION: Detected HF Space environment")
         
     | 
| 17 | 
         
            +
                print("??? TTS-only mode ENABLED (video generation disabled for storage limits)")
         
     | 
| 18 | 
         
            +
                print("?? Model auto-download DISABLED to prevent storage exceeded error")
         
     | 
| 19 | 
         
            +
            import os
         
     | 
| 20 | 
         
            +
            import torch
         
     | 
| 21 | 
         
            +
            import tempfile
         
     | 
| 22 | 
         
            +
            import gradio as gr
         
     | 
| 23 | 
         
            +
            from fastapi import FastAPI, HTTPException
         
     | 
| 24 | 
         
            +
            from fastapi.staticfiles import StaticFiles
         
     | 
| 25 | 
         
            +
            from fastapi.middleware.cors import CORSMiddleware
         
     | 
| 26 | 
         
            +
            from pydantic import BaseModel, HttpUrl
         
     | 
| 27 | 
         
            +
            import subprocess
         
     | 
| 28 | 
         
            +
            import json
         
     | 
| 29 | 
         
            +
            from pathlib import Path
         
     | 
| 30 | 
         
            +
            import logging
         
     | 
| 31 | 
         
            +
            import requests
         
     | 
| 32 | 
         
            +
            from urllib.parse import urlparse
         
     | 
| 33 | 
         
            +
            from PIL import Image
         
     | 
| 34 | 
         
            +
            import io
         
     | 
| 35 | 
         
            +
            from typing import Optional
         
     | 
| 36 | 
         
            +
            import aiohttp
         
     | 
| 37 | 
         
            +
            import asyncio
         
     | 
| 38 | 
         
            +
            # Safe dotenv import
         
     | 
| 39 | 
         
            +
            try:
         
     | 
| 40 | 
         
            +
                from dotenv import load_dotenv
         
     | 
| 41 | 
         
            +
                load_dotenv()
         
     | 
| 42 | 
         
            +
            except ImportError:
         
     | 
| 43 | 
         
            +
                print("Warning: python-dotenv not found, continuing without .env support")
         
     | 
| 44 | 
         
            +
                def load_dotenv():
         
     | 
| 45 | 
         
            +
                    pass
         
     | 
| 46 | 
         
            +
             
     | 
| 47 | 
         
            +
            # CRITICAL: HF Spaces compatibility fix
         
     | 
| 48 | 
         
            +
            try:
         
     | 
| 49 | 
         
            +
                from hf_spaces_fix import setup_hf_spaces_environment, HFSpacesCompatible
         
     | 
| 50 | 
         
            +
                setup_hf_spaces_environment()
         
     | 
| 51 | 
         
            +
            except ImportError:
         
     | 
| 52 | 
         
            +
                print('Warning: HF Spaces fix not available')
         
     | 
| 53 | 
         
            +
             
     | 
| 54 | 
         
            +
            # Load environment variables
         
     | 
| 55 | 
         
            +
            load_dotenv()
         
     | 
| 56 | 
         
            +
             
     | 
| 57 | 
         
            +
            # Set up logging
         
     | 
| 58 | 
         
            +
            logging.basicConfig(level=logging.INFO)
         
     | 
| 59 | 
         
            +
            logger = logging.getLogger(__name__)
         
     | 
| 60 | 
         
            +
             
     | 
| 61 | 
         
            +
            # Set environment variables for matplotlib, gradio, and huggingface cache
         
     | 
| 62 | 
         
            +
            os.environ['MPLCONFIGDIR'] = '/tmp/matplotlib'
         
     | 
| 63 | 
         
            +
            os.environ['GRADIO_ALLOW_FLAGGING'] = 'never'
         
     | 
| 64 | 
         
            +
            os.environ['HF_HOME'] = '/tmp/huggingface'
         
     | 
| 65 | 
         
            +
            # Use HF_HOME instead of deprecated TRANSFORMERS_CACHE
         
     | 
| 66 | 
         
            +
            os.environ['HF_DATASETS_CACHE'] = '/tmp/huggingface/datasets'
         
     | 
| 67 | 
         
            +
            os.environ['HUGGINGFACE_HUB_CACHE'] = '/tmp/huggingface/hub'
         
     | 
| 68 | 
         
            +
             
     | 
| 69 | 
         
            +
            # FastAPI app will be created after lifespan is defined
         
     | 
| 70 | 
         
            +
             
     | 
| 71 | 
         
            +
             
     | 
| 72 | 
         
            +
             
     | 
| 73 | 
         
            +
            # Create directories with proper permissions
         
     | 
| 74 | 
         
            +
            os.makedirs("outputs", exist_ok=True)
         
     | 
| 75 | 
         
            +
            os.makedirs("/tmp/matplotlib", exist_ok=True)
         
     | 
| 76 | 
         
            +
            os.makedirs("/tmp/huggingface", exist_ok=True)
         
     | 
| 77 | 
         
            +
            os.makedirs("/tmp/huggingface/transformers", exist_ok=True)
         
     | 
| 78 | 
         
            +
            os.makedirs("/tmp/huggingface/datasets", exist_ok=True)
         
     | 
| 79 | 
         
            +
            os.makedirs("/tmp/huggingface/hub", exist_ok=True)
         
     | 
| 80 | 
         
            +
             
     | 
| 81 | 
         
            +
            # Mount static files for serving generated videos  
         
     | 
| 82 | 
         
            +
             
     | 
| 83 | 
         
            +
             
     | 
| 84 | 
         
            +
            def get_video_url(output_path: str) -> str:
         
     | 
| 85 | 
         
            +
                """Convert local file path to accessible URL"""
         
     | 
| 86 | 
         
            +
                try:
         
     | 
| 87 | 
         
            +
                    from pathlib import Path
         
     | 
| 88 | 
         
            +
                    filename = Path(output_path).name
         
     | 
| 89 | 
         
            +
                    
         
     | 
| 90 | 
         
            +
                    # For HuggingFace Spaces, construct the URL
         
     | 
| 91 | 
         
            +
                    base_url = "https://bravedims-ai-avatar-chat.hf.space"
         
     | 
| 92 | 
         
            +
                    video_url = f"{base_url}/outputs/{filename}"
         
     | 
| 93 | 
         
            +
                    logger.info(f"Generated video URL: {video_url}")
         
     | 
| 94 | 
         
            +
                    return video_url
         
     | 
| 95 | 
         
            +
                except Exception as e:
         
     | 
| 96 | 
         
            +
                    logger.error(f"Error creating video URL: {e}")
         
     | 
| 97 | 
         
            +
                    return output_path  # Fallback to original path
         
     | 
| 98 | 
         
            +
             
     | 
| 99 | 
         
            +
            # Pydantic models for request/response
         
     | 
| 100 | 
         
            +
            class GenerateRequest(BaseModel):
         
     | 
| 101 | 
         
            +
                prompt: str
         
     | 
| 102 | 
         
            +
                text_to_speech: Optional[str] = None  # Text to convert to speech
         
     | 
| 103 | 
         
            +
                audio_url: Optional[HttpUrl] = None  # Direct audio URL
         
     | 
| 104 | 
         
            +
                voice_id: Optional[str] = "21m00Tcm4TlvDq8ikWAM"  # Voice profile ID
         
     | 
| 105 | 
         
            +
                image_url: Optional[HttpUrl] = None
         
     | 
| 106 | 
         
            +
                guidance_scale: float = 5.0
         
     | 
| 107 | 
         
            +
                audio_scale: float = 3.0
         
     | 
| 108 | 
         
            +
                num_steps: int = 30
         
     | 
| 109 | 
         
            +
                sp_size: int = 1
         
     | 
| 110 | 
         
            +
                tea_cache_l1_thresh: Optional[float] = None
         
     | 
| 111 | 
         
            +
             
     | 
| 112 | 
         
            +
            class GenerateResponse(BaseModel):
         
     | 
| 113 | 
         
            +
                message: str
         
     | 
| 114 | 
         
            +
                output_path: str
         
     | 
| 115 | 
         
            +
                processing_time: float
         
     | 
| 116 | 
         
            +
                audio_generated: bool = False
         
     | 
| 117 | 
         
            +
                tts_method: Optional[str] = None
         
     | 
| 118 | 
         
            +
             
     | 
| 119 | 
         
            +
            # Try to import TTS clients, but make them optional
         
     | 
| 120 | 
         
            +
            try:
         
     | 
| 121 | 
         
            +
                from advanced_tts_client import AdvancedTTSClient
         
     | 
| 122 | 
         
            +
                ADVANCED_TTS_AVAILABLE = True
         
     | 
| 123 | 
         
            +
                logger.info("SUCCESS: Advanced TTS client available")
         
     | 
| 124 | 
         
            +
            except ImportError as e:
         
     | 
| 125 | 
         
            +
                ADVANCED_TTS_AVAILABLE = False
         
     | 
| 126 | 
         
            +
                logger.warning(f"WARNING: Advanced TTS client not available: {e}")
         
     | 
| 127 | 
         
            +
             
     | 
| 128 | 
         
            +
            # Always import the robust fallback
         
     | 
| 129 | 
         
            +
            try:
         
     | 
| 130 | 
         
            +
                from robust_tts_client import RobustTTSClient
         
     | 
| 131 | 
         
            +
                ROBUST_TTS_AVAILABLE = True
         
     | 
| 132 | 
         
            +
                logger.info("SUCCESS: Robust TTS client available")
         
     | 
| 133 | 
         
            +
            except ImportError as e:
         
     | 
| 134 | 
         
            +
                ROBUST_TTS_AVAILABLE = False
         
     | 
| 135 | 
         
            +
                logger.error(f"ERROR: Robust TTS client not available: {e}")
         
     | 
| 136 | 
         
            +
             
     | 
| 137 | 
         
            +
            class TTSManager:
         
     | 
| 138 | 
         
            +
                """Manages multiple TTS clients with fallback chain"""
         
     | 
| 139 | 
         
            +
                
         
     | 
| 140 | 
         
            +
                def __init__(self):
         
     | 
| 141 | 
         
            +
                    # Initialize TTS clients based on availability
         
     | 
| 142 | 
         
            +
                    self.advanced_tts = None
         
     | 
| 143 | 
         
            +
                    self.robust_tts = None
         
     | 
| 144 | 
         
            +
                    self.clients_loaded = False
         
     | 
| 145 | 
         
            +
                    
         
     | 
| 146 | 
         
            +
                    if ADVANCED_TTS_AVAILABLE:
         
     | 
| 147 | 
         
            +
                        try:
         
     | 
| 148 | 
         
            +
                            self.advanced_tts = AdvancedTTSClient()
         
     | 
| 149 | 
         
            +
                            logger.info("SUCCESS: Advanced TTS client initialized")
         
     | 
| 150 | 
         
            +
                        except Exception as e:
         
     | 
| 151 | 
         
            +
                            logger.warning(f"WARNING: Advanced TTS client initialization failed: {e}")
         
     | 
| 152 | 
         
            +
                    
         
     | 
| 153 | 
         
            +
                    if ROBUST_TTS_AVAILABLE:
         
     | 
| 154 | 
         
            +
                        try:
         
     | 
| 155 | 
         
            +
                            self.robust_tts = RobustTTSClient()
         
     | 
| 156 | 
         
            +
                            logger.info("SUCCESS: Robust TTS client initialized")
         
     | 
| 157 | 
         
            +
                        except Exception as e:
         
     | 
| 158 | 
         
            +
                            logger.error(f"ERROR: Robust TTS client initialization failed: {e}")
         
     | 
| 159 | 
         
            +
                    
         
     | 
| 160 | 
         
            +
                    if not self.advanced_tts and not self.robust_tts:
         
     | 
| 161 | 
         
            +
                        logger.error("ERROR: No TTS clients available!")
         
     | 
| 162 | 
         
            +
                    
         
     | 
| 163 | 
         
            +
                async def load_models(self):
         
     | 
| 164 | 
         
            +
                    """Load TTS models"""
         
     | 
| 165 | 
         
            +
                    try:
         
     | 
| 166 | 
         
            +
                        logger.info("Loading TTS models...")
         
     | 
| 167 | 
         
            +
                        
         
     | 
| 168 | 
         
            +
                        # Try to load advanced TTS first
         
     | 
| 169 | 
         
            +
                        if self.advanced_tts:
         
     | 
| 170 | 
         
            +
                            try:
         
     | 
| 171 | 
         
            +
                                logger.info("[PROCESS] Loading advanced TTS models (this may take a few minutes)...")
         
     | 
| 172 | 
         
            +
                                success = await self.advanced_tts.load_models()
         
     | 
| 173 | 
         
            +
                                if success:
         
     | 
| 174 | 
         
            +
                                    logger.info("SUCCESS: Advanced TTS models loaded successfully")
         
     | 
| 175 | 
         
            +
                                else:
         
     | 
| 176 | 
         
            +
                                    logger.warning("WARNING: Advanced TTS models failed to load")
         
     | 
| 177 | 
         
            +
                            except Exception as e:
         
     | 
| 178 | 
         
            +
                                logger.warning(f"WARNING: Advanced TTS loading error: {e}")
         
     | 
| 179 | 
         
            +
                        
         
     | 
| 180 | 
         
            +
                        # Always ensure robust TTS is available
         
     | 
| 181 | 
         
            +
                        if self.robust_tts:
         
     | 
| 182 | 
         
            +
                            try:
         
     | 
| 183 | 
         
            +
                                await self.robust_tts.load_model()
         
     | 
| 184 | 
         
            +
                                logger.info("SUCCESS: Robust TTS fallback ready")
         
     | 
| 185 | 
         
            +
                            except Exception as e:
         
     | 
| 186 | 
         
            +
                                logger.error(f"ERROR: Robust TTS loading failed: {e}")
         
     | 
| 187 | 
         
            +
                        
         
     | 
| 188 | 
         
            +
                        self.clients_loaded = True
         
     | 
| 189 | 
         
            +
                        return True
         
     | 
| 190 | 
         
            +
                        
         
     | 
| 191 | 
         
            +
                    except Exception as e:
         
     | 
| 192 | 
         
            +
                        logger.error(f"ERROR: TTS manager initialization failed: {e}")
         
     | 
| 193 | 
         
            +
                        return False
         
     | 
| 194 | 
         
            +
                
         
     | 
| 195 | 
         
            +
                async def text_to_speech(self, text: str, voice_id: Optional[str] = None) -> tuple[str, str]:
         
     | 
| 196 | 
         
            +
                    """
         
     | 
| 197 | 
         
            +
                    Convert text to speech with fallback chain
         
     | 
| 198 | 
         
            +
                    Returns: (audio_file_path, method_used)
         
     | 
| 199 | 
         
            +
                    """
         
     | 
| 200 | 
         
            +
                    if not self.clients_loaded:
         
     | 
| 201 | 
         
            +
                        logger.info("TTS models not loaded, loading now...")
         
     | 
| 202 | 
         
            +
                        await self.load_models()
         
     | 
| 203 | 
         
            +
                    
         
     | 
| 204 | 
         
            +
                    logger.info(f"Generating speech: {text[:50]}...")
         
     | 
| 205 | 
         
            +
                    logger.info(f"Voice ID: {voice_id}")
         
     | 
| 206 | 
         
            +
                    
         
     | 
| 207 | 
         
            +
                    # Try Advanced TTS first (Facebook VITS / SpeechT5)
         
     | 
| 208 | 
         
            +
                    if self.advanced_tts:
         
     | 
| 209 | 
         
            +
                        try:
         
     | 
| 210 | 
         
            +
                            audio_path = await self.advanced_tts.text_to_speech(text, voice_id)
         
     | 
| 211 | 
         
            +
                            return audio_path, "Facebook VITS/SpeechT5"
         
     | 
| 212 | 
         
            +
                        except Exception as advanced_error:
         
     | 
| 213 | 
         
            +
                            logger.warning(f"Advanced TTS failed: {advanced_error}")
         
     | 
| 214 | 
         
            +
                    
         
     | 
| 215 | 
         
            +
                    # Fall back to robust TTS
         
     | 
| 216 | 
         
            +
                    if self.robust_tts:
         
     | 
| 217 | 
         
            +
                        try:
         
     | 
| 218 | 
         
            +
                            logger.info("Falling back to robust TTS...")
         
     | 
| 219 | 
         
            +
                            audio_path = await self.robust_tts.text_to_speech(text, voice_id)
         
     | 
| 220 | 
         
            +
                            return audio_path, "Robust TTS (Fallback)"
         
     | 
| 221 | 
         
            +
                        except Exception as robust_error:
         
     | 
| 222 | 
         
            +
                            logger.error(f"Robust TTS also failed: {robust_error}")
         
     | 
| 223 | 
         
            +
                    
         
     | 
| 224 | 
         
            +
                    # If we get here, all methods failed
         
     | 
| 225 | 
         
            +
                    logger.error("All TTS methods failed!")
         
     | 
| 226 | 
         
            +
                    raise HTTPException(
         
     | 
| 227 | 
         
            +
                        status_code=500, 
         
     | 
| 228 | 
         
            +
                        detail="All TTS methods failed. Please check system configuration."
         
     | 
| 229 | 
         
            +
                    )
         
     | 
| 230 | 
         
            +
                
         
     | 
| 231 | 
         
            +
                async def get_available_voices(self):
         
     | 
| 232 | 
         
            +
                    """Get available voice configurations"""
         
     | 
| 233 | 
         
            +
                    try:
         
     | 
| 234 | 
         
            +
                        if self.advanced_tts and hasattr(self.advanced_tts, 'get_available_voices'):
         
     | 
| 235 | 
         
            +
                            return await self.advanced_tts.get_available_voices()
         
     | 
| 236 | 
         
            +
                    except:
         
     | 
| 237 | 
         
            +
                        pass
         
     | 
| 238 | 
         
            +
                    
         
     | 
| 239 | 
         
            +
                    # Return default voices if advanced TTS not available
         
     | 
| 240 | 
         
            +
                    return {
         
     | 
| 241 | 
         
            +
                        "21m00Tcm4TlvDq8ikWAM": "Female (Neutral)",
         
     | 
| 242 | 
         
            +
                        "pNInz6obpgDQGcFmaJgB": "Male (Professional)", 
         
     | 
| 243 | 
         
            +
                        "EXAVITQu4vr4xnSDxMaL": "Female (Sweet)",
         
     | 
| 244 | 
         
            +
                        "ErXwobaYiN019PkySvjV": "Male (Professional)",
         
     | 
| 245 | 
         
            +
                        "TxGEqnHWrfGW9XjX": "Male (Deep)",
         
     | 
| 246 | 
         
            +
                        "yoZ06aMxZJJ28mfd3POQ": "Unisex (Friendly)",
         
     | 
| 247 | 
         
            +
                        "AZnzlk1XvdvUeBnXmlld": "Female (Strong)"
         
     | 
| 248 | 
         
            +
                    }
         
     | 
| 249 | 
         
            +
                
         
     | 
| 250 | 
         
            +
                def get_tts_info(self):
         
     | 
| 251 | 
         
            +
                    """Get TTS system information"""
         
     | 
| 252 | 
         
            +
                    info = {
         
     | 
| 253 | 
         
            +
                        "clients_loaded": self.clients_loaded,
         
     | 
| 254 | 
         
            +
                        "advanced_tts_available": self.advanced_tts is not None,
         
     | 
| 255 | 
         
            +
                        "robust_tts_available": self.robust_tts is not None,
         
     | 
| 256 | 
         
            +
                        "primary_method": "Robust TTS"
         
     | 
| 257 | 
         
            +
                    }
         
     | 
| 258 | 
         
            +
                    
         
     | 
| 259 | 
         
            +
                    try:
         
     | 
| 260 | 
         
            +
                        if self.advanced_tts and hasattr(self.advanced_tts, 'get_model_info'):
         
     | 
| 261 | 
         
            +
                            advanced_info = self.advanced_tts.get_model_info()
         
     | 
| 262 | 
         
            +
                            info.update({
         
     | 
| 263 | 
         
            +
                                "advanced_tts_loaded": advanced_info.get("models_loaded", False),
         
     | 
| 264 | 
         
            +
                                "transformers_available": advanced_info.get("transformers_available", False),
         
     | 
| 265 | 
         
            +
                                "primary_method": "Facebook VITS/SpeechT5" if advanced_info.get("models_loaded") else "Robust TTS",
         
     | 
| 266 | 
         
            +
                                "device": advanced_info.get("device", "cpu"),
         
     | 
| 267 | 
         
            +
                                "vits_available": advanced_info.get("vits_available", False),
         
     | 
| 268 | 
         
            +
                                "speecht5_available": advanced_info.get("speecht5_available", False)
         
     | 
| 269 | 
         
            +
                            })
         
     | 
| 270 | 
         
            +
                    except Exception as e:
         
     | 
| 271 | 
         
            +
                        logger.debug(f"Could not get advanced TTS info: {e}")
         
     | 
| 272 | 
         
            +
                    
         
     | 
| 273 | 
         
            +
                    return info
         
     | 
| 274 | 
         
            +
             
     | 
| 275 | 
         
            +
            # Import the VIDEO-FOCUSED engine
         
     | 
| 276 | 
         
            +
            try:
         
     | 
| 277 | 
         
            +
                from omniavatar_video_engine import video_engine
         
     | 
| 278 | 
         
            +
                VIDEO_ENGINE_AVAILABLE = True
         
     | 
| 279 | 
         
            +
                logger.info("SUCCESS: OmniAvatar Video Engine available")
         
     | 
| 280 | 
         
            +
            except ImportError as e:
         
     | 
| 281 | 
         
            +
                VIDEO_ENGINE_AVAILABLE = False
         
     | 
| 282 | 
         
            +
                logger.error(f"ERROR: OmniAvatar Video Engine not available: {e}")
         
     | 
| 283 | 
         
            +
             
     | 
| 284 | 
         
            +
            class OmniAvatarAPI:
         
     | 
| 285 | 
         
            +
                def __init__(self):
         
     | 
| 286 | 
         
            +
                    self.model_loaded = False
         
     | 
| 287 | 
         
            +
                    self.device = "cuda" if torch.cuda.is_available() else "cpu"
         
     | 
| 288 | 
         
            +
                    self.tts_manager = TTSManager()
         
     | 
| 289 | 
         
            +
                    logger.info(f"Using device: {self.device}")
         
     | 
| 290 | 
         
            +
                    logger.info("Initialized with robust TTS system")
         
     | 
| 291 | 
         
            +
                    
         
     | 
| 292 | 
         
            +
                def load_model(self):
         
     | 
| 293 | 
         
            +
                    """Load the OmniAvatar model - now more flexible"""
         
     | 
| 294 | 
         
            +
                    try:
         
     | 
| 295 | 
         
            +
                        # Check if models are downloaded (but don't require them)
         
     | 
| 296 | 
         
            +
                        model_paths = [
         
     | 
| 297 | 
         
            +
                            "./pretrained_models/Wan2.1-T2V-14B",
         
     | 
| 298 | 
         
            +
                            "./pretrained_models/OmniAvatar-14B", 
         
     | 
| 299 | 
         
            +
                            "./pretrained_models/wav2vec2-base-960h"
         
     | 
| 300 | 
         
            +
                        ]
         
     | 
| 301 | 
         
            +
                        
         
     | 
| 302 | 
         
            +
                        missing_models = []
         
     | 
| 303 | 
         
            +
                        for path in model_paths:
         
     | 
| 304 | 
         
            +
                            if not os.path.exists(path):
         
     | 
| 305 | 
         
            +
                                missing_models.append(path)
         
     | 
| 306 | 
         
            +
                        
         
     | 
| 307 | 
         
            +
                        if missing_models:
         
     | 
| 308 | 
         
            +
                            logger.warning("WARNING: Some OmniAvatar models not found:")
         
     | 
| 309 | 
         
            +
                            for model in missing_models:
         
     | 
| 310 | 
         
            +
                                logger.warning(f"   - {model}")
         
     | 
| 311 | 
         
            +
                            logger.info("TIP: App will run in TTS-only mode (no video generation)")
         
     | 
| 312 | 
         
            +
                            logger.info("TIP: To enable full avatar generation, download the required models")
         
     | 
| 313 | 
         
            +
                            
         
     | 
| 314 | 
         
            +
                            # Set as loaded but in limited mode
         
     | 
| 315 | 
         
            +
                            self.model_loaded = False  # Video generation disabled
         
     | 
| 316 | 
         
            +
                            return True  # But app can still run
         
     | 
| 317 | 
         
            +
                        else:
         
     | 
| 318 | 
         
            +
                            self.model_loaded = True
         
     | 
| 319 | 
         
            +
                            logger.info("SUCCESS: All OmniAvatar models found - full functionality enabled")
         
     | 
| 320 | 
         
            +
                            return True
         
     | 
| 321 | 
         
            +
                            
         
     | 
| 322 | 
         
            +
                    except Exception as e:
         
     | 
| 323 | 
         
            +
                        logger.error(f"Error checking models: {str(e)}")
         
     | 
| 324 | 
         
            +
                        logger.info("TIP: Continuing in TTS-only mode")
         
     | 
| 325 | 
         
            +
                        self.model_loaded = False
         
     | 
| 326 | 
         
            +
                        return True  # Continue running
         
     | 
| 327 | 
         
            +
                
         
     | 
| 328 | 
         
            +
                async def download_file(self, url: str, suffix: str = "") -> str:
         
     | 
| 329 | 
         
            +
                    """Download file from URL and save to temporary location"""
         
     | 
| 330 | 
         
            +
                    try:
         
     | 
| 331 | 
         
            +
                        async with aiohttp.ClientSession() as session:
         
     | 
| 332 | 
         
            +
                            async with session.get(str(url)) as response:
         
     | 
| 333 | 
         
            +
                                if response.status != 200:
         
     | 
| 334 | 
         
            +
                                    raise HTTPException(status_code=400, detail=f"Failed to download file from URL: {url}")
         
     | 
| 335 | 
         
            +
                                
         
     | 
| 336 | 
         
            +
                                content = await response.read()
         
     | 
| 337 | 
         
            +
                                
         
     | 
| 338 | 
         
            +
                                # Create temporary file
         
     | 
| 339 | 
         
            +
                                temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
         
     | 
| 340 | 
         
            +
                                temp_file.write(content)
         
     | 
| 341 | 
         
            +
                                temp_file.close()
         
     | 
| 342 | 
         
            +
                                
         
     | 
| 343 | 
         
            +
                                return temp_file.name
         
     | 
| 344 | 
         
            +
                                
         
     | 
| 345 | 
         
            +
                    except aiohttp.ClientError as e:
         
     | 
| 346 | 
         
            +
                        logger.error(f"Network error downloading {url}: {e}")
         
     | 
| 347 | 
         
            +
                        raise HTTPException(status_code=400, detail=f"Network error downloading file: {e}")
         
     | 
| 348 | 
         
            +
                    except Exception as e:
         
     | 
| 349 | 
         
            +
                        logger.error(f"Error downloading file from {url}: {e}")
         
     | 
| 350 | 
         
            +
                        raise HTTPException(status_code=500, detail=f"Error downloading file: {e}")
         
     | 
| 351 | 
         
            +
                
         
     | 
| 352 | 
         
            +
                def validate_audio_url(self, url: str) -> bool:
         
     | 
| 353 | 
         
            +
                    """Validate if URL is likely an audio file"""
         
     | 
| 354 | 
         
            +
                    try:
         
     | 
| 355 | 
         
            +
                        parsed = urlparse(url)
         
     | 
| 356 | 
         
            +
                        # Check for common audio file extensions
         
     | 
| 357 | 
         
            +
                        audio_extensions = ['.mp3', '.wav', '.m4a', '.ogg', '.aac', '.flac']
         
     | 
| 358 | 
         
            +
                        is_audio_ext = any(parsed.path.lower().endswith(ext) for ext in audio_extensions)
         
     | 
| 359 | 
         
            +
                        
         
     | 
| 360 | 
         
            +
                        return is_audio_ext or 'audio' in url.lower()
         
     | 
| 361 | 
         
            +
                    except:
         
     | 
| 362 | 
         
            +
                        return False
         
     | 
| 363 | 
         
            +
                
         
     | 
| 364 | 
         
            +
                def validate_image_url(self, url: str) -> bool:
         
     | 
| 365 | 
         
            +
                    """Validate if URL is likely an image file"""
         
     | 
| 366 | 
         
            +
                    try:
         
     | 
| 367 | 
         
            +
                        parsed = urlparse(url)
         
     | 
| 368 | 
         
            +
                        image_extensions = ['.jpg', '.jpeg', '.png', '.webp', '.bmp', '.gif']
         
     | 
| 369 | 
         
            +
                        return any(parsed.path.lower().endswith(ext) for ext in image_extensions)
         
     | 
| 370 | 
         
            +
                    except:
         
     | 
| 371 | 
         
            +
                        return False
         
     | 
| 372 | 
         
            +
                
         
     | 
| 373 | 
         
            +
                async def generate_avatar(self, request: GenerateRequest) -> tuple[str, float, bool, str]:
         
     | 
| 374 | 
         
            +
                    """Generate avatar VIDEO - PRIMARY FUNCTIONALITY"""
         
     | 
| 375 | 
         
            +
                    import time
         
     | 
| 376 | 
         
            +
                    start_time = time.time()
         
     | 
| 377 | 
         
            +
                    audio_generated = False
         
     | 
| 378 | 
         
            +
                    method_used = "Unknown"
         
     | 
| 379 | 
         
            +
                    
         
     | 
| 380 | 
         
            +
                    logger.info("[VIDEO] STARTING AVATAR VIDEO GENERATION")
         
     | 
| 381 | 
         
            +
                    logger.info(f"[INFO] Prompt: {request.prompt}")
         
     | 
| 382 | 
         
            +
                    
         
     | 
| 383 | 
         
            +
                    if VIDEO_ENGINE_AVAILABLE:
         
     | 
| 384 | 
         
            +
                        try:
         
     | 
| 385 | 
         
            +
                            # PRIORITIZE VIDEO GENERATION
         
     | 
| 386 | 
         
            +
                            logger.info("[TARGET] Using OmniAvatar Video Engine for FULL video generation")
         
     | 
| 387 | 
         
            +
                            
         
     | 
| 388 | 
         
            +
                            # Handle audio source
         
     | 
| 389 | 
         
            +
                            audio_path = None
         
     | 
| 390 | 
         
            +
                            if request.text_to_speech:
         
     | 
| 391 | 
         
            +
                                logger.info("[MIC] Generating audio from text...")
         
     | 
| 392 | 
         
            +
                                audio_path, method_used = await self.tts_manager.text_to_speech(
         
     | 
| 393 | 
         
            +
                                    request.text_to_speech, 
         
     | 
| 394 | 
         
            +
                                    request.voice_id or "21m00Tcm4TlvDq8ikWAM"
         
     | 
| 395 | 
         
            +
                                )
         
     | 
| 396 | 
         
            +
                                audio_generated = True
         
     | 
| 397 | 
         
            +
                            elif request.audio_url:
         
     | 
| 398 | 
         
            +
                                logger.info("๐ฅ Downloading audio from URL...")
         
     | 
| 399 | 
         
            +
                                audio_path = await self.download_file(str(request.audio_url), ".mp3")
         
     | 
| 400 | 
         
            +
                                method_used = "External Audio"
         
     | 
| 401 | 
         
            +
                            else:
         
     | 
| 402 | 
         
            +
                                raise HTTPException(status_code=400, detail="Either text_to_speech or audio_url required for video generation")
         
     | 
| 403 | 
         
            +
                            
         
     | 
| 404 | 
         
            +
                            # Handle image if provided
         
     | 
| 405 | 
         
            +
                            image_path = None
         
     | 
| 406 | 
         
            +
                            if request.image_url:
         
     | 
| 407 | 
         
            +
                                logger.info("[IMAGE] Downloading reference image...")
         
     | 
| 408 | 
         
            +
                                parsed = urlparse(str(request.image_url))
         
     | 
| 409 | 
         
            +
                                ext = os.path.splitext(parsed.path)[1] or ".jpg"
         
     | 
| 410 | 
         
            +
                                image_path = await self.download_file(str(request.image_url), ext)
         
     | 
| 411 | 
         
            +
                            
         
     | 
| 412 | 
         
            +
                            # GENERATE VIDEO using OmniAvatar engine
         
     | 
| 413 | 
         
            +
                            logger.info("[VIDEO] Generating avatar video with adaptive body animation...")
         
     | 
| 414 | 
         
            +
                            video_path, generation_time = video_engine.generate_avatar_video(
         
     | 
| 415 | 
         
            +
                                prompt=request.prompt,
         
     | 
| 416 | 
         
            +
                                audio_path=audio_path,
         
     | 
| 417 | 
         
            +
                                image_path=image_path,
         
     | 
| 418 | 
         
            +
                                guidance_scale=request.guidance_scale,
         
     | 
| 419 | 
         
            +
                                audio_scale=request.audio_scale,
         
     | 
| 420 | 
         
            +
                                num_steps=request.num_steps
         
     | 
| 421 | 
         
            +
                            )
         
     | 
| 422 | 
         
            +
                            
         
     | 
| 423 | 
         
            +
                            processing_time = time.time() - start_time
         
     | 
| 424 | 
         
            +
                            logger.info(f"SUCCESS: VIDEO GENERATED successfully in {processing_time:.1f}s")
         
     | 
| 425 | 
         
            +
                            
         
     | 
| 426 | 
         
            +
                            # Cleanup temporary files
         
     | 
| 427 | 
         
            +
                            if audio_path and os.path.exists(audio_path):
         
     | 
| 428 | 
         
            +
                                os.unlink(audio_path)
         
     | 
| 429 | 
         
            +
                            if image_path and os.path.exists(image_path):
         
     | 
| 430 | 
         
            +
                                os.unlink(image_path)
         
     | 
| 431 | 
         
            +
                            
         
     | 
| 432 | 
         
            +
                            return video_path, processing_time, audio_generated, f"OmniAvatar Video Generation ({method_used})"
         
     | 
| 433 | 
         
            +
                            
         
     | 
| 434 | 
         
            +
                        except Exception as e:
         
     | 
| 435 | 
         
            +
                            logger.error(f"ERROR: Video generation failed: {e}")
         
     | 
| 436 | 
         
            +
                            # For a VIDEO generation app, we should NOT fall back to audio-only
         
     | 
| 437 | 
         
            +
                            # Instead, provide clear guidance
         
     | 
| 438 | 
         
            +
                            if "models" in str(e).lower():
         
     | 
| 439 | 
         
            +
                                raise HTTPException(
         
     | 
| 440 | 
         
            +
                                    status_code=503,
         
     | 
| 441 | 
         
            +
                                    detail=f"Video generation requires OmniAvatar models (~30GB). Please run model download script. Error: {str(e)}"
         
     | 
| 442 | 
         
            +
                                )
         
     | 
| 443 | 
         
            +
                            else:
         
     | 
| 444 | 
         
            +
                                raise HTTPException(status_code=500, detail=f"Video generation failed: {str(e)}")
         
     | 
| 445 | 
         
            +
                    
         
     | 
| 446 | 
         
            +
                    # If video engine not available, this is a critical error for a VIDEO app
         
     | 
| 447 | 
         
            +
                    raise HTTPException(
         
     | 
| 448 | 
         
            +
                        status_code=503, 
         
     | 
| 449 | 
         
            +
                        detail="Video generation engine not available. This application requires OmniAvatar models for video generation."
         
     | 
| 450 | 
         
            +
                    )
         
     | 
| 451 | 
         
            +
             
     | 
| 452 | 
         
            +
                async def generate_avatar_BACKUP(self, request: GenerateRequest) -> tuple[str, float, bool, str]:
         
     | 
| 453 | 
         
            +
                    """OLD TTS-ONLY METHOD - kept as backup reference.
         
     | 
| 454 | 
         
            +
                    Generate avatar video from prompt and audio/text - now handles missing models"""
         
     | 
| 455 | 
         
            +
                    import time
         
     | 
| 456 | 
         
            +
                    start_time = time.time()
         
     | 
| 457 | 
         
            +
                    audio_generated = False
         
     | 
| 458 | 
         
            +
                    tts_method = None
         
     | 
| 459 | 
         
            +
                    
         
     | 
| 460 | 
         
            +
                    try:
         
     | 
| 461 | 
         
            +
                        # Check if video generation is available
         
     | 
| 462 | 
         
            +
                        if not self.model_loaded:
         
     | 
| 463 | 
         
            +
                            logger.info("๐๏ธ Running in TTS-only mode (OmniAvatar models not available)")
         
     | 
| 464 | 
         
            +
                            
         
     | 
| 465 | 
         
            +
                            # Only generate audio, no video
         
     | 
| 466 | 
         
            +
                            if request.text_to_speech:
         
     | 
| 467 | 
         
            +
                                logger.info(f"Generating speech from text: {request.text_to_speech[:50]}...")
         
     | 
| 468 | 
         
            +
                                audio_path, tts_method = await self.tts_manager.text_to_speech(
         
     | 
| 469 | 
         
            +
                                    request.text_to_speech, 
         
     | 
| 470 | 
         
            +
                                    request.voice_id or "21m00Tcm4TlvDq8ikWAM"
         
     | 
| 471 | 
         
            +
                                )
         
     | 
| 472 | 
         
            +
                                
         
     | 
| 473 | 
         
            +
                                # Return the audio file as the "output"
         
     | 
| 474 | 
         
            +
                                processing_time = time.time() - start_time
         
     | 
| 475 | 
         
            +
                                logger.info(f"SUCCESS: TTS completed in {processing_time:.1f}s using {tts_method}")
         
     | 
| 476 | 
         
            +
                                return audio_path, processing_time, True, f"{tts_method} (TTS-only mode)"
         
     | 
| 477 | 
         
            +
                            else:
         
     | 
| 478 | 
         
            +
                                raise HTTPException(
         
     | 
| 479 | 
         
            +
                                    status_code=503,
         
     | 
| 480 | 
         
            +
                                    detail="Video generation unavailable. OmniAvatar models not found. Only TTS from text is supported."
         
     | 
| 481 | 
         
            +
                                )
         
     | 
| 482 | 
         
            +
                        
         
     | 
| 483 | 
         
            +
                        # Original video generation logic (when models are available)
         
     | 
| 484 | 
         
            +
                        # Determine audio source
         
     | 
| 485 | 
         
            +
                        audio_path = None
         
     | 
| 486 | 
         
            +
                        
         
     | 
| 487 | 
         
            +
                        if request.text_to_speech:
         
     | 
| 488 | 
         
            +
                            # Generate speech from text using TTS manager
         
     | 
| 489 | 
         
            +
                            logger.info(f"Generating speech from text: {request.text_to_speech[:50]}...")
         
     | 
| 490 | 
         
            +
                            audio_path, tts_method = await self.tts_manager.text_to_speech(
         
     | 
| 491 | 
         
            +
                                request.text_to_speech, 
         
     | 
| 492 | 
         
            +
                                request.voice_id or "21m00Tcm4TlvDq8ikWAM"
         
     | 
| 493 | 
         
            +
                            )
         
     | 
| 494 | 
         
            +
                            audio_generated = True
         
     | 
| 495 | 
         
            +
                            
         
     | 
| 496 | 
         
            +
                        elif request.audio_url:
         
     | 
| 497 | 
         
            +
                            # Download audio from provided URL
         
     | 
| 498 | 
         
            +
                            logger.info(f"Downloading audio from URL: {request.audio_url}")
         
     | 
| 499 | 
         
            +
                            if not self.validate_audio_url(str(request.audio_url)):
         
     | 
| 500 | 
         
            +
                                logger.warning(f"Audio URL may not be valid: {request.audio_url}")
         
     | 
| 501 | 
         
            +
                            
         
     | 
| 502 | 
         
            +
                            audio_path = await self.download_file(str(request.audio_url), ".mp3")
         
     | 
| 503 | 
         
            +
                            tts_method = "External Audio URL"
         
     | 
| 504 | 
         
            +
                        
         
     | 
| 505 | 
         
            +
                        else:
         
     | 
| 506 | 
         
            +
                            raise HTTPException(
         
     | 
| 507 | 
         
            +
                                status_code=400, 
         
     | 
| 508 | 
         
            +
                                detail="Either text_to_speech or audio_url must be provided"
         
     | 
| 509 | 
         
            +
                            )
         
     | 
| 510 | 
         
            +
                        
         
     | 
| 511 | 
         
            +
                        # Download image if provided
         
     | 
| 512 | 
         
            +
                        image_path = None
         
     | 
| 513 | 
         
            +
                        if request.image_url:
         
     | 
| 514 | 
         
            +
                            logger.info(f"Downloading image from URL: {request.image_url}")
         
     | 
| 515 | 
         
            +
                            if not self.validate_image_url(str(request.image_url)):
         
     | 
| 516 | 
         
            +
                                logger.warning(f"Image URL may not be valid: {request.image_url}")
         
     | 
| 517 | 
         
            +
                            
         
     | 
| 518 | 
         
            +
                            # Determine image extension from URL or default to .jpg
         
     | 
| 519 | 
         
            +
                            parsed = urlparse(str(request.image_url))
         
     | 
| 520 | 
         
            +
                            ext = os.path.splitext(parsed.path)[1] or ".jpg"
         
     | 
| 521 | 
         
            +
                            image_path = await self.download_file(str(request.image_url), ext)
         
     | 
| 522 | 
         
            +
                        
         
     | 
| 523 | 
         
            +
                        # Create temporary input file for inference
         
     | 
| 524 | 
         
            +
                        with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as f:
         
     | 
| 525 | 
         
            +
                            if image_path:
         
     | 
| 526 | 
         
            +
                                input_line = f"{request.prompt}@@{image_path}@@{audio_path}"
         
     | 
| 527 | 
         
            +
                            else:
         
     | 
| 528 | 
         
            +
                                input_line = f"{request.prompt}@@@@{audio_path}"
         
     | 
| 529 | 
         
            +
                            f.write(input_line)
         
     | 
| 530 | 
         
            +
                            temp_input_file = f.name
         
     | 
| 531 | 
         
            +
                        
         
     | 
| 532 | 
         
            +
                        # Prepare inference command
         
     | 
| 533 | 
         
            +
                        cmd = [
         
     | 
| 534 | 
         
            +
                            "python", "-m", "torch.distributed.run",
         
     | 
| 535 | 
         
            +
                            "--standalone", f"--nproc_per_node={request.sp_size}",
         
     | 
| 536 | 
         
            +
                            "scripts/inference.py",
         
     | 
| 537 | 
         
            +
                            "--config", "configs/inference.yaml",
         
     | 
| 538 | 
         
            +
                            "--input_file", temp_input_file,
         
     | 
| 539 | 
         
            +
                            "--guidance_scale", str(request.guidance_scale),
         
     | 
| 540 | 
         
            +
                            "--audio_scale", str(request.audio_scale),
         
     | 
| 541 | 
         
            +
                            "--num_steps", str(request.num_steps)
         
     | 
| 542 | 
         
            +
                        ]
         
     | 
| 543 | 
         
            +
                        
         
     | 
| 544 | 
         
            +
                        if request.tea_cache_l1_thresh:
         
     | 
| 545 | 
         
            +
                            cmd.extend(["--tea_cache_l1_thresh", str(request.tea_cache_l1_thresh)])
         
     | 
| 546 | 
         
            +
                        
         
     | 
| 547 | 
         
            +
                        logger.info(f"Running inference with command: {' '.join(cmd)}")
         
     | 
| 548 | 
         
            +
                        
         
     | 
| 549 | 
         
            +
                        # Run inference
         
     | 
| 550 | 
         
            +
                        result = subprocess.run(cmd, capture_output=True, text=True)
         
     | 
| 551 | 
         
            +
                        
         
     | 
| 552 | 
         
            +
                        # Clean up temporary files
         
     | 
| 553 | 
         
            +
                        os.unlink(temp_input_file)
         
     | 
| 554 | 
         
            +
                        os.unlink(audio_path)
         
     | 
| 555 | 
         
            +
                        if image_path:
         
     | 
| 556 | 
         
            +
                            os.unlink(image_path)
         
     | 
| 557 | 
         
            +
                        
         
     | 
| 558 | 
         
            +
                        if result.returncode != 0:
         
     | 
| 559 | 
         
            +
                            logger.error(f"Inference failed: {result.stderr}")
         
     | 
| 560 | 
         
            +
                            raise Exception(f"Inference failed: {result.stderr}")
         
     | 
| 561 | 
         
            +
                        
         
     | 
| 562 | 
         
            +
                        # Find output video file
         
     | 
| 563 | 
         
            +
                        output_dir = "./outputs"
         
     | 
| 564 | 
         
            +
                        if os.path.exists(output_dir):
         
     | 
| 565 | 
         
            +
                            video_files = [f for f in os.listdir(output_dir) if f.endswith(('.mp4', '.avi'))]
         
     | 
| 566 | 
         
            +
                            if video_files:
         
     | 
| 567 | 
         
            +
                                # Return the most recent video file
         
     | 
| 568 | 
         
            +
                                video_files.sort(key=lambda x: os.path.getmtime(os.path.join(output_dir, x)), reverse=True)
         
     | 
| 569 | 
         
            +
                                output_path = os.path.join(output_dir, video_files[0])
         
     | 
| 570 | 
         
            +
                                processing_time = time.time() - start_time
         
     | 
| 571 | 
         
            +
                                return output_path, processing_time, audio_generated, tts_method
         
     | 
| 572 | 
         
            +
                        
         
     | 
| 573 | 
         
            +
                        raise Exception("No output video generated")
         
     | 
| 574 | 
         
            +
                        
         
     | 
| 575 | 
         
            +
                    except Exception as e:
         
     | 
| 576 | 
         
            +
                        # Clean up any temporary files in case of error
         
     | 
| 577 | 
         
            +
                        try:
         
     | 
| 578 | 
         
            +
                            if 'audio_path' in locals() and audio_path and os.path.exists(audio_path):
         
     | 
| 579 | 
         
            +
                                os.unlink(audio_path)
         
     | 
| 580 | 
         
            +
                            if 'image_path' in locals() and image_path and os.path.exists(image_path):
         
     | 
| 581 | 
         
            +
                                os.unlink(image_path)
         
     | 
| 582 | 
         
            +
                            if 'temp_input_file' in locals() and os.path.exists(temp_input_file):
         
     | 
| 583 | 
         
            +
                                os.unlink(temp_input_file)
         
     | 
| 584 | 
         
            +
                        except:
         
     | 
| 585 | 
         
            +
                            pass
         
     | 
| 586 | 
         
            +
                        
         
     | 
| 587 | 
         
            +
                        logger.error(f"Generation error: {str(e)}")
         
     | 
| 588 | 
         
            +
                        raise HTTPException(status_code=500, detail=str(e))
         
     | 
| 589 | 
         
            +
             
     | 
| 590 | 
         
            +
            # Initialize API
         
     | 
| 591 | 
         
            +
            omni_api = OmniAvatarAPI()
         
     | 
| 592 | 
         
            +
             
     | 
| 593 | 
         
            +
            # Use FastAPI lifespan instead of deprecated on_event
         
     | 
| 594 | 
         
            +
            from contextlib import asynccontextmanager
         
     | 
| 595 | 
         
            +
             
     | 
| 596 | 
         
            +
            @asynccontextmanager
         
     | 
| 597 | 
         
            +
            async def lifespan(app: FastAPI):
         
     | 
| 598 | 
         
            +
                # Startup
         
     | 
| 599 | 
         
            +
                success = omni_api.load_model()
         
     | 
| 600 | 
         
            +
                if not success:
         
     | 
| 601 | 
         
            +
                    logger.warning("WARNING: OmniAvatar model loading failed - running in limited mode")
         
     | 
| 602 | 
         
            +
                
         
     | 
| 603 | 
         
            +
                # Load TTS models
         
     | 
| 604 | 
         
            +
                try:
         
     | 
| 605 | 
         
            +
                    await omni_api.tts_manager.load_models()
         
     | 
| 606 | 
         
            +
                    logger.info("SUCCESS: TTS models initialization completed")
         
     | 
| 607 | 
         
            +
                except Exception as e:
         
     | 
| 608 | 
         
            +
                    logger.error(f"ERROR: TTS initialization failed: {e}")
         
     | 
| 609 | 
         
            +
                
         
     | 
| 610 | 
         
            +
                yield
         
     | 
| 611 | 
         
            +
                
         
     | 
| 612 | 
         
            +
                # Shutdown (if needed)
         
     | 
| 613 | 
         
            +
                logger.info("Application shutting down...")
         
     | 
| 614 | 
         
            +
             
     | 
| 615 | 
         
            +
            # Create FastAPI app WITH lifespan parameter
         
     | 
| 616 | 
         
            +
            app = FastAPI(
         
     | 
| 617 | 
         
            +
                title="OmniAvatar-14B API with Advanced TTS", 
         
     | 
| 618 | 
         
            +
                version="1.0.0",
         
     | 
| 619 | 
         
            +
                lifespan=lifespan
         
     | 
| 620 | 
         
            +
            )
         
     | 
| 621 | 
         
            +
             
     | 
| 622 | 
         
            +
            # Add CORS middleware
         
     | 
| 623 | 
         
            +
            app.add_middleware(
         
     | 
| 624 | 
         
            +
                CORSMiddleware,
         
     | 
| 625 | 
         
            +
                allow_origins=["*"],
         
     | 
| 626 | 
         
            +
                allow_credentials=True,
         
     | 
| 627 | 
         
            +
                allow_methods=["*"],
         
     | 
| 628 | 
         
            +
                allow_headers=["*"],
         
     | 
| 629 | 
         
            +
            )
         
     | 
| 630 | 
         
            +
             
     | 
| 631 | 
         
            +
            # Mount static files for serving generated videos
         
     | 
| 632 | 
         
            +
            app.mount("/outputs", StaticFiles(directory="outputs"), name="outputs")
         
     | 
| 633 | 
         
            +
             
     | 
| 634 | 
         
            +
            @app.get("/health")
         
     | 
| 635 | 
         
            +
            async def health_check():
         
     | 
| 636 | 
         
            +
                """Health check endpoint"""
         
     | 
| 637 | 
         
            +
                tts_info = omni_api.tts_manager.get_tts_info()
         
     | 
| 638 | 
         
            +
                
         
     | 
| 639 | 
         
            +
                return {
         
     | 
| 640 | 
         
            +
                    "status": "healthy",
         
     | 
| 641 | 
         
            +
                    "model_loaded": omni_api.model_loaded,
         
     | 
| 642 | 
         
            +
                    "video_generation_available": omni_api.model_loaded,
         
     | 
| 643 | 
         
            +
                    "tts_only_mode": not omni_api.model_loaded,
         
     | 
| 644 | 
         
            +
                    "device": omni_api.device,
         
     | 
| 645 | 
         
            +
                    "supports_text_to_speech": True,
         
     | 
| 646 | 
         
            +
                    "supports_image_urls": omni_api.model_loaded,
         
     | 
| 647 | 
         
            +
                    "supports_audio_urls": omni_api.model_loaded,
         
     | 
| 648 | 
         
            +
                    "tts_system": "Advanced TTS with Robust Fallback",
         
     | 
| 649 | 
         
            +
                    "advanced_tts_available": ADVANCED_TTS_AVAILABLE,
         
     | 
| 650 | 
         
            +
                    "robust_tts_available": ROBUST_TTS_AVAILABLE,
         
     | 
| 651 | 
         
            +
                    **tts_info
         
     | 
| 652 | 
         
            +
                }
         
     | 
| 653 | 
         
            +
             
     | 
| 654 | 
         
            +
            @app.get("/voices")
         
     | 
| 655 | 
         
            +
            async def get_voices():
         
     | 
| 656 | 
         
            +
                """Get available voice configurations"""
         
     | 
| 657 | 
         
            +
                try:
         
     | 
| 658 | 
         
            +
                    voices = await omni_api.tts_manager.get_available_voices()
         
     | 
| 659 | 
         
            +
                    return {"voices": voices}
         
     | 
| 660 | 
         
            +
                except Exception as e:
         
     | 
| 661 | 
         
            +
                    logger.error(f"Error getting voices: {e}")
         
     | 
| 662 | 
         
            +
                    return {"error": str(e)}
         
     | 
| 663 | 
         
            +
             
     | 
| 664 | 
         
            +
            @app.post("/generate", response_model=GenerateResponse)
         
     | 
| 665 | 
         
            +
            async def generate_avatar(request: GenerateRequest):
         
     | 
| 666 | 
         
            +
                """Generate avatar video from prompt, text/audio, and optional image URL"""
         
     | 
| 667 | 
         
            +
                
         
     | 
| 668 | 
         
            +
                logger.info(f"Generating avatar with prompt: {request.prompt}")
         
     | 
| 669 | 
         
            +
                if request.text_to_speech:
         
     | 
| 670 | 
         
            +
                    logger.info(f"Text to speech: {request.text_to_speech[:100]}...")
         
     | 
| 671 | 
         
            +
                    logger.info(f"Voice ID: {request.voice_id}")
         
     | 
| 672 | 
         
            +
                if request.audio_url:
         
     | 
| 673 | 
         
            +
                    logger.info(f"Audio URL: {request.audio_url}")
         
     | 
| 674 | 
         
            +
                if request.image_url:
         
     | 
| 675 | 
         
            +
                    logger.info(f"Image URL: {request.image_url}")
         
     | 
| 676 | 
         
            +
                
         
     | 
| 677 | 
         
            +
                try:
         
     | 
| 678 | 
         
            +
                    output_path, processing_time, audio_generated, tts_method = await omni_api.generate_avatar(request)
         
     | 
| 679 | 
         
            +
                    
         
     | 
| 680 | 
         
            +
                    return GenerateResponse(
         
     | 
| 681 | 
         
            +
                        message="Generation completed successfully" + (" (TTS-only mode)" if not omni_api.model_loaded else ""),
         
     | 
| 682 | 
         
            +
                        output_path=get_video_url(output_path) if omni_api.model_loaded else output_path,
         
     | 
| 683 | 
         
            +
                        processing_time=processing_time,
         
     | 
| 684 | 
         
            +
                        audio_generated=audio_generated,
         
     | 
| 685 | 
         
            +
                        tts_method=tts_method
         
     | 
| 686 | 
         
            +
                    )
         
     | 
| 687 | 
         
            +
                    
         
     | 
| 688 | 
         
            +
                except HTTPException:
         
     | 
| 689 | 
         
            +
                    raise
         
     | 
| 690 | 
         
            +
                except Exception as e:
         
     | 
| 691 | 
         
            +
                    logger.error(f"Unexpected error: {e}")
         
     | 
| 692 | 
         
            +
                    raise HTTPException(status_code=500, detail=f"Unexpected error: {e}")
         
     | 
| 693 | 
         
            +
             
     | 
| 694 | 
         
            +
            @app.post("/download-models")
         
     | 
| 695 | 
         
            +
            async def download_video_models():
         
     | 
| 696 | 
         
            +
                """Manually trigger video model downloads"""
         
     | 
| 697 | 
         
            +
                logger.info("?? Manual model download requested...")
         
     | 
| 698 | 
         
            +
                
         
     | 
| 699 | 
         
            +
                try:
         
     | 
| 700 | 
         
            +
                    from huggingface_hub import snapshot_download
         
     | 
| 701 | 
         
            +
                    import shutil
         
     | 
| 702 | 
         
            +
                    
         
     | 
| 703 | 
         
            +
                    # Check storage first
         
     | 
| 704 | 
         
            +
                    _, _, free_bytes = shutil.disk_usage(".")
         
     | 
| 705 | 
         
            +
                    free_gb = free_bytes / (1024**3)
         
     | 
| 706 | 
         
            +
                    
         
     | 
| 707 | 
         
            +
                    logger.info(f"?? Available storage: {free_gb:.1f}GB")
         
     | 
| 708 | 
         
            +
                    
         
     | 
| 709 | 
         
            +
                    if free_gb < 10:  # Need at least 10GB free
         
     | 
| 710 | 
         
            +
                        return {
         
     | 
| 711 | 
         
            +
                            "success": False,
         
     | 
| 712 | 
         
            +
                            "message": f"Insufficient storage: {free_gb:.1f}GB available, 10GB+ required",
         
     | 
| 713 | 
         
            +
                            "storage_gb": free_gb
         
     | 
| 714 | 
         
            +
                        }
         
     | 
| 715 | 
         
            +
                    
         
     | 
| 716 | 
         
            +
                    # Download small video generation model
         
     | 
| 717 | 
         
            +
                    logger.info("?? Downloading text-to-video model...")
         
     | 
| 718 | 
         
            +
                    
         
     | 
| 719 | 
         
            +
                    model_path = snapshot_download(
         
     | 
| 720 | 
         
            +
                        repo_id="ali-vilab/text-to-video-ms-1.7b",
         
     | 
| 721 | 
         
            +
                        cache_dir="./downloaded_models/video",
         
     | 
| 722 | 
         
            +
                        local_files_only=False
         
     | 
| 723 | 
         
            +
                    )
         
     | 
| 724 | 
         
            +
                    
         
     | 
| 725 | 
         
            +
                    logger.info(f"? Video model downloaded: {model_path}")
         
     | 
| 726 | 
         
            +
                    
         
     | 
| 727 | 
         
            +
                    # Download audio model
         
     | 
| 728 | 
         
            +
                    audio_model_path = snapshot_download(
         
     | 
| 729 | 
         
            +
                        repo_id="facebook/wav2vec2-base-960h", 
         
     | 
| 730 | 
         
            +
                        cache_dir="./downloaded_models/audio",
         
     | 
| 731 | 
         
            +
                        local_files_only=False
         
     | 
| 732 | 
         
            +
                    )
         
     | 
| 733 | 
         
            +
                    
         
     | 
| 734 | 
         
            +
                    logger.info(f"? Audio model downloaded: {audio_model_path}")
         
     | 
| 735 | 
         
            +
                    
         
     | 
| 736 | 
         
            +
                    # Check final storage usage
         
     | 
| 737 | 
         
            +
                    _, _, free_bytes_after = shutil.disk_usage(".")
         
     | 
| 738 | 
         
            +
                    free_gb_after = free_bytes_after / (1024**3)
         
     | 
| 739 | 
         
            +
                    used_gb = free_gb - free_gb_after
         
     | 
| 740 | 
         
            +
                    
         
     | 
| 741 | 
         
            +
                    return {
         
     | 
| 742 | 
         
            +
                        "success": True,
         
     | 
| 743 | 
         
            +
                        "message": "? Video generation models downloaded successfully!",
         
     | 
| 744 | 
         
            +
                        "models_downloaded": [
         
     | 
| 745 | 
         
            +
                            "ali-vilab/text-to-video-ms-1.7b",
         
     | 
| 746 | 
         
            +
                            "facebook/wav2vec2-base-960h"
         
     | 
| 747 | 
         
            +
                        ],
         
     | 
| 748 | 
         
            +
                        "storage_used_gb": round(used_gb, 2),
         
     | 
| 749 | 
         
            +
                        "storage_remaining_gb": round(free_gb_after, 2),
         
     | 
| 750 | 
         
            +
                        "video_model_path": model_path,
         
     | 
| 751 | 
         
            +
                        "audio_model_path": audio_model_path,
         
     | 
| 752 | 
         
            +
                        "status": "READY FOR VIDEO GENERATION"
         
     | 
| 753 | 
         
            +
                    }
         
     | 
| 754 | 
         
            +
                    
         
     | 
| 755 | 
         
            +
                except Exception as e:
         
     | 
| 756 | 
         
            +
                    logger.error(f"? Model download failed: {e}")
         
     | 
| 757 | 
         
            +
                    return {
         
     | 
| 758 | 
         
            +
                        "success": False,
         
     | 
| 759 | 
         
            +
                        "message": f"Model download failed: {str(e)}",
         
     | 
| 760 | 
         
            +
                        "error": str(e)
         
     | 
| 761 | 
         
            +
                    }
         
     | 
| 762 | 
         
            +
             
     | 
| 763 | 
         
            +
            @app.get("/model-status")
         
     | 
| 764 | 
         
            +
            async def get_model_status():
         
     | 
| 765 | 
         
            +
                """Check status of downloaded models"""
         
     | 
| 766 | 
         
            +
                try:
         
     | 
| 767 | 
         
            +
                    models_dir = Path("./downloaded_models")
         
     | 
| 768 | 
         
            +
                    
         
     | 
| 769 | 
         
            +
                    status = {
         
     | 
| 770 | 
         
            +
                        "models_downloaded": models_dir.exists(),
         
     | 
| 771 | 
         
            +
                        "available_models": [],
         
     | 
| 772 | 
         
            +
                        "storage_info": {}
         
     | 
| 773 | 
         
            +
                    }
         
     | 
| 774 | 
         
            +
                    
         
     | 
| 775 | 
         
            +
                    if models_dir.exists():
         
     | 
| 776 | 
         
            +
                        for model_dir in models_dir.iterdir():
         
     | 
| 777 | 
         
            +
                            if model_dir.is_dir():
         
     | 
| 778 | 
         
            +
                                status["available_models"].append({
         
     | 
| 779 | 
         
            +
                                    "name": model_dir.name,
         
     | 
| 780 | 
         
            +
                                    "path": str(model_dir),
         
     | 
| 781 | 
         
            +
                                    "files": len(list(model_dir.rglob("*")))
         
     | 
| 782 | 
         
            +
                                })
         
     | 
| 783 | 
         
            +
                    
         
     | 
| 784 | 
         
            +
                    # Storage info
         
     | 
| 785 | 
         
            +
                    import shutil
         
     | 
| 786 | 
         
            +
                    _, _, free_bytes = shutil.disk_usage(".")
         
     | 
| 787 | 
         
            +
                    status["storage_info"] = {
         
     | 
| 788 | 
         
            +
                        "free_gb": round(free_bytes / (1024**3), 2),
         
     | 
| 789 | 
         
            +
                        "models_dir_exists": models_dir.exists()
         
     | 
| 790 | 
         
            +
                    }
         
     | 
| 791 | 
         
            +
                    
         
     | 
| 792 | 
         
            +
                    return status
         
     | 
| 793 | 
         
            +
                    
         
     | 
| 794 | 
         
            +
                except Exception as e:
         
     | 
| 795 | 
         
            +
                    return {"error": str(e)}
         
     | 
| 796 | 
         
            +
             
     | 
| 797 | 
         
            +
             
     | 
| 798 | 
         
            +
            # Enhanced Gradio interface
         
     | 
| 799 | 
         
            +
            def gradio_generate(prompt, text_to_speech, audio_url, image_url, voice_id, guidance_scale, audio_scale, num_steps):
         
     | 
| 800 | 
         
            +
                """Gradio interface wrapper with robust TTS support"""
         
     | 
| 801 | 
         
            +
                try:
         
     | 
| 802 | 
         
            +
                    # Create request object
         
     | 
| 803 | 
         
            +
                    request_data = {
         
     | 
| 804 | 
         
            +
                        "prompt": prompt,
         
     | 
| 805 | 
         
            +
                        "guidance_scale": guidance_scale,
         
     | 
| 806 | 
         
            +
                        "audio_scale": audio_scale,
         
     | 
| 807 | 
         
            +
                        "num_steps": int(num_steps)
         
     | 
| 808 | 
         
            +
                    }
         
     | 
| 809 | 
         
            +
                    
         
     | 
| 810 | 
         
            +
                    # Add audio source
         
     | 
| 811 | 
         
            +
                    if text_to_speech and text_to_speech.strip():
         
     | 
| 812 | 
         
            +
                        request_data["text_to_speech"] = text_to_speech
         
     | 
| 813 | 
         
            +
                        request_data["voice_id"] = voice_id or "21m00Tcm4TlvDq8ikWAM"
         
     | 
| 814 | 
         
            +
                    elif audio_url and audio_url.strip():
         
     | 
| 815 | 
         
            +
                        if omni_api.model_loaded:
         
     | 
| 816 | 
         
            +
                            request_data["audio_url"] = audio_url
         
     | 
| 817 | 
         
            +
                        else:
         
     | 
| 818 | 
         
            +
                            return "Error: Audio URL input requires full OmniAvatar models. Please use text-to-speech instead."
         
     | 
| 819 | 
         
            +
                    else:
         
     | 
| 820 | 
         
            +
                        return "Error: Please provide either text to speech or audio URL"
         
     | 
| 821 | 
         
            +
                    
         
     | 
| 822 | 
         
            +
                    if image_url and image_url.strip():
         
     | 
| 823 | 
         
            +
                        if omni_api.model_loaded:
         
     | 
| 824 | 
         
            +
                            request_data["image_url"] = image_url
         
     | 
| 825 | 
         
            +
                        else:
         
     | 
| 826 | 
         
            +
                            return "Error: Image URL input requires full OmniAvatar models for video generation."
         
     | 
| 827 | 
         
            +
                    
         
     | 
| 828 | 
         
            +
                    request = GenerateRequest(**request_data)
         
     | 
| 829 | 
         
            +
                    
         
     | 
| 830 | 
         
            +
                    # Run async function in sync context
         
     | 
| 831 | 
         
            +
                    loop = asyncio.new_event_loop()
         
     | 
| 832 | 
         
            +
                    asyncio.set_event_loop(loop)
         
     | 
| 833 | 
         
            +
                    output_path, processing_time, audio_generated, tts_method = loop.run_until_complete(omni_api.generate_avatar(request))
         
     | 
| 834 | 
         
            +
                    loop.close()
         
     | 
| 835 | 
         
            +
                    
         
     | 
| 836 | 
         
            +
                    success_message = f"SUCCESS: Generation completed in {processing_time:.1f}s using {tts_method}"
         
     | 
| 837 | 
         
            +
                    print(success_message)
         
     | 
| 838 | 
         
            +
                    
         
     | 
| 839 | 
         
            +
                    if omni_api.model_loaded:
         
     | 
| 840 | 
         
            +
                        return output_path
         
     | 
| 841 | 
         
            +
                    else:
         
     | 
| 842 | 
         
            +
                        return f"๐๏ธ TTS Audio generated successfully using {tts_method}\nFile: {output_path}\n\nWARNING: Video generation unavailable (OmniAvatar models not found)"
         
     | 
| 843 | 
         
            +
                    
         
     | 
| 844 | 
         
            +
                except Exception as e:
         
     | 
| 845 | 
         
            +
                    logger.error(f"Gradio generation error: {e}")
         
     | 
| 846 | 
         
            +
                    return f"Error: {str(e)}"
         
     | 
| 847 | 
         
            +
             
     | 
| 848 | 
         
            +
            # Create Gradio interface
         
     | 
| 849 | 
         
            +
            mode_info = " (TTS-Only Mode)" if not omni_api.model_loaded else ""
         
     | 
| 850 | 
         
            +
            description_extra = """
         
     | 
| 851 | 
         
            +
            WARNING: Running in TTS-Only Mode - OmniAvatar models not found. Only text-to-speech generation is available.
         
     | 
| 852 | 
         
            +
            To enable full video generation, the required model files need to be downloaded.
         
     | 
| 853 | 
         
            +
            """ if not omni_api.model_loaded else ""
         
     | 
| 854 | 
         
            +
             
     | 
| 855 | 
         
            +
            iface = gr.Interface(
         
     | 
| 856 | 
         
            +
                fn=gradio_generate,
         
     | 
| 857 | 
         
            +
                inputs=[
         
     | 
| 858 | 
         
            +
                    gr.Textbox(
         
     | 
| 859 | 
         
            +
                        label="Prompt", 
         
     | 
| 860 | 
         
            +
                        placeholder="Describe the character behavior (e.g., 'A friendly person explaining a concept')",
         
     | 
| 861 | 
         
            +
                        lines=2
         
     | 
| 862 | 
         
            +
                    ),
         
     | 
| 863 | 
         
            +
                    gr.Textbox(
         
     | 
| 864 | 
         
            +
                        label="Text to Speech", 
         
     | 
| 865 | 
         
            +
                        placeholder="Enter text to convert to speech",
         
     | 
| 866 | 
         
            +
                        lines=3,
         
     | 
| 867 | 
         
            +
                        info="Will use best available TTS system (Advanced or Fallback)"
         
     | 
| 868 | 
         
            +
                    ),
         
     | 
| 869 | 
         
            +
                    gr.Textbox(
         
     | 
| 870 | 
         
            +
                        label="OR Audio URL", 
         
     | 
| 871 | 
         
            +
                        placeholder="https://example.com/audio.mp3",
         
     | 
| 872 | 
         
            +
                        info="Direct URL to audio file (requires full models)" if not omni_api.model_loaded else "Direct URL to audio file"
         
     | 
| 873 | 
         
            +
                    ),
         
     | 
| 874 | 
         
            +
                    gr.Textbox(
         
     | 
| 875 | 
         
            +
                        label="Image URL (Optional)", 
         
     | 
| 876 | 
         
            +
                        placeholder="https://example.com/image.jpg",
         
     | 
| 877 | 
         
            +
                        info="Direct URL to reference image (requires full models)" if not omni_api.model_loaded else "Direct URL to reference image"
         
     | 
| 878 | 
         
            +
                    ),
         
     | 
| 879 | 
         
            +
                    gr.Dropdown(
         
     | 
| 880 | 
         
            +
                        choices=[
         
     | 
| 881 | 
         
            +
                            "21m00Tcm4TlvDq8ikWAM", 
         
     | 
| 882 | 
         
            +
                            "pNInz6obpgDQGcFmaJgB", 
         
     | 
| 883 | 
         
            +
                            "EXAVITQu4vr4xnSDxMaL",
         
     | 
| 884 | 
         
            +
                            "ErXwobaYiN019PkySvjV",
         
     | 
| 885 | 
         
            +
                            "TxGEqnHWrfGW9XjX",
         
     | 
| 886 | 
         
            +
                            "yoZ06aMxZJJ28mfd3POQ",
         
     | 
| 887 | 
         
            +
                            "AZnzlk1XvdvUeBnXmlld"
         
     | 
| 888 | 
         
            +
                        ],
         
     | 
| 889 | 
         
            +
                        value="21m00Tcm4TlvDq8ikWAM",
         
     | 
| 890 | 
         
            +
                        label="Voice Profile",
         
     | 
| 891 | 
         
            +
                        info="Choose voice characteristics for TTS generation"
         
     | 
| 892 | 
         
            +
                    ),
         
     | 
| 893 | 
         
            +
                    gr.Slider(minimum=1, maximum=10, value=5.0, label="Guidance Scale", info="4-6 recommended"),
         
     | 
| 894 | 
         
            +
                    gr.Slider(minimum=1, maximum=10, value=3.0, label="Audio Scale", info="Higher values = better lip-sync"),
         
     | 
| 895 | 
         
            +
                    gr.Slider(minimum=10, maximum=100, value=30, step=1, label="Number of Steps", info="20-50 recommended")
         
     | 
| 896 | 
         
            +
                ],
         
     | 
| 897 | 
         
            +
                outputs=gr.Video(label="Generated Avatar Video") if omni_api.model_loaded else gr.Textbox(label="TTS Output"),
         
     | 
| 898 | 
         
            +
                title="[VIDEO] OmniAvatar-14B - Avatar Video Generation with Adaptive Body Animation",
         
     | 
| 899 | 
         
            +
                description=f"""
         
     | 
| 900 | 
         
            +
                Generate avatar videos with lip-sync from text prompts and speech using robust TTS system.
         
     | 
| 901 | 
         
            +
                
         
     | 
| 902 | 
         
            +
                {description_extra}
         
     | 
| 903 | 
         
            +
                
         
     | 
| 904 | 
         
            +
                **Robust TTS Architecture**
         
     | 
| 905 | 
         
            +
                - **Primary**: Advanced TTS (Facebook VITS & SpeechT5) if available
         
     | 
| 906 | 
         
            +
                - **Fallback**: Robust tone generation for 100% reliability
         
     | 
| 907 | 
         
            +
                - **Automatic**: Seamless switching between methods
         
     | 
| 908 | 
         
            +
                
         
     | 
| 909 | 
         
            +
                **Features:**
         
     | 
| 910 | 
         
            +
                - **Guaranteed Generation**: Always produces audio output
         
     | 
| 911 | 
         
            +
                - **No Dependencies**: Works even without advanced models
         
     | 
| 912 | 
         
            +
                - **High Availability**: Multiple fallback layers
         
     | 
| 913 | 
         
            +
                - **Voice Profiles**: Multiple voice characteristics
         
     | 
| 914 | 
         
            +
                - **Audio URL Support**: Use external audio files {"(full models required)" if not omni_api.model_loaded else ""}
         
     | 
| 915 | 
         
            +
                - **Image URL Support**: Reference images for characters {"(full models required)" if not omni_api.model_loaded else ""}
         
     | 
| 916 | 
         
            +
                
         
     | 
| 917 | 
         
            +
                **Usage:**
         
     | 
| 918 | 
         
            +
                1. Enter a character description in the prompt
         
     | 
| 919 | 
         
            +
                2. **Enter text for speech generation** (recommended in current mode)
         
     | 
| 920 | 
         
            +
                3. {"Optionally add reference image/audio URLs (requires full models)" if not omni_api.model_loaded else "Optionally add reference image URL and choose audio source"}
         
     | 
| 921 | 
         
            +
                4. Choose voice profile and adjust parameters
         
     | 
| 922 | 
         
            +
                5. Generate your {"audio" if not omni_api.model_loaded else "avatar video"}!
         
     | 
| 923 | 
         
            +
                """,
         
     | 
| 924 | 
         
            +
                examples=[
         
     | 
| 925 | 
         
            +
                    [
         
     | 
| 926 | 
         
            +
                        "A professional teacher explaining a mathematical concept with clear gestures",
         
     | 
| 927 | 
         
            +
                        "Hello students! Today we're going to learn about calculus and derivatives.",
         
     | 
| 928 | 
         
            +
                        "",
         
     | 
| 929 | 
         
            +
                        "",
         
     | 
| 930 | 
         
            +
                        "21m00Tcm4TlvDq8ikWAM",
         
     | 
| 931 | 
         
            +
                        5.0,
         
     | 
| 932 | 
         
            +
                        3.5,
         
     | 
| 933 | 
         
            +
                        30
         
     | 
| 934 | 
         
            +
                    ],
         
     | 
| 935 | 
         
            +
                    [
         
     | 
| 936 | 
         
            +
                        "A friendly presenter speaking confidently to an audience",
         
     | 
| 937 | 
         
            +
                        "Welcome everyone to our presentation on artificial intelligence!",
         
     | 
| 938 | 
         
            +
                        "",
         
     | 
| 939 | 
         
            +
                        "",
         
     | 
| 940 | 
         
            +
                        "pNInz6obpgDQGcFmaJgB", 
         
     | 
| 941 | 
         
            +
                        5.5,
         
     | 
| 942 | 
         
            +
                        4.0,
         
     | 
| 943 | 
         
            +
                        35
         
     | 
| 944 | 
         
            +
                    ]
         
     | 
| 945 | 
         
            +
                ],
         
     | 
| 946 | 
         
            +
                allow_flagging="never",
         
     | 
| 947 | 
         
            +
                flagging_dir="/tmp/gradio_flagged"
         
     | 
| 948 | 
         
            +
            )
         
     | 
| 949 | 
         
            +
             
     | 
| 950 | 
         
            +
            # Mount Gradio app
         
     | 
| 951 | 
         
            +
            app = gr.mount_gradio_app(app, iface, path="/gradio")
         
     | 
| 952 | 
         
            +
             
     | 
| 953 | 
         
            +
            if __name__ == "__main__":
         
     | 
| 954 | 
         
            +
                import uvicorn
         
     | 
| 955 | 
         
            +
                uvicorn.run(app, host="0.0.0.0", port=7860)
         
     | 
| 956 | 
         
            +
             
     | 
| 957 | 
         
            +
             
     | 
| 958 | 
         
            +
             
     | 
| 959 | 
         
            +
             
     | 
| 960 | 
         
            +
             
     | 
| 961 | 
         
            +
             
     | 
| 962 | 
         
            +
             
     | 
| 963 | 
         
            +
             
     | 
| 964 | 
         
            +
             
     | 
| 965 | 
         
            +
             
     | 
| 966 | 
         
            +
             
     | 
| 967 | 
         
            +
             
     | 
| 968 | 
         
            +
             
     | 
| 969 | 
         
            +
             
     | 
| 970 | 
         
            +
             
     | 
| 971 | 
         
            +
             
     | 
| 972 | 
         
            +
             
     | 
| 973 | 
         
            +
             
     | 
| 
         @@ -0,0 +1,34 @@ 
     | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
| 
         | 
|
| 1 | 
         
            +
            def download_models_interface():
         
     | 
| 2 | 
         
            +
                """Gradio interface function to download models"""
         
     | 
| 3 | 
         
            +
                try:
         
     | 
| 4 | 
         
            +
                    import requests
         
     | 
| 5 | 
         
            +
                    import time
         
     | 
| 6 | 
         
            +
                    
         
     | 
| 7 | 
         
            +
                    # Trigger model download via API
         
     | 
| 8 | 
         
            +
                    response = requests.post("http://localhost:7860/download-models")
         
     | 
| 9 | 
         
            +
                    result = response.json()
         
     | 
| 10 | 
         
            +
                    
         
     | 
| 11 | 
         
            +
                    if result.get("success", False):
         
     | 
| 12 | 
         
            +
                        return f"? SUCCESS! Models downloaded:\n{', '.join(result.get('models_downloaded', []))}\n\nStorage used: {result.get('storage_used_gb', 0):.1f}GB\nRemaining: {result.get('storage_remaining_gb', 0):.1f}GB"
         
     | 
| 13 | 
         
            +
                    else:
         
     | 
| 14 | 
         
            +
                        return f"? Download failed: {result.get('message', 'Unknown error')}"
         
     | 
| 15 | 
         
            +
                        
         
     | 
| 16 | 
         
            +
                except Exception as e:
         
     | 
| 17 | 
         
            +
                    return f"? Error: {str(e)}"
         
     | 
| 18 | 
         
            +
             
     | 
| 19 | 
         
            +
            def check_model_status():
         
     | 
| 20 | 
         
            +
                """Check current model status"""
         
     | 
| 21 | 
         
            +
                try:
         
     | 
| 22 | 
         
            +
                    import requests
         
     | 
| 23 | 
         
            +
                    
         
     | 
| 24 | 
         
            +
                    response = requests.get("http://localhost:7860/model-status")
         
     | 
| 25 | 
         
            +
                    status = response.json()
         
     | 
| 26 | 
         
            +
                    
         
     | 
| 27 | 
         
            +
                    if status.get("models_downloaded", False):
         
     | 
| 28 | 
         
            +
                        models_info = "\n".join([f"- {model['name']}: {model['files']} files" for model in status.get("available_models", [])])
         
     | 
| 29 | 
         
            +
                        return f"? MODELS READY!\n\nDownloaded models:\n{models_info}\n\nFree storage: {status['storage_info']['free_gb']:.1f}GB"
         
     | 
| 30 | 
         
            +
                    else:
         
     | 
| 31 | 
         
            +
                        return f"?? No models downloaded yet\n\nFree storage: {status['storage_info']['free_gb']:.1f}GB\n\nClick 'Download Models' button to enable video generation."
         
     | 
| 32 | 
         
            +
                        
         
     | 
| 33 | 
         
            +
                except Exception as e:
         
     | 
| 34 | 
         
            +
                    return f"? Status check error: {str(e)}"
         
     | 
| 
         @@ -0,0 +1,79 @@ 
     | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
| 
         | 
|
| 1 | 
         
            +
            #!/usr/bin/env python3
         
     | 
| 2 | 
         
            +
            """
         
     | 
| 3 | 
         
            +
            Manual Model Download Script
         
     | 
| 4 | 
         
            +
            Run this to manually download video generation models
         
     | 
| 5 | 
         
            +
            """
         
     | 
| 6 | 
         
            +
             
     | 
| 7 | 
         
            +
            import os
         
     | 
| 8 | 
         
            +
            import sys
         
     | 
| 9 | 
         
            +
            from pathlib import Path
         
     | 
| 10 | 
         
            +
             
     | 
| 11 | 
         
            +
            def download_models():
         
     | 
| 12 | 
         
            +
                """Download models manually"""
         
     | 
| 13 | 
         
            +
                print("?? Manual Model Download Starting...")
         
     | 
| 14 | 
         
            +
                
         
     | 
| 15 | 
         
            +
                try:
         
     | 
| 16 | 
         
            +
                    from huggingface_hub import snapshot_download
         
     | 
| 17 | 
         
            +
                    import shutil
         
     | 
| 18 | 
         
            +
                    
         
     | 
| 19 | 
         
            +
                    # Check storage
         
     | 
| 20 | 
         
            +
                    _, _, free_bytes = shutil.disk_usage(".")
         
     | 
| 21 | 
         
            +
                    free_gb = free_bytes / (1024**3)
         
     | 
| 22 | 
         
            +
                    
         
     | 
| 23 | 
         
            +
                    print(f"?? Available storage: {free_gb:.1f}GB")
         
     | 
| 24 | 
         
            +
                    
         
     | 
| 25 | 
         
            +
                    if free_gb < 8:
         
     | 
| 26 | 
         
            +
                        print(f"? Insufficient storage: {free_gb:.1f}GB < 8GB required")
         
     | 
| 27 | 
         
            +
                        return False
         
     | 
| 28 | 
         
            +
                    
         
     | 
| 29 | 
         
            +
                    # Create models directory
         
     | 
| 30 | 
         
            +
                    models_dir = Path("./downloaded_models")
         
     | 
| 31 | 
         
            +
                    models_dir.mkdir(exist_ok=True)
         
     | 
| 32 | 
         
            +
                    
         
     | 
| 33 | 
         
            +
                    # Download text-to-video model
         
     | 
| 34 | 
         
            +
                    print("?? Downloading text-to-video model (ali-vilab/text-to-video-ms-1.7b)...")
         
     | 
| 35 | 
         
            +
                    video_path = snapshot_download(
         
     | 
| 36 | 
         
            +
                        repo_id="ali-vilab/text-to-video-ms-1.7b",
         
     | 
| 37 | 
         
            +
                        cache_dir="./downloaded_models/video",
         
     | 
| 38 | 
         
            +
                        local_files_only=False
         
     | 
| 39 | 
         
            +
                    )
         
     | 
| 40 | 
         
            +
                    print(f"? Video model downloaded: {video_path}")
         
     | 
| 41 | 
         
            +
                    
         
     | 
| 42 | 
         
            +
                    # Download audio model
         
     | 
| 43 | 
         
            +
                    print("?? Downloading audio model (facebook/wav2vec2-base-960h)...")
         
     | 
| 44 | 
         
            +
                    audio_path = snapshot_download(
         
     | 
| 45 | 
         
            +
                        repo_id="facebook/wav2vec2-base-960h",
         
     | 
| 46 | 
         
            +
                        cache_dir="./downloaded_models/audio", 
         
     | 
| 47 | 
         
            +
                        local_files_only=False
         
     | 
| 48 | 
         
            +
                    )
         
     | 
| 49 | 
         
            +
                    print(f"? Audio model downloaded: {audio_path}")
         
     | 
| 50 | 
         
            +
                    
         
     | 
| 51 | 
         
            +
                    # Check final storage
         
     | 
| 52 | 
         
            +
                    _, _, free_bytes_after = shutil.disk_usage(".")
         
     | 
| 53 | 
         
            +
                    free_gb_after = free_bytes_after / (1024**3)
         
     | 
| 54 | 
         
            +
                    used_gb = free_gb - free_gb_after
         
     | 
| 55 | 
         
            +
                    
         
     | 
| 56 | 
         
            +
                    # Create success marker
         
     | 
| 57 | 
         
            +
                    success_file = models_dir / "manual_download_success.txt"
         
     | 
| 58 | 
         
            +
                    with open(success_file, "w") as f:
         
     | 
| 59 | 
         
            +
                        f.write("MANUAL DOWNLOAD COMPLETED\\n")
         
     | 
| 60 | 
         
            +
                        f.write(f"Video model: {video_path}\\n")
         
     | 
| 61 | 
         
            +
                        f.write(f"Audio model: {audio_path}\\n")
         
     | 
| 62 | 
         
            +
                        f.write(f"Storage used: {used_gb:.1f}GB\\n")
         
     | 
| 63 | 
         
            +
                        f.write(f"Storage remaining: {free_gb_after:.1f}GB\\n")
         
     | 
| 64 | 
         
            +
                        f.write("STATUS: SUCCESS\\n")
         
     | 
| 65 | 
         
            +
                    
         
     | 
| 66 | 
         
            +
                    print(f"? SUCCESS! Models downloaded successfully!")
         
     | 
| 67 | 
         
            +
                    print(f"?? Storage used: {used_gb:.1f}GB")
         
     | 
| 68 | 
         
            +
                    print(f"?? Storage remaining: {free_gb_after:.1f}GB")
         
     | 
| 69 | 
         
            +
                    print("?? Video generation is now available!")
         
     | 
| 70 | 
         
            +
                    
         
     | 
| 71 | 
         
            +
                    return True
         
     | 
| 72 | 
         
            +
                    
         
     | 
| 73 | 
         
            +
                except Exception as e:
         
     | 
| 74 | 
         
            +
                    print(f"? Download failed: {e}")
         
     | 
| 75 | 
         
            +
                    return False
         
     | 
| 76 | 
         
            +
             
     | 
| 77 | 
         
            +
            if __name__ == "__main__":
         
     | 
| 78 | 
         
            +
                success = download_models()
         
     | 
| 79 | 
         
            +
                sys.exit(0 if success else 1)
         
     | 
| 
         @@ -0,0 +1,101 @@ 
     | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
| 
         | 
|
| 1 | 
         
            +
            #!/usr/bin/env python3
         
     | 
| 2 | 
         
            +
            """
         
     | 
| 3 | 
         
            +
            Startup Script to Trigger Model Download
         
     | 
| 4 | 
         
            +
            This script runs at startup and ensures models are downloaded
         
     | 
| 5 | 
         
            +
            """
         
     | 
| 6 | 
         
            +
             
     | 
| 7 | 
         
            +
            import os
         
     | 
| 8 | 
         
            +
            import sys
         
     | 
| 9 | 
         
            +
            import time
         
     | 
| 10 | 
         
            +
            import logging
         
     | 
| 11 | 
         
            +
            import requests
         
     | 
| 12 | 
         
            +
            from pathlib import Path
         
     | 
| 13 | 
         
            +
             
     | 
| 14 | 
         
            +
            # Configure logging
         
     | 
| 15 | 
         
            +
            logging.basicConfig(
         
     | 
| 16 | 
         
            +
                level=logging.INFO,
         
     | 
| 17 | 
         
            +
                format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
         
     | 
| 18 | 
         
            +
                handlers=[logging.StreamHandler(sys.stdout)]
         
     | 
| 19 | 
         
            +
            )
         
     | 
| 20 | 
         
            +
             
     | 
| 21 | 
         
            +
            logger = logging.getLogger("startup")
         
     | 
| 22 | 
         
            +
             
     | 
| 23 | 
         
            +
            def main():
         
     | 
| 24 | 
         
            +
                """Main startup function"""
         
     | 
| 25 | 
         
            +
                logger.info("?? AI Avatar Chat Startup Script")
         
     | 
| 26 | 
         
            +
                
         
     | 
| 27 | 
         
            +
                # Wait for the API to be ready
         
     | 
| 28 | 
         
            +
                logger.info("? Waiting for API to initialize...")
         
     | 
| 29 | 
         
            +
                time.sleep(10)  # Give the main app time to start
         
     | 
| 30 | 
         
            +
                
         
     | 
| 31 | 
         
            +
                # Create marker directory
         
     | 
| 32 | 
         
            +
                models_dir = Path("./downloaded_models")
         
     | 
| 33 | 
         
            +
                models_dir.mkdir(exist_ok=True)
         
     | 
| 34 | 
         
            +
                
         
     | 
| 35 | 
         
            +
                # Check if models already exist
         
     | 
| 36 | 
         
            +
                status_file = models_dir / "download_status.txt"
         
     | 
| 37 | 
         
            +
                if status_file.exists():
         
     | 
| 38 | 
         
            +
                    logger.info("? Found existing model status file, checking...")
         
     | 
| 39 | 
         
            +
                    with open(status_file, "r") as f:
         
     | 
| 40 | 
         
            +
                        status = f.read()
         
     | 
| 41 | 
         
            +
                        if "SUCCESS" in status:
         
     | 
| 42 | 
         
            +
                            logger.info("? Models previously downloaded successfully")
         
     | 
| 43 | 
         
            +
                            return
         
     | 
| 44 | 
         
            +
                
         
     | 
| 45 | 
         
            +
                # Check model status API
         
     | 
| 46 | 
         
            +
                try:
         
     | 
| 47 | 
         
            +
                    logger.info("?? Checking model status...")
         
     | 
| 48 | 
         
            +
                    status_response = requests.get("http://localhost:7860/model-status")
         
     | 
| 49 | 
         
            +
                    status = status_response.json()
         
     | 
| 50 | 
         
            +
                    
         
     | 
| 51 | 
         
            +
                    logger.info(f"?? Current status: {status}")
         
     | 
| 52 | 
         
            +
                    
         
     | 
| 53 | 
         
            +
                    if status.get("models_downloaded", False):
         
     | 
| 54 | 
         
            +
                        logger.info("? Models already downloaded")
         
     | 
| 55 | 
         
            +
                        return
         
     | 
| 56 | 
         
            +
                        
         
     | 
| 57 | 
         
            +
                    # Check storage
         
     | 
| 58 | 
         
            +
                    free_gb = status.get("storage_info", {}).get("free_gb", 0)
         
     | 
| 59 | 
         
            +
                    logger.info(f"?? Free storage: {free_gb:.2f}GB")
         
     | 
| 60 | 
         
            +
                    
         
     | 
| 61 | 
         
            +
                    if free_gb > 10:  # Need at least 10GB
         
     | 
| 62 | 
         
            +
                        logger.info("?? Triggering model download...")
         
     | 
| 63 | 
         
            +
                        
         
     | 
| 64 | 
         
            +
                        # Trigger model download
         
     | 
| 65 | 
         
            +
                        download_response = requests.post(
         
     | 
| 66 | 
         
            +
                            "http://localhost:7860/download-models",
         
     | 
| 67 | 
         
            +
                            json={}
         
     | 
| 68 | 
         
            +
                        )
         
     | 
| 69 | 
         
            +
                        
         
     | 
| 70 | 
         
            +
                        result = download_response.json()
         
     | 
| 71 | 
         
            +
                        logger.info(f"?? Download result: {result}")
         
     | 
| 72 | 
         
            +
                        
         
     | 
| 73 | 
         
            +
                        # Write status file
         
     | 
| 74 | 
         
            +
                        with open(status_file, "w") as f:
         
     | 
| 75 | 
         
            +
                            f.write(f"DOWNLOAD ATTEMPT: {time.ctime()}\n")
         
     | 
| 76 | 
         
            +
                            f.write(f"RESULT: {result.get('success', False)}\n")
         
     | 
| 77 | 
         
            +
                            if result.get("success", False):
         
     | 
| 78 | 
         
            +
                                f.write("STATUS: SUCCESS\n")
         
     | 
| 79 | 
         
            +
                                f.write(f"MODELS: {', '.join(result.get('models_downloaded', []))}\n")
         
     | 
| 80 | 
         
            +
                            else:
         
     | 
| 81 | 
         
            +
                                f.write(f"STATUS: FAILED\n")
         
     | 
| 82 | 
         
            +
                                f.write(f"ERROR: {result.get('message', 'Unknown error')}\n")
         
     | 
| 83 | 
         
            +
                        
         
     | 
| 84 | 
         
            +
                        if result.get("success", False):
         
     | 
| 85 | 
         
            +
                            logger.info("? Model download successful!")
         
     | 
| 86 | 
         
            +
                        else:
         
     | 
| 87 | 
         
            +
                            logger.error(f"? Model download failed: {result.get('message')}")
         
     | 
| 88 | 
         
            +
                    else:
         
     | 
| 89 | 
         
            +
                        logger.warning(f"?? Insufficient storage: {free_gb:.2f}GB < 10GB required")
         
     | 
| 90 | 
         
            +
                        
         
     | 
| 91 | 
         
            +
                except Exception as e:
         
     | 
| 92 | 
         
            +
                    logger.error(f"? Error during startup: {e}")
         
     | 
| 93 | 
         
            +
                    
         
     | 
| 94 | 
         
            +
                    # Write error status
         
     | 
| 95 | 
         
            +
                    with open(status_file, "w") as f:
         
     | 
| 96 | 
         
            +
                        f.write(f"DOWNLOAD ATTEMPT: {time.ctime()}\n")
         
     | 
| 97 | 
         
            +
                        f.write(f"STATUS: ERROR\n")
         
     | 
| 98 | 
         
            +
                        f.write(f"ERROR: {str(e)}\n")
         
     | 
| 99 | 
         
            +
             
     | 
| 100 | 
         
            +
            if __name__ == "__main__":
         
     | 
| 101 | 
         
            +
                main()
         
     |