File size: 2,757 Bytes
b5df735
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Deployment configuration for Gradio + MCP Server
Supports two deployment modes:
1. Local mode: Gradio runs locally, GPU functions call Modal endpoints
2. Modal mode: Gradio runs on Modal, GPU functions run locally on Modal
"""

import os
from enum import Enum
from typing import Optional

class DeploymentMode(Enum):
    LOCAL = "local"      # Local Gradio + Remote GPU (Modal endpoints)
    MODAL = "modal"      # Modal Gradio + Local GPU (Modal functions)

# Get deployment mode from environment variable
DEPLOYMENT_MODE = DeploymentMode(os.getenv("DEPLOYMENT_MODE", "local"))

# Modal endpoints configuration
MODAL_APP_NAME = "gradio-mcp-server"

# Endpoint URLs (will be set when deployed)
ENDPOINTS = {
    "transcribe_audio": None,  # Will be filled with actual endpoint URL
}

def get_deployment_mode() -> DeploymentMode:
    """Get current deployment mode"""
    return DEPLOYMENT_MODE

def is_local_mode() -> bool:
    """Check if running in local mode"""
    return DEPLOYMENT_MODE == DeploymentMode.LOCAL

def is_modal_mode() -> bool:
    """Check if running in modal mode"""
    return DEPLOYMENT_MODE == DeploymentMode.MODAL

def set_endpoint_url(endpoint_name: str, url: str):
    """Set endpoint URL for local mode"""
    global ENDPOINTS
    ENDPOINTS[endpoint_name] = url

def get_endpoint_url(endpoint_name: str) -> Optional[str]:
    """Get endpoint URL for local mode"""
    return ENDPOINTS.get(endpoint_name)

def get_transcribe_endpoint_url() -> Optional[str]:
    """Get transcription endpoint URL"""
    return get_endpoint_url("transcribe_audio")

# Environment-specific cache directory
def get_cache_dir() -> str:
    """Get cache directory based on deployment mode"""
    if is_modal_mode():
        return "/root/cache"
    else:
        # Local mode - use user's home directory
        home_dir = os.path.expanduser("~")
        cache_dir = os.path.join(home_dir, ".gradio_mcp_cache")
        os.makedirs(cache_dir, exist_ok=True)
        return cache_dir

# Auto-load endpoint configuration in local mode
if is_local_mode():
    import json
    config_file = "endpoint_config.json"
    if os.path.exists(config_file):
        try:
            with open(config_file, 'r') as f:
                config = json.load(f)
            for endpoint_name, url in config.items():
                set_endpoint_url(endpoint_name, url)
            print(f"โœ… Loaded endpoint configuration from {config_file}")
        except Exception as e:
            print(f"โš ๏ธ Failed to load endpoint configuration: {e}")
    else:
        print(f"โš ๏ธ No endpoint configuration found. Run 'python deploy_endpoints.py deploy' first.")

print(f"๐Ÿš€ Deployment mode: {DEPLOYMENT_MODE.value}")
print(f"๐Ÿ“ Cache directory: {get_cache_dir()}")