Spaces:
Running
Running
| import gradio as gr | |
| import pandas as pd | |
| import plotly.graph_objects as go | |
| def safety_agent(fire_alarm, exits_clear, hazards): | |
| score = 100 | |
| if not fire_alarm: | |
| score -= 35 | |
| if not exits_clear: | |
| score -= 35 | |
| score -= hazards * 10 | |
| return max(score, 0) | |
| def security_agent(cctv, access_control, incidents): | |
| score = 100 | |
| if not cctv: | |
| score -= 30 | |
| if not access_control: | |
| score -= 30 | |
| score -= incidents * 15 | |
| return max(score, 0) | |
| def reliability_agent(elevator_ok, hvac_ok, power_backup, maintenance_issues): | |
| score = 100 | |
| if not elevator_ok: | |
| score -= 25 | |
| if not hvac_ok: | |
| score -= 25 | |
| if not power_backup: | |
| score -= 25 | |
| score -= maintenance_issues * 10 | |
| return max(score, 0) | |
| def status_label(score): | |
| if score >= 80: | |
| return "GOOD" | |
| elif score >= 60: | |
| return "WARNING" | |
| else: | |
| return "CRITICAL" | |
| def evaluate_building( | |
| fire_alarm, exits_clear, hazards, | |
| cctv, access_control, incidents, | |
| elevator_ok, hvac_ok, power_backup, maintenance_issues | |
| ): | |
| safety = safety_agent(fire_alarm, exits_clear, hazards) | |
| security = security_agent(cctv, access_control, incidents) | |
| reliability = reliability_agent(elevator_ok, hvac_ok, power_backup, maintenance_issues) | |
| overall = round((safety + security + reliability) / 3, 2) | |
| df = pd.DataFrame({ | |
| "Agent": ["Safety", "Security", "Reliability", "Overall"], | |
| "Score": [safety, security, reliability, overall], | |
| "Status": [ | |
| status_label(safety), | |
| status_label(security), | |
| status_label(reliability), | |
| status_label(overall) | |
| ] | |
| }) | |
| fig = go.Figure() | |
| fig.add_trace(go.Bar( | |
| x=df["Agent"], | |
| y=df["Score"], | |
| text=df["Status"], | |
| textposition="auto" | |
| )) | |
| fig.update_layout( | |
| title="Building Safety, Security, and Reliability Status", | |
| yaxis=dict(range=[0, 100]), | |
| xaxis_title="Evaluation Agent", | |
| yaxis_title="Score" | |
| ) | |
| report = f""" | |
| # Building Evaluation Report | |
| ## Overall Status: {status_label(overall)} | |
| Overall Score: {overall}/100 | |
| ## Agent Results | |
| - Safety Score: {safety}/100 — {status_label(safety)} | |
| - Security Score: {security}/100 — {status_label(security)} | |
| - Reliability Score: {reliability}/100 — {status_label(reliability)} | |
| ## Recommendation | |
| """ | |
| if overall >= 80: | |
| report += "The building condition is acceptable. Continue regular monitoring." | |
| elif overall >= 60: | |
| report += "The building needs attention. Some risks are present and should be addressed soon." | |
| else: | |
| report += "The building is in critical condition. Immediate action is required." | |
| return df, fig, report | |
| with gr.Blocks(title="Building Multi-Agent Risk Evaluation") as demo: | |
| gr.Markdown("# Multi-Agent Building Safety, Security, and Reliability Evaluator") | |
| with gr.Row(): | |
| with gr.Column(): | |
| gr.Markdown("## Safety Agent") | |
| fire_alarm = gr.Checkbox(label="Fire alarm working", value=True) | |
| exits_clear = gr.Checkbox(label="Emergency exits clear", value=True) | |
| hazards = gr.Slider(0, 5, step=1, label="Number of visible hazards") | |
| with gr.Column(): | |
| gr.Markdown("## Security Agent") | |
| cctv = gr.Checkbox(label="CCTV working", value=True) | |
| access_control = gr.Checkbox(label="Access control active", value=True) | |
| incidents = gr.Slider(0, 5, step=1, label="Recent security incidents") | |
| with gr.Column(): | |
| gr.Markdown("## Reliability Agent") | |
| elevator_ok = gr.Checkbox(label="Elevator working", value=True) | |
| hvac_ok = gr.Checkbox(label="HVAC working", value=True) | |
| power_backup = gr.Checkbox(label="Backup power available", value=True) | |
| maintenance_issues = gr.Slider(0, 5, step=1, label="Maintenance issues") | |
| btn = gr.Button("Evaluate Building") | |
| output_table = gr.Dataframe(label="Agent Scores") | |
| output_chart = gr.Plot(label="Status Chart") | |
| output_report = gr.Markdown(label="Report") | |
| btn.click( | |
| evaluate_building, | |
| inputs=[ | |
| fire_alarm, exits_clear, hazards, | |
| cctv, access_control, incidents, | |
| elevator_ok, hvac_ok, power_backup, maintenance_issues | |
| ], | |
| outputs=[output_table, output_chart, output_report] | |
| ) | |
| demo.launch() |