File size: 3,991 Bytes
881ea9e | 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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | 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")
warnings.filterwarnings(
"ignore",
message=r".*HTTP_422_UNPROCESSABLE_ENTITY.*HTTP_422_UNPROCESSABLE_CONTENT.*",
category=Warning,
module=r"gradio\.routes",
)
# --- 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()
from api import app as api_app
app = gr.mount_gradio_app(
api_app,
demo,
path="/",
ssr_mode=False,
theme=gr.themes.Soft(primary_hue="blue"),
)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=int(os.getenv("PORT", "7860")))
|