Snigs98's picture
Update app.py
1a45e9e verified
import gradio as gr
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import load_model
from sklearn.preprocessing import StandardScaler
import pandas as pd
# Load model and label encoder
model = load_model("human_activity_recognition_model.h5")
label_encoder = {0: "Walking", 1: "Running", 2: "Jumping", 3: "Sitting"} # Adjust as per your dataset
# Sample scaler (ensure this matches what you used during training)
scaler = StandardScaler()
# Define prediction function
def predict_activity(x, y, z):
sample_input = np.array([[x, y, z]])
sample_input = scaler.fit_transform(sample_input) # Apply the same scaling as training
sample_input = np.expand_dims(sample_input, axis=0) # Reshape for LSTM input
prediction = model.predict(sample_input)
predicted_label = label_encoder[np.argmax(prediction)]
return f"Predicted Activity: {predicted_label}"
# Create Gradio Interface
demo = gr.Interface(
fn=predict_activity,
inputs=[gr.Number(label="X-axis"), gr.Number(label="Y-axis"), gr.Number(label="Z-axis")],
outputs="text",
title="Human Activity Recognition",
description="Enter accelerometer values (X, Y, Z) to predict activity.",
)
# Launch the app
demo.launch()