voxtral-local / app.py
broadfield-dev's picture
Create app.py
a7193e2 verified
import os
import subprocess
import sys
import time
from pathlib import Path
import gradio as gr
# HF Spaces environment variables
SPACE_ID = os.getenv("SPACE_ID", "broadfield-dev/voxtral-local")
HF_TOKEN = os.getenv("HF_TOKEN") # Optional, for private models if needed
# Configuration
PORT = 7860 # Gradio default on HF Spaces
HOST = "0.0.0.0"
print("๐Ÿš€ Starting Voxtral TTS Gradio Demo on Hugging Face Spaces...")
# Step 1: Clone the repository (if not already present)
repo_dir = Path("vllm-omni")
if not repo_dir.exists():
print("Cloning vllm-omni repository...")
subprocess.run(
["git", "clone", "https://github.com/vllm-project/vllm-omni.git"],
check=True
)
else:
print("Repository already cloned.")
os.chdir(repo_dir)
# Step 2: Install dependencies
# Note: HF Spaces uses uv or pip; we use uv as in the original command
print("Installing dependencies with uv...")
subprocess.run(["uv", "pip", "install", "gradio==5.50"], check=True)
# Install vllm-omni from source (recommended by the model authors)
print("Installing vllm-omni from source...")
subprocess.run(
["uv", "pip", "install", "-e", "."],
check=True
)
# Additional common dependencies for audio/TTS (soundfile, etc.)
subprocess.run(
["uv", "pip", "install", "soundfile", "numpy"],
check=True
)
print("โœ… Dependencies installed.")
# Step 3: Launch the Gradio demo
print(f"Launching Gradio demo on http://{HOST}:{PORT} ...")
# We run the demo script in a subprocess so the main thread can keep the Space alive
# The original command uses --host and --port; we adapt for HF Spaces (0.0.0.0 + 7860)
cmd = [
sys.executable,
"examples/online_serving/voxtral_tts/gradio_demo.py",
"--host", HOST,
"--port", str(PORT),
]
# Optional: add other flags if the script supports them (e.g. model path, etc.)
# cmd.extend(["--model", "mistralai/Voxtral-4B-TTS-2603"]) # if needed
print("Running command:", " ".join(cmd))
# Launch the process
process = subprocess.Popen(
cmd,
stdout=sys.stdout,
stderr=sys.stderr,
env=os.environ.copy()
)
# Keep the main process alive (required for HF Spaces)
try:
while True:
time.sleep(60) # Check every minute
if process.poll() is not None:
print("โš ๏ธ Gradio process exited unexpectedly.")
break
except KeyboardInterrupt:
print("Shutting down...")
process.terminate()