#!/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}")