import warnings import os import sys # Force UTF-8 for console output (Windows fix) sys.stdout.reconfigure(encoding='utf-8') from huggingface_hub import HfApi, login import gradio as gr # Filter warnings immediately warnings.filterwarnings("ignore", category=SyntaxWarning, module="pydub") # --- INTERNAL IMPORTS --- from config import AppConfig from src.utils import create_warning_beep # Import your refactored classes from src.managers import GeminiManager, HFManager, GroqManager from src.input_agent import AgentInput from src.brain_agent import AgentInterpretation from src.trust_agent import AgentTrust from src.ux_agent import AgentUX from src.acoustic_agent import AgentAcoustic # 1. Add the import for your recovery script import recover_chain # (or 'from src import recover_chain' depending on where the file is) # 2. Add this block before ui.launch() print("🔄 Initiating Blockchain Recovery Sequence...") try: # Assuming your recover_chain.py has a main() or run() function recover_chain.main() print("✅ Recovery Complete!") except Exception as e: print(f"❌ Recovery Failed: {e}") # ========================================== # MAIN APPLICATION LOGIC # ========================================== def create_app(): print("🚀 Initializing PureVersation...") # 1. Setup Configuration & Directories AppConfig.setup_directories() # 🟢 NEW: The Silver Bullet - Global Hugging Face Login # This must happen BEFORE HFManager tries to pull datasets if AppConfig.HF_TOKEN: login(token=AppConfig.HF_TOKEN) print("✅ Hugging Face Global Login Successful.") else: print("🚨 WARNING: HF_TOKEN is missing in AppConfig! Downloads will fail.") # 2. Initialize Managers (Inject Config) # We pass AppConfig to HFManager so it knows where to download files hf_manager = HFManager(AppConfig) hf_manager.pull_datasets() # Initialize Groq (New Brain Stem) groq_manager = GroqManager(AppConfig.GROQ_API_KEY) if AppConfig.GROQ_API_KEY else None # Initialize Gemini (Brain Stem) #gemini_manager = GeminiManager(AppConfig.GOOGLE_API_KEY) if AppConfig.GOOGLE_API_KEY else None # 3. Initialize Agents (Dependency Injection) # Agent 1: Input (Whisper) agent1 = AgentInput() # Agent 2: Brain (Interpretation) # We pass AppConfig so it knows where PROFILES_DIR is # agent2 = AgentInterpretation(AppConfig, gemini_manager_instance=gemini_manager) # Inject groq_manager instead of gemini_manager agent2 = AgentInterpretation(AppConfig, gemini_manager_instance=groq_manager) # Agent 4: Trust (Blockchain/IPFS) # We pass AppConfig so it has keys for Web3 and Pinata agent4 = AgentTrust(AppConfig) # Inject hf_manager into Trust so it can save audio files to HF agent4.hf_manager_ref = hf_manager # Agent 5: Acoustic (Phonetic IPA) agent5 = AgentAcoustic(groq_manager) agent4.acoustic_agent = agent5 # Agent 3: UX (Interface) agent3 = AgentUX(agent1, agent2, agent4) # 4. Launch print("✅ System Assembled. Launching Interface...") # Pass the warning beep path agent3.alert_sound = create_warning_beep() # 1. Generate the UI blocks app = agent3.create_ui() # 🟢 FIX: Enable the Gradio Queue here to prevent async loop collisions! app.queue(default_concurrency_limit=10) return app # Expose 'demo' at the module level for Gradio Hot Reload demo = create_app() if __name__ == "__main__": # 2. Launch the app and apply your theme here safely! demo.launch(theme=gr.themes.Soft(primary_hue="blue"))