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("""
🚨 Active Protocol: System prioritizes emergency vehicles in real-time.
""") with gr.Row(): with gr.Column(scale=1): gr.Markdown("

Simulation Logs

") output_text = gr.Textbox(show_label=False, lines=22, interactive=False) with gr.Column(scale=1): gr.Markdown("

Performance Improvement After Optimization

") output_img = gr.Image(show_label=False, type="filepath") run_btn.click(fn=run_simulation, inputs=[seed_input], outputs=[output_text, output_img]) if __name__ == "__main__": interface.launch(server_name="0.0.0.0", server_port=7860, theme=gr.themes.Soft())