Spaces:
Sleeping
Sleeping
#!/usr/bin/env python3 | |
import subprocess | |
import sys | |
import os | |
import time | |
import signal | |
import atexit | |
def start_api_server(): | |
api_process = subprocess.Popen( | |
[sys.executable, "api.py"], | |
cwd=os.path.dirname(os.path.abspath(__file__)) | |
) | |
print("API server started (PID:", api_process.pid, ")") | |
return api_process | |
def start_gradio_app(): | |
gradio_process = subprocess.Popen( | |
[sys.executable, "-c", "import gradio as gr; import run; run.demo.launch()"], | |
cwd=os.path.dirname(os.path.abspath(__file__)) | |
) | |
print("Gradio interface started (PID:", gradio_process.pid, ")") | |
return gradio_process | |
def cleanup_processes(api_process, gradio_process): | |
print("\nShutting down services...") | |
if api_process and api_process.poll() is None: | |
api_process.terminate() | |
print("API server terminated") | |
if gradio_process and gradio_process.poll() is None: | |
gradio_process.terminate() | |
print("Gradio interface terminated") | |
def main(): | |
api_process = start_api_server() | |
# Give the API server a moment to start | |
time.sleep(2) | |
gradio_process = start_gradio_app() | |
# Register cleanup function to be called on exit | |
atexit.register(cleanup_processes, api_process, gradio_process) | |
# Handle keyboard interrupts | |
def signal_handler(sig, frame): | |
print("\nReceived termination signal") | |
cleanup_processes(api_process, gradio_process) | |
sys.exit(0) | |
signal.signal(signal.SIGINT, signal_handler) | |
signal.signal(signal.SIGTERM, signal_handler) | |
print("\nLeaderboard application started!") | |
print("- API server running at http://localhost:8000") | |
print("- Gradio interface running at http://localhost:7860") | |
print("\nPress Ctrl+C to stop all services") | |
# Keep the main process running | |
try: | |
while True: | |
time.sleep(1) | |
except KeyboardInterrupt: | |
pass | |
if __name__ == "__main__": | |
main() | |