CoolFace
Apppublic

amith33/voice_agent

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
config.py204 linesDownload Raw Back to root
1"""2Configuration file for AI Voice & Lip Sync Generator3Centralizes all settings and paths4"""5 6import os7from pathlib import Path8 9# Get the project root directory10PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))11 12# Check if we're running in Hugging Face Spaces (restricted environment)13IS_SPACES_ENV = os.environ.get('SPACE_ID') is not None14 15# Base paths - use /tmp in restricted environments16if IS_SPACES_ENV:17    BASE_STATIC_DIR = "/tmp"18    UPLOADS_DIR = "/tmp/static_uploads"19    PRESETS_DIR = "/tmp/static_presets"20    RESULTS_DIR = "/tmp/static_results"21else:22    BASE_STATIC_DIR = os.path.join(PROJECT_ROOT, "static")23    UPLOADS_DIR = os.path.join(BASE_STATIC_DIR, "uploads")24    PRESETS_DIR = os.path.join(BASE_STATIC_DIR, "presets")25    RESULTS_DIR = os.path.join(BASE_STATIC_DIR, "results")26 27# Voice cloning configuration28VOICE_CLONE_CONFIG = {29    "script": os.path.join(PROJECT_ROOT, "voice_clone_test/tts_clone.py"),30    "python_path": "venv/bin/python",31    "reference_audio": os.path.join(PROJECT_ROOT, "voice_clone_test/ryan_reference.wav"),32    "output_dir": os.path.join(PROJECT_ROOT, "outputs")33}34 35# Lip sync configuration36LIP_SYNC_CONFIG = {37    "directory": os.path.join(PROJECT_ROOT, "lip_sync_test/SadTalker"),38    "script": "inference.py",39    "python_path": "venv/bin/python",40    "source_images_dir": "examples/source_image",41    "default_image": "people_0.png",42    "batch_size": 2,43    "size": 256,44    "expression_scale": 1.0,45    "enhancer": None,46    "background_enhancer": None,47    "still_mode": True,48    "preprocess": "full"  # Use full image instead of cropping49}50 51# Application configuration52APP_CONFIG = {53    "host": "0.0.0.0",54    "port": 8000,55    "debug": False,56    "static_dir": BASE_STATIC_DIR,57    "uploads_dir": UPLOADS_DIR,58    "presets_dir": PRESETS_DIR,59    "results_dir": RESULTS_DIR,60    "max_file_size": 50 * 1024 * 1024,  # 50MB61    "allowed_extensions": {".jpg", ".jpeg", ".png", ".wav", ".mp3", ".mp4"}62}63 64# Output Configuration65OUTPUT_CONFIG = {66    "base_dir": os.path.join(PROJECT_ROOT, "outputs"),67    "audio_format": "wav",68    "video_format": "mp4",69    "cleanup_temp_files": True70}71 72# Processing Configuration73PROCESSING_CONFIG = {74    "max_text_length": 1000,  # Maximum characters for text input75    "min_text_length": 10,     # Minimum characters for text input76    "timeout_seconds": 300,    # Timeout for processing (5 minutes)77    "progress_update_interval": 1.0  # Seconds between progress updates78}79 80# Preset Images Configuration81PRESET_IMAGES = [82    {83        "name": "people_0.png",84        "display_name": "Person 1",85        "description": "Standard portrait"86    },87    {88        "name": "happy.png",89        "display_name": "Happy Person",90        "description": "Smiling face"91    },92    {93        "name": "sad.png",94        "display_name": "Sad Person",95        "description": "Serious expression"96    },97    {98        "name": "full_body_1.png",99        "display_name": "Full Body",100        "description": "Full body shot"101    },102    {103        "name": "art_0.png",104        "display_name": "Art Style 1",105        "description": "Artistic rendering"106    },107    {108        "name": "art_1.png",109        "display_name": "Art Style 2",110        "description": "Another artistic style"111    }112]113 114# Error Messages115ERROR_MESSAGES = {116    "text_too_short": "Text must be at least {} characters long",117    "text_too_long": "Text must be less than {} characters",118    "invalid_image": "Please upload a valid image file (PNG, JPG, JPEG)",119    "upload_failed": "Failed to upload image. Please try again.",120    "voice_clone_failed": "Voice cloning failed. Please check the logs.",121    "lip_sync_failed": "Lip synchronization failed. Please check the logs.",122    "no_video_generated": "No video was generated. Please try again.",123    "file_not_found": "Required file not found: {}",124    "venv_not_found": "Virtual environment not found: {}"125}126 127# Success Messages128SUCCESS_MESSAGES = {129    "image_uploaded": "Image uploaded successfully!",130    "video_generated": "Video generated successfully!",131    "pipeline_completed": "Pipeline completed successfully!"132}133 134# Progress Messages135PROGRESS_MESSAGES = {136    "initializing": "Initializing pipeline...",137    "voice_cloning": "Cloning voice from reference audio...",138    "lip_syncing": "Generating lip sync video...",139    "finalizing": "Finalizing video...",140    "completed": "Video generation completed!"141}142 143def get_voice_clone_command(text, output_path):144    """Generate voice cloning command"""145    return [146        os.path.join(VOICE_CLONE_CONFIG["directory"], VOICE_CLONE_CONFIG["python_path"]),147        os.path.join(VOICE_CLONE_CONFIG["directory"], VOICE_CLONE_CONFIG["script"]),148        "--text", text,149        "--speaker_wav", os.path.join(VOICE_CLONE_CONFIG["directory"], VOICE_CLONE_CONFIG["reference_audio"]),150        "--file_path", output_path,151        "--language", VOICE_CLONE_CONFIG["language"]152    ]153 154def get_lip_sync_command(audio_path, image_path, results_dir):155    """Generate lip sync command"""156    base_cmd = [157        os.path.join(LIP_SYNC_CONFIG["directory"], LIP_SYNC_CONFIG["python_path"]),158        os.path.join(LIP_SYNC_CONFIG["directory"], LIP_SYNC_CONFIG["script"]),159        "--driven_audio", audio_path,160        "--source_image", image_path,161        "--result_dir", results_dir,162        "--batch_size", str(LIP_SYNC_CONFIG["batch_size"]),163        "--size", str(LIP_SYNC_CONFIG["size"]),164        "--expression_scale", str(LIP_SYNC_CONFIG["expression_scale"]),165        "--preprocess", LIP_SYNC_CONFIG["preprocess"]166    ]167    168    if LIP_SYNC_CONFIG["still_mode"]:169        base_cmd.append("--still")170    171    if LIP_SYNC_CONFIG["enhancer"]:172        base_cmd.extend(["--enhancer", LIP_SYNC_CONFIG["enhancer"]])173    174    if LIP_SYNC_CONFIG["background_enhancer"]:175        base_cmd.extend(["--background_enhancer", LIP_SYNC_CONFIG["background_enhancer"]])176    177    return base_cmd178 179def validate_text(text):180    """Validate text input"""181    if len(text) < PROCESSING_CONFIG["min_text_length"]:182        return False, ERROR_MESSAGES["text_too_short"].format(PROCESSING_CONFIG["min_text_length"])183    184    if len(text) > PROCESSING_CONFIG["max_text_length"]:185        return False, ERROR_MESSAGES["text_too_long"].format(PROCESSING_CONFIG["max_text_length"])186    187    return True, ""188 189def get_image_path(image_name):190    """Get full path for image file"""191    if image_name.startswith("upload_"):192        # Custom uploaded image193        return os.path.join(PROJECT_ROOT, WEB_CONFIG["static_dir"], "uploads", image_name)194    else:195        # Preset image196        return os.path.join(LIP_SYNC_CONFIG["directory"], LIP_SYNC_CONFIG["source_images_dir"], image_name)197 198def create_output_directory():199    """Create output directory with timestamp"""200    from datetime import datetime201    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")202    output_dir = os.path.join(OUTPUT_CONFIG["base_dir"], timestamp)203    os.makedirs(output_dir, exist_ok=True)204    return output_dir