pandas import time import random import pandas as pd import gradio as gr # Global list to store collected device metrics (acts as our dataset) dataset_logs = [] def collect_metrics(): """Generates device health readings, appends to the dataset, and returns values for the UI. """ # Simulate dynamic RAM and CPU Temperature readings ram_percent = round(random.uniform(45.0, 85.0), 1) cpu_temp = round(random.uniform(40.0, 72.0), 1) timestamp = time.strftime("%H:%M:%S") # Save current reading to our dataset dataset_logs.append({ "Timestamp": timestamp, "RAM Usage (%)": ram_percent, "CPU Temp (°C)": cpu_temp }) # Convert list to DataFrame for UI display df = pd.DataFrame(dataset_logs) status_text = f"Last Updated: {timestamp} | RAM: {ram_percent}% | Temp: {cpu_temp}°C" return ram_percent, cpu_temp, status_text, df # Build UI with Gradio with gr.Blocks(title="AI Device & Resource Monitor") as demo: gr.Markdown("# Device Resource & Health Monitor") gr.Markdown("A lightweight AI Lab project tracking real-time **RAM usage** and **CPU temperature**.") with gr.Row(): ram_box = gr.Number(label="RAM Usage (%)") temp_box = gr.Number(label="CPU Temperature (°C)") status_box = gr.Textbox(label="System Status", interactive=False) refresh_btn = gr.Button("Refresh Metrics & Update Dataset", variant="primary") gr.Markdown("### Log Dataset (Historical Readings)") log_table = gr.Dataframe(interactive=False) refresh_btn.click( fn=collect_metrics, inputs=[], outputs=[ram_box, temp_box, status_box, log_table] ) demo.load( fn=collect_metrics, inputs=[], outputs=[ram_box, temp_box, status_box, log_table] ) demo.launch()