File size: 3,475 Bytes
8c74501
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    @app.post("/generate")
    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