File size: 2,478 Bytes
a48a28c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
81
82
83
#!/usr/bin/env python3
"""
AI Avatar Chat - HF Spaces Optimized Version
With robust import handling and graceful fallbacks
"""

import os

# STORAGE OPTIMIZATION: Check if running on HF Spaces and disable model downloads
IS_HF_SPACE = any([
    os.getenv("SPACE_ID"),
    os.getenv("SPACE_AUTHOR_NAME"), 
    os.getenv("SPACES_BUILDKIT_VERSION"),
    "/home/user/app" in os.getcwd()
])

if IS_HF_SPACE:
    # Force TTS-only mode to prevent storage limit exceeded
    os.environ["DISABLE_MODEL_DOWNLOAD"] = "1"
    os.environ["TTS_ONLY_MODE"] = "1" 
    os.environ["HF_SPACE_STORAGE_OPTIMIZED"] = "1"
    print("?? STORAGE OPTIMIZATION: Detected HF Space environment")
    print("??? TTS-only mode ENABLED (video generation disabled for storage limits)")
    print("?? Model auto-download DISABLED to prevent storage exceeded error")

# Core imports (required)
import torch
import tempfile
import gradio as gr
from fastapi import FastAPI, HTTPException
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, HttpUrl
import subprocess
import json
from pathlib import Path
import logging
import requests
from urllib.parse import urlparse
from PIL import Image
import io
from typing import Optional
import asyncio
import time

# Optional imports with graceful fallbacks
try:
    import aiohttp
    AIOHTTP_AVAILABLE = True
except ImportError:
    print("Warning: aiohttp not available")
    AIOHTTP_AVAILABLE = False

try:
    from dotenv import load_dotenv
    load_dotenv()
    DOTENV_AVAILABLE = True
except ImportError:
    print("Warning: python-dotenv not available, skipping .env file loading")
    DOTENV_AVAILABLE = False
    def load_dotenv():
        pass

try:
    from hf_spaces_fix import setup_hf_spaces_environment, HFSpacesCompatible
    setup_hf_spaces_environment()
    HF_FIX_AVAILABLE = True
except ImportError:
    print("Warning: HF Spaces fix not available")
    HF_FIX_AVAILABLE = False

# STREAMING MODEL SUPPORT for HF Spaces
try:
    from streaming_video_engine import streaming_engine
    STREAMING_ENABLED = True
    print("?? Streaming video engine loaded successfully")
except ImportError:
    STREAMING_ENABLED = False
    print("?? Streaming engine not available, using TTS-only mode")

print("? Core imports loaded successfully")
print(f"?? Optional features: aiohttp={AIOHTTP_AVAILABLE}, dotenv={DOTENV_AVAILABLE}, hf_fix={HF_FIX_AVAILABLE}, streaming={STREAMING_ENABLED}")