PalDPathak/Smart-Traffic-openenv
0
1import gradio as gr2import os3import random4from evaluate import run_evaluation5from visualize import generate_graph6 7def run_simulation(manual_seed=None):8 try:9 seed = int(manual_seed) if manual_seed and str(manual_seed).isdigit() else random.randint(1000, 99999)10 11 import io12 from contextlib import redirect_stdout13 14 f = io.StringIO()15 with redirect_stdout(f):16 scores = run_evaluation(base_seed=seed, silent=False)17 logs = f.getvalue()18 19 graph_path = "optimization_results.png"20 generate_graph(scores, seed, output_path=graph_path)21 22 return logs, graph_path23 except Exception as e:24 return f"Error running simulation: {str(e)}", None25 26with gr.Blocks() as interface:27 gr.Markdown("# ๐ฆ Smart Traffic Optimization Environment (OpenEnv)")28 gr.Markdown("Welcome to the Traffic Simulator. Watch how our AI improves traffic flow in real time.")29 30 with gr.Row():31 with gr.Column(scale=1):32 pass33 with gr.Column(scale=2, min_width=320):34 seed_input = gr.Textbox(label="Optional Seed (Empty for Random)", placeholder="e.g. 42")35 run_btn = gr.Button("๐ Run Traffic Evaluator", variant="primary", size="lg")36 gr.HTML("""37 <p style='38 text-align: center;39 color: rgba(100, 100, 100, 0.75);40 font-size: 0.88em;41 margin: 4px 0 16px 0;42 font-weight: 400;43 letter-spacing: 0.01em;44 '>Click to simulate AI-driven traffic optimization across difficulty levels.</p>45 """)46 with gr.Column(scale=1):47 pass48 49 gr.Markdown("""50 <div style='background-color: #ffeaea; border-left: 4px solid #ff4d4f; padding: 12px; margin: 15px 0px; border-radius: 4px; box-shadow: 0 1px 3px rgba(0,0,0,0.1);'>51 <span style='color: #a8071a; font-weight: 600; font-size: 1.05em;'>๐จ Active Protocol:</span> 52 <span style='color: #434343;'>System prioritizes emergency vehicles in real-time.</span>53 </div>54 """)55 56 with gr.Row():57 with gr.Column(scale=1):58 gr.Markdown("<h3 style='text-align: center; color: #555; margin-bottom: 2px;'>Simulation Logs</h3>")59 output_text = gr.Textbox(show_label=False, lines=22, interactive=False)60 61 with gr.Column(scale=1):62 gr.Markdown("<h3 style='text-align: center; color: #222; margin-bottom: 10px; border-bottom: 1px solid #eaeaea; padding-bottom: 5px;'>Performance Improvement After Optimization</h3>")63 output_img = gr.Image(show_label=False, type="filepath")64 65 run_btn.click(fn=run_simulation, inputs=[seed_input], outputs=[output_text, output_img])66 67if __name__ == "__main__":68 interface.launch(server_name="0.0.0.0", server_port=7860, theme=gr.themes.Soft())69 