| | import gradio as gr |
| | import pandas as pd |
| | import numpy as np |
| | from datetime import datetime, timedelta |
| | import plotly.graph_objects as go |
| | from plotly.subplots import make_subplots |
| |
|
| | def visualize_sensor_data(file): |
| | """Visualize IoT sensor data from uploaded CSV file""" |
| | if file is None: |
| | return generate_sample_chart(), "π Showing sample IoT sensor data" |
| | |
| | try: |
| | df = pd.read_csv(file.name) |
| | |
| | |
| | fig = make_subplots( |
| | rows=2, cols=2, |
| | subplot_titles=('Temperature (Β°C)', 'Humidity (%)', 'Vibration (Hz)', 'Power (W)') |
| | ) |
| | |
| | |
| | if 'timestamp' in df.columns or 'time' in df.columns: |
| | x_col = 'timestamp' if 'timestamp' in df.columns else 'time' |
| | else: |
| | x_col = df.columns[0] |
| | |
| | if 'temperature' in df.columns: |
| | fig.add_trace(go.Scatter(x=df[x_col], y=df['temperature'], name='Temperature', line=dict(color='#ff6b6b')), row=1, col=1) |
| | if 'humidity' in df.columns: |
| | fig.add_trace(go.Scatter(x=df[x_col], y=df['humidity'], name='Humidity', line=dict(color='#4ecdc4')), row=1, col=2) |
| | if 'vibration' in df.columns: |
| | fig.add_trace(go.Scatter(x=df[x_col], y=df['vibration'], name='Vibration', line=dict(color='#95e1d3')), row=2, col=1) |
| | if 'power' in df.columns: |
| | fig.add_trace(go.Scatter(x=df[x_col], y=df['power'], name='Power', line=dict(color='#f38181')), row=2, col=2) |
| | |
| | fig.update_layout(height=600, showlegend=False, title_text="IoT Sensor Data Visualization") |
| | |
| | summary = f"β
Data loaded: {len(df)} records | Columns: {', '.join(df.columns.tolist())}" |
| | return fig, summary |
| | |
| | except Exception as e: |
| | return generate_sample_chart(), f"β οΈ Error: {str(e)}. Showing sample data instead." |
| |
|
| | def generate_sample_chart(): |
| | """Generate sample IoT sensor data for demonstration""" |
| | timestamps = pd.date_range(start=datetime.now() - timedelta(hours=24), periods=100, freq='15min') |
| | |
| | df = pd.DataFrame({ |
| | 'timestamp': timestamps, |
| | 'temperature': 20 + np.random.randn(100) * 5, |
| | 'humidity': 60 + np.random.randn(100) * 10, |
| | 'vibration': 50 + np.random.randn(100) * 15, |
| | 'power': 100 + np.random.randn(100) * 20 |
| | }) |
| | |
| | fig = make_subplots( |
| | rows=2, cols=2, |
| | subplot_titles=('Temperature (Β°C)', 'Humidity (%)', 'Vibration (Hz)', 'Power (W)') |
| | ) |
| | |
| | fig.add_trace(go.Scatter(x=df['timestamp'], y=df['temperature'], name='Temperature', line=dict(color='#ff6b6b')), row=1, col=1) |
| | fig.add_trace(go.Scatter(x=df['timestamp'], y=df['humidity'], name='Humidity', line=dict(color='#4ecdc4')), row=1, col=2) |
| | fig.add_trace(go.Scatter(x=df['timestamp'], y=df['vibration'], name='Vibration', line=dict(color='#95e1d3')), row=2, col=1) |
| | fig.add_trace(go.Scatter(x=df['timestamp'], y=df['power'], name='Power', line=dict(color='#f38181')), row=2, col=2) |
| | |
| | fig.update_layout(height=600, showlegend=False, title_text="Sample IoT Sensor Data (24 Hours)") |
| | |
| | return fig |
| |
|
| | |
| | with gr.Blocks(title="IoT Sensor Visualizer - Anktechsol", theme=gr.themes.Soft()) as demo: |
| | gr.Markdown(""" |
| | # π IoT Sensor Visualizer for Edge AI Projects |
| | ### by **Anktechsol** - AI + IoT Experts |
| | |
| | Upload your IoT sensor CSV file to visualize temperature, humidity, vibration, and power data. |
| | **No file?** We'll show you sample data! |
| | |
| | **Expected CSV format:** `timestamp, temperature, humidity, vibration, power` |
| | """) |
| | |
| | with gr.Row(): |
| | with gr.Column(scale=1): |
| | file_input = gr.File(label="π Upload CSV File", file_types=[".csv"]) |
| | visualize_btn = gr.Button("π Visualize Data", variant="primary", size="lg") |
| | gr.Markdown("""--- |
| | ### π Links |
| | - [Anktechsol Website](https://anktechsol.com) |
| | - [More AIoT Tools](https://huggingface.co/anktechsol) |
| | """) |
| | |
| | with gr.Column(scale=3): |
| | plot_output = gr.Plot(label="Sensor Data Visualization") |
| | status_output = gr.Textbox(label="Status", interactive=False) |
| | |
| | |
| | visualize_btn.click( |
| | fn=visualize_sensor_data, |
| | inputs=[file_input], |
| | outputs=[plot_output, status_output] |
| | ) |
| | |
| | |
| | demo.load(fn=lambda: (generate_sample_chart(), "π Sample data loaded. Upload your CSV to visualize your IoT data!"), |
| | outputs=[plot_output, status_output]) |
| |
|
| | if __name__ == "__main__": |
| | demo.launch() |