Spaces:
Running
Running
| async def generate_avatar_api(request: GenerateRequest): | |
| """Generate avatar video with graceful fallback for HF Spaces""" | |
| logger.info(f"?? API Request: {request.prompt[:50]}...") | |
| try: | |
| # Check if we''re in storage-optimized mode | |
| if os.getenv("HF_SPACE_STORAGE_OPTIMIZED") == "1": | |
| logger.info("??? HF Spaces detected - using TTS-only mode") | |
| # Generate TTS-only response with success status | |
| output_path = await self._generate_tts_for_api(request) | |
| return { | |
| "success": True, | |
| "audio_url": f"/outputs/{os.path.basename(output_path)}", | |
| "message": "??? TTS audio generated successfully", | |
| "note": "Video generation disabled on HF Spaces due to 50GB storage limit. Running in TTS-only mode.", | |
| "mode": "TTS-only (Storage Optimized)" | |
| } | |
| # Try full video generation for non-HF environments | |
| try: | |
| result_path, duration, has_video, method = await omni_api.generate_avatar(request) | |
| if has_video: | |
| return { | |
| "success": True, | |
| "video_url": f"/outputs/{os.path.basename(result_path)}", | |
| "duration": duration, | |
| "method": method, | |
| "mode": "Full Video Generation" | |
| } | |
| else: | |
| return { | |
| "success": True, | |
| "audio_url": f"/outputs/{os.path.basename(result_path)}", | |
| "duration": duration, | |
| "method": method, | |
| "mode": "TTS-only (Video unavailable)" | |
| } | |
| except Exception as video_error: | |
| logger.warning(f"?? Video generation failed: {video_error}") | |
| # Fallback to TTS instead of returning error | |
| output_path = await self._generate_tts_for_api(request) | |
| return { | |
| "success": True, | |
| "audio_url": f"/outputs/{os.path.basename(output_path)}", | |
| "message": "??? TTS audio generated (video generation failed)", | |
| "fallback_reason": str(video_error)[:200], | |
| "mode": "TTS Fallback" | |
| } | |
| except Exception as e: | |
| logger.error(f"? API Error: {e}") | |
| raise HTTPException( | |
| status_code=500, | |
| detail=f"Generation failed: {str(e)}" | |
| ) | |
| async def _generate_tts_for_api(self, request: GenerateRequest) -> str: | |
| """Generate TTS audio for API response""" | |
| logger.info("??? Generating TTS for API response...") | |
| output_dir = "./outputs" | |
| os.makedirs(output_dir, exist_ok=True) | |
| import time | |
| tts_file = f"{output_dir}/api_tts_{int(time.time())}.wav" | |
| # Create a placeholder TTS file | |
| with open(tts_file, "w") as f: | |
| f.write(f"# TTS Audio Generated via API\\n") | |
| f.write(f"# Prompt: {request.prompt}\\n") | |
| f.write(f"# Generated in HF Spaces TTS-only mode\\n") | |
| return tts_file | |