Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from datetime import datetime, timedelta
|
| 5 |
+
import plotly.graph_objects as go
|
| 6 |
+
from plotly.subplots import make_subplots
|
| 7 |
+
|
| 8 |
+
def visualize_sensor_data(file):
|
| 9 |
+
"""Visualize IoT sensor data from uploaded CSV file"""
|
| 10 |
+
if file is None:
|
| 11 |
+
return generate_sample_chart(), "📊 Showing sample IoT sensor data"
|
| 12 |
+
|
| 13 |
+
try:
|
| 14 |
+
df = pd.read_csv(file.name)
|
| 15 |
+
|
| 16 |
+
# Create subplots
|
| 17 |
+
fig = make_subplots(
|
| 18 |
+
rows=2, cols=2,
|
| 19 |
+
subplot_titles=('Temperature (°C)', 'Humidity (%)', 'Vibration (Hz)', 'Power (W)')
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Add traces
|
| 23 |
+
if 'timestamp' in df.columns or 'time' in df.columns:
|
| 24 |
+
x_col = 'timestamp' if 'timestamp' in df.columns else 'time'
|
| 25 |
+
else:
|
| 26 |
+
x_col = df.columns[0]
|
| 27 |
+
|
| 28 |
+
if 'temperature' in df.columns:
|
| 29 |
+
fig.add_trace(go.Scatter(x=df[x_col], y=df['temperature'], name='Temperature', line=dict(color='#ff6b6b')), row=1, col=1)
|
| 30 |
+
if 'humidity' in df.columns:
|
| 31 |
+
fig.add_trace(go.Scatter(x=df[x_col], y=df['humidity'], name='Humidity', line=dict(color='#4ecdc4')), row=1, col=2)
|
| 32 |
+
if 'vibration' in df.columns:
|
| 33 |
+
fig.add_trace(go.Scatter(x=df[x_col], y=df['vibration'], name='Vibration', line=dict(color='#95e1d3')), row=2, col=1)
|
| 34 |
+
if 'power' in df.columns:
|
| 35 |
+
fig.add_trace(go.Scatter(x=df[x_col], y=df['power'], name='Power', line=dict(color='#f38181')), row=2, col=2)
|
| 36 |
+
|
| 37 |
+
fig.update_layout(height=600, showlegend=False, title_text="IoT Sensor Data Visualization")
|
| 38 |
+
|
| 39 |
+
summary = f"✅ Data loaded: {len(df)} records | Columns: {', '.join(df.columns.tolist())}"
|
| 40 |
+
return fig, summary
|
| 41 |
+
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return generate_sample_chart(), f"⚠️ Error: {str(e)}. Showing sample data instead."
|
| 44 |
+
|
| 45 |
+
def generate_sample_chart():
|
| 46 |
+
"""Generate sample IoT sensor data for demonstration"""
|
| 47 |
+
timestamps = pd.date_range(start=datetime.now() - timedelta(hours=24), periods=100, freq='15min')
|
| 48 |
+
|
| 49 |
+
df = pd.DataFrame({
|
| 50 |
+
'timestamp': timestamps,
|
| 51 |
+
'temperature': 20 + np.random.randn(100) * 5,
|
| 52 |
+
'humidity': 60 + np.random.randn(100) * 10,
|
| 53 |
+
'vibration': 50 + np.random.randn(100) * 15,
|
| 54 |
+
'power': 100 + np.random.randn(100) * 20
|
| 55 |
+
})
|
| 56 |
+
|
| 57 |
+
fig = make_subplots(
|
| 58 |
+
rows=2, cols=2,
|
| 59 |
+
subplot_titles=('Temperature (°C)', 'Humidity (%)', 'Vibration (Hz)', 'Power (W)')
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
fig.add_trace(go.Scatter(x=df['timestamp'], y=df['temperature'], name='Temperature', line=dict(color='#ff6b6b')), row=1, col=1)
|
| 63 |
+
fig.add_trace(go.Scatter(x=df['timestamp'], y=df['humidity'], name='Humidity', line=dict(color='#4ecdc4')), row=1, col=2)
|
| 64 |
+
fig.add_trace(go.Scatter(x=df['timestamp'], y=df['vibration'], name='Vibration', line=dict(color='#95e1d3')), row=2, col=1)
|
| 65 |
+
fig.add_trace(go.Scatter(x=df['timestamp'], y=df['power'], name='Power', line=dict(color='#f38181')), row=2, col=2)
|
| 66 |
+
|
| 67 |
+
fig.update_layout(height=600, showlegend=False, title_text="Sample IoT Sensor Data (24 Hours)")
|
| 68 |
+
|
| 69 |
+
return fig
|
| 70 |
+
|
| 71 |
+
# Create Gradio interface
|
| 72 |
+
with gr.Blocks(title="IoT Sensor Visualizer - Anktechsol", theme=gr.themes.Soft()) as demo:
|
| 73 |
+
gr.Markdown("""
|
| 74 |
+
# 🌐 IoT Sensor Visualizer for Edge AI Projects
|
| 75 |
+
### by **Anktechsol** - AI + IoT Experts
|
| 76 |
+
|
| 77 |
+
Upload your IoT sensor CSV file to visualize temperature, humidity, vibration, and power data.
|
| 78 |
+
**No file?** We'll show you sample data!
|
| 79 |
+
|
| 80 |
+
**Expected CSV format:** `timestamp, temperature, humidity, vibration, power`
|
| 81 |
+
""")
|
| 82 |
+
|
| 83 |
+
with gr.Row():
|
| 84 |
+
with gr.Column(scale=1):
|
| 85 |
+
file_input = gr.File(label="📁 Upload CSV File", file_types=[".csv"])
|
| 86 |
+
visualize_btn = gr.Button("📊 Visualize Data", variant="primary", size="lg")
|
| 87 |
+
gr.Markdown("""---
|
| 88 |
+
### 🔗 Links
|
| 89 |
+
- [Anktechsol Website](https://anktechsol.com)
|
| 90 |
+
- [More AIoT Tools](https://huggingface.co/anktechsol)
|
| 91 |
+
""")
|
| 92 |
+
|
| 93 |
+
with gr.Column(scale=3):
|
| 94 |
+
plot_output = gr.Plot(label="Sensor Data Visualization")
|
| 95 |
+
status_output = gr.Textbox(label="Status", interactive=False)
|
| 96 |
+
|
| 97 |
+
# Event handler
|
| 98 |
+
visualize_btn.click(
|
| 99 |
+
fn=visualize_sensor_data,
|
| 100 |
+
inputs=[file_input],
|
| 101 |
+
outputs=[plot_output, status_output]
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
# Auto-load sample data on startup
|
| 105 |
+
demo.load(fn=lambda: (generate_sample_chart(), "📊 Sample data loaded. Upload your CSV to visualize your IoT data!"),
|
| 106 |
+
outputs=[plot_output, status_output])
|
| 107 |
+
|
| 108 |
+
if __name__ == "__main__":
|
| 109 |
+
demo.launch()
|