Spaces:
Sleeping
Sleeping
Delete app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879import gradio as grimport tensorflow as tfimport numpy as npimport pandas as pdimport matplotlib.pyplot as pltfrom preprocess import preprocess_eeg
# Load the trained modelMODEL_PATH = "cnnGru_fydp.keras"model = tf.keras.models.load_model(MODEL_PATH)
# Emotion labels in the same order as model trainingEMOTIONS = ["funny", "focused", "meditation", "normal", "sad"]
def predict_eeg(file): """Run full pipeline: preprocess EEG → predict → visualize.""" try: # Run preprocessing X_windows, window_timestamps = preprocess_eeg(file) # Predict emotion probabilities preds = model.predict(X_windows) pred_classes = np.argmax(preds, axis=1) # Convert timestamps to seconds times_sec = np.array(window_timestamps) / 1000.0 pred_labels = [EMOTIONS[i] for i in pred_classes]
# --- Plot 1: Emotion vs Time --- fig1, ax1 = plt.subplots(figsize=(8, 3)) ax1.scatter(times_sec, pred_classes, c=pred_classes, cmap='viridis', s=25) ax1.set_yticks(range(len(EMOTIONS))) ax1.set_yticklabels(EMOTIONS) ax1.set_xlabel("Time (s)") ax1.set_title("Predicted Emotion over Time") fig1.tight_layout()
# --- Plot 2: Class distribution --- unique, counts = np.unique(pred_labels, return_counts=True) percentages = (counts / len(pred_labels)) * 100
fig2, ax2 = plt.subplots(figsize=(5, 2.5)) ax2.barh(unique, percentages, color='teal') ax2.set_xlabel("Percentage (%)") ax2.set_title("Overall Emotion Distribution") fig2.tight_layout()
# --- Dominant emotions --- sorted_idx = np.argsort(-percentages) dominant_classes = [(unique[i], round(percentages[i], 1)) for i in sorted_idx[:2]] dominant_text = "\n".join([f"**{c}**: {p}%" for c, p in dominant_classes])
return (fig1, fig2, f"### Dominant Emotions\n{dominant_text}") except Exception as e: return None, None, f"⚠️ Error: {str(e)}"
# --- Gradio UI ---title = "NeuroSense: The Brain IMDb"description = """Upload a preprocessed EEG `.csv` file corresponding to a video stimulus shown to a child. The model predicts emotion states every second and summarizes dominant emotions."""
demo = gr.Interface( fn=predict_eeg, inputs=gr.File(label="Upload EEG CSV File (.csv)"), outputs=[ gr.Plot(label="Emotion vs Time"), gr.Plot(label="Emotion Distribution"), gr.Markdown(label="Dominant Emotions") ], title=title, description=description, theme=gr.themes.Soft(primary_hue="teal", secondary_hue="gray"))
if __name__ == "__main__": demo.launch()app.pyDeleted-79