#!/usr/bin/env python3 """ Deployment Script for FinVoice on Hugging Face Spaces Starts both FastAPI services and the Gradio interface """ import subprocess import time import os import threading import sys from pathlib import Path def start_fastapi_service(script_name, port): """Start a FastAPI service in the background""" try: cmd = [sys.executable, "-m", "uvicorn", f"{script_name}:app", "--host", "0.0.0.0", "--port", str(port)] print(f"Starting {script_name} on port {port}...") process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) return process except Exception as e: print(f"Error starting {script_name}: {e}") return None def wait_for_service(url, timeout=60): """Wait for a service to be ready""" import requests start_time = time.time() while time.time() - start_time < timeout: try: response = requests.get(f"{url}/docs", timeout=5) if response.status_code == 200: print(f"Service at {url} is ready!") return True except: pass time.sleep(2) return False def main(): """Main deployment function""" print("🚀 Starting FinVoice Deployment...") # Set environment variables os.environ["FASTAPI2_URL"] = "http://localhost:8001" # Start FastAPI services multilingual_process = start_fastapi_service("fastapi1_multilingual", 8000) time.sleep(5) # Give first service time to start finance_process = start_fastapi_service("fastapi2_finance", 8001) time.sleep(5) # Give second service time to start # Wait for services to be ready print("⏳ Waiting for services to initialize...") if not wait_for_service("http://localhost:8000"): print("❌ Multilingual service failed to start") return if not wait_for_service("http://localhost:8001"): print("❌ Finance service failed to start") return print("✅ All services are ready!") print("🎯 Starting Gradio interface...") # Import and start the main Gradio app try: from app import demo demo.launch( server_name="0.0.0.0", server_port=7860, share=False, show_error=True, quiet=False ) except Exception as e: print(f"❌ Error starting Gradio interface: {e}") # Cleanup processes if multilingual_process: multilingual_process.terminate() if finance_process: finance_process.terminate() if __name__ == "__main__": main()