yougandar's picture
Update app.py
ff5c874 verified
import gradio as gr
import pandas as pd
import random
from datetime import datetime
from transformers import pipeline
# Load a Hugging Face model for sentiment analysis (can be adapted for other tasks)
sentiment_analyzer = pipeline("sentiment-analysis")
# Simulated functions for generating data
def get_sensor_data():
return {
"Temperature (°C)": round(random.uniform(20, 35), 2),
"Humidity (%)": round(random.uniform(30, 70), 2),
"Motion Detected": random.choice(["Yes", "No"]),
}
def get_geo_tag_data():
return {
"Device ID": f"Geo-{random.randint(1000, 9999)}",
"Latitude": round(random.uniform(-90.0, 90.0), 6),
"Longitude": round(random.uniform(-180.0, 180.0), 6),
"Last Updated": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
}
def get_rfid_data():
return {
"Tag ID": f"RFID-{random.randint(1000, 9999)}",
"Location": random.choice(["Entry Gate", "Exit Gate", "Warehouse"]),
"Timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
}
# Sentiment analysis for logging data
def analyze_data(data):
text_input = f"Sensor Data: {data['Temperature (°C)']}°C, {data['Humidity (%)']}% Humidity. Motion Detected: {data['Motion Detected']}."
sentiment = sentiment_analyzer(text_input)
return sentiment[0]['label'], sentiment[0]['score']
# Combine all data sources
def get_all_data():
sensor_data = get_sensor_data()
geo_tag_data = get_geo_tag_data()
rfid_data = get_rfid_data()
sentiment_label, sentiment_score = analyze_data(sensor_data)
data = {
"Temperature (°C)": sensor_data["Temperature (°C)"],
"Humidity (%)": sensor_data["Humidity (%)"],
"Motion Detected": sensor_data["Motion Detected"],
"Geo Device ID": geo_tag_data["Device ID"],
"Latitude": geo_tag_data["Latitude"],
"Longitude": geo_tag_data["Longitude"],
"Geo Last Updated": geo_tag_data["Last Updated"],
"RFID Tag ID": rfid_data["Tag ID"],
"RFID Location": rfid_data["Location"],
"RFID Timestamp": rfid_data["Timestamp"],
"Sentiment": sentiment_label,
"Confidence": round(sentiment_score, 2),
}
return pd.DataFrame([data])
# Function to refresh the dashboard
def update_dashboard():
return get_all_data()
# Gradio interface
with gr.Blocks() as dashboard:
gr.Markdown("# 📊 Real-Time Monitoring Dashboard with Hugging Face Integration")
gr.Markdown("This dashboard tracks sensor data, geo-tag locations, and RFID activities with real-time sentiment analysis of logs.")
data_view = gr.Dataframe(
headers=[
"Temperature (°C)", "Humidity (%)", "Motion Detected",
"Geo Device ID", "Latitude", "Longitude", "Geo Last Updated",
"RFID Tag ID", "RFID Location", "RFID Timestamp", "Sentiment", "Confidence"
],
row_count=5,
interactive=False
)
refresh_button = gr.Button("Refresh Data")
refresh_button.click(update_dashboard, outputs=data_view)
dashboard.launch()