|
""" |
|
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" |
|
MODAL = "modal" |
|
|
|
|
|
DEPLOYMENT_MODE = DeploymentMode(os.getenv("DEPLOYMENT_MODE", "local")) |
|
|
|
|
|
MODAL_APP_NAME = "gradio-mcp-server" |
|
|
|
|
|
ENDPOINTS = { |
|
"transcribe_audio": None, |
|
} |
|
|
|
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") |
|
|
|
|
|
def get_cache_dir() -> str: |
|
"""Get cache directory based on deployment mode""" |
|
if is_modal_mode(): |
|
return "/root/cache" |
|
else: |
|
|
|
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 |
|
|
|
|
|
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()}") |