import gradio as gr import os import random from evaluate import run_evaluation from visualize import generate_graph def run_simulation(manual_seed=None): try: seed = int(manual_seed) if manual_seed and str(manual_seed).isdigit() else random.randint(1000, 99999) import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): scores = run_evaluation(base_seed=seed, silent=False) logs = f.getvalue() graph_path = "optimization_results.png" generate_graph(scores, seed, output_path=graph_path) return logs, graph_path except Exception as e: return f"Error running simulation: {str(e)}", None with gr.Blocks() as interface: gr.Markdown("# 🚦 Smart Traffic Optimization Environment (OpenEnv)") gr.Markdown("Welcome to the Traffic Simulator. Watch how our AI improves traffic flow in real time.") with gr.Row(): with gr.Column(scale=1): pass with gr.Column(scale=2, min_width=320): seed_input = gr.Textbox(label="Optional Seed (Empty for Random)", placeholder="e.g. 42") run_btn = gr.Button("🚀 Run Traffic Evaluator", variant="primary", size="lg") gr.HTML("""
Click to simulate AI-driven traffic optimization across difficulty levels.
""") with gr.Column(scale=1): pass gr.Markdown("""