File size: 3,637 Bytes
6fc2b75
 
4f625d4
6fc2b75
 
4f625d4
6fc2b75
 
 
 
 
4f625d4
6fc2b75
 
 
 
 
4f625d4
6fc2b75
 
 
 
 
 
 
4f625d4
6fc2b75
 
4f625d4
6fc2b75
 
 
 
 
 
 
 
 
4f625d4
 
6fc2b75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Wrdler - Gradio Application Entry Point

A vocabulary puzzle game with an 8x6 grid and 6 hidden words.
This is the Gradio version, running alongside the Streamlit app.

Usage:
    python gradio_app.py
    # or
    gradio gradio_app.py
"""

import os
import tempfile
from pathlib import Path
import gradio as gr
from wrdler.gradio_ui import create_app

# Import word_loader_ai to register MCP functions
# This must happen before creating the app
try:
    from wrdler import word_loader_ai
    print("? word_loader_ai module loaded (MCP functions may be registered)")
except Exception as e:
    print(f"?? Could not load word_loader_ai: {e}")

# Get the project root directory (where this file is located)
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

# Set the current working directory to project root
# This ensures file paths are resolved correctly
os.chdir(PROJECT_ROOT)

print(f"Working Directory: {os.getcwd()}")
print(f"Project Root: {PROJECT_ROOT}")

# Create the Gradio app
demo = create_app()

if __name__ == "__main__":
    # Configure paths for static assets
    favicon_path = os.path.join(PROJECT_ROOT, "static", "favicon.ico")
    
    # Get Gradio's temp directory
    gradio_temp_dir = Path(tempfile.gettempdir()) / "gradio"
    
    # Verify audio directory exists
    audio_dir = os.path.join(PROJECT_ROOT, "wrdler", "assets", "audio")
    print(f"Audio directory exists: {os.path.exists(audio_dir)}")
    
    if os.path.exists(audio_dir):
        # List audio files for debugging
        music_dir = os.path.join(audio_dir, "music")
        effects_dir = os.path.join(audio_dir, "effects")
        
        if os.path.exists(music_dir):
            music_files = [f for f in os.listdir(music_dir) if f.endswith('.mp3')]
            print(f"Music files found: {music_files}")
        
        if os.path.exists(effects_dir):
            effect_files = [f for f in os.listdir(effects_dir) if f.endswith('.mp3')]
            print(f"Effect files found: {effect_files}")
    
    # Check MCP server status
    use_hf_words = os.getenv("USE_HF_WORDS", "false").lower() == "true"
    if not use_hf_words:
        print("\n" + "="*70)
        print("?? MCP SERVER ENABLED (Local Mode)")
        print("="*70)
        print("MCP tools available:")
        print("  - generate_ai_words: Generate AI vocabulary words for topics")
        print("\nTo use MCP tools, connect your MCP client to this Gradio app.")
        print("See: https://www.gradio.app/guides/building-mcp-server-with-gradio")
        print("="*70 + "\n")
    else:
        print("\n?? MCP server disabled (USE_HF_WORDS=true, running in remote mode)")
    
    # Launch configuration
    launch_kwargs = {
        "server_name": "0.0.0.0",
        "server_port": 7860,
        "share": False,
        "show_error": True,
        # Enable MCP server in local mode
        "mcp_server": not use_hf_words,
        # Gradio's allowed_paths should include:
        # 1. Project root (for any relative paths)
        # 2. System temp directory (where Gradio caches files)
        "allowed_paths": [
            PROJECT_ROOT,
            str(gradio_temp_dir),
            tempfile.gettempdir(),
        ],
    }
    
    # Add favicon if it exists
    if os.path.exists(favicon_path):
        launch_kwargs["favicon_path"] = favicon_path
    
    print(f"\nLaunching Gradio app...")
    print(f"Allowed paths:")
    for path in launch_kwargs['allowed_paths']:
        print(f"  - {path}")
    print(f"Server URL: http://localhost:7860")
    if launch_kwargs["mcp_server"]:
        print(f"MCP Server: ENABLED")
    
    demo.launch(**launch_kwargs)