File size: 2,196 Bytes
b1ef68c
 
 
 
b69ca2b
b1ef68c
 
 
 
23778bd
 
 
 
 
 
 
 
 
 
 
 
 
b1ef68c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b69ca2b
b1ef68c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import numpy as np
import pandas as pd
import gradio as gr
import tensorflow as tf
import spaces

CLASS_NAMES = ["Stationary", "Walking", "Running"]
EXPECTED_STEPS = 100

from tensorflow.keras import Sequential
from tensorflow.keras.layers import Conv1D, MaxPooling1D, Flatten, Dense

MODEL_PATH = "activity_model.weights.h5"

model = Sequential([
    Conv1D(32, kernel_size=5, activation="relu", input_shape=(100, 1)),
    MaxPooling1D(pool_size=2),
    Flatten(),
    Dense(32, activation="relu"),
    Dense(3, activation="softmax")
])
model.load_weights(MODEL_PATH)

def read_signal(file_path):
    if file_path is None:
        raise gr.Error("Please upload a CSV file.")
    df = pd.read_csv(file_path)
    # Accept either a column named sensor_value or the first numeric column.
    if "sensor_value" in df.columns:
        values = df["sensor_value"].to_numpy(dtype=np.float32)
    else:
        numeric = df.select_dtypes(include=["number"])
        if numeric.shape[1] == 0:
            raise gr.Error("CSV must contain a numeric sensor column.")
        values = numeric.iloc[:, 0].to_numpy(dtype=np.float32)
    if len(values) != EXPECTED_STEPS:
        raise gr.Error(f"Expected exactly {EXPECTED_STEPS} sensor readings, found {len(values)}.")
    return values
@spaces.GPU
def predict_activity(file_path):
    values = read_signal(file_path)
    x = values.reshape(1, EXPECTED_STEPS, 1)
    probs = model.predict(x, verbose=0)[0]
    return {name: float(prob) for name, prob in zip(CLASS_NAMES, probs)}

with gr.Blocks() as demo:
    gr.Markdown("# Wearable Activity Classifier")
    gr.Markdown("Upload a CSV containing exactly 100 sensor readings. The student-trained model predicts Stationary, Walking, or Running.")
    file_input = gr.File(label="Sensor CSV", file_types=[".csv"], type="filepath")
    output = gr.Label(label="Prediction", num_top_classes=3)
    button = gr.Button("Predict activity", variant="primary")
    button.click(predict_activity, inputs=file_input, outputs=output)
    gr.Examples(
        examples=[["sample_stationary.csv"], ["sample_walking.csv"], ["sample_running.csv"]],
        inputs=file_input,
        label="Sample files"
    )

demo.launch()