Spaces:
Sleeping
Sleeping
Create app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221# app.py - NeuroSense: FULL PRODUCTION APP# Landing Page + About Us + Results Page + Nav Bar + FULLY WORKING Analyzer
import gradio as grimport numpy as npfrom tensorflow.keras.models import load_modelfrom preprocess import preprocess_eegimport time
# Load modelmodel = load_model("cnnGru_fydp.keras", compile=False)print("Model loaded!")
CLASSES = ["Focused", "Funny", "Meditation", "Normal", "Sad"]COLOR_MAP = { "Focused": "#00ff88", "Funny": "#ffaa00", "Meditation": "#aa00ff", "Normal": "#888888", "Sad": "#3366ff"}
# Global stateemotions_list = []duration = 0current_idx = 0is_playing = False
# === EMOTION HTML HELPER ===def get_emotion_html(idx): if not emotions_list or idx >= len(emotions_list): idx = 0 emo = emotions_list[idx] return f"<div style='text-align:center; padding:50px; font-size:5rem; background:{COLOR_MAP[emo]}; color:white; border-radius:30px; font-weight:bold;'>{emo}<br><small style='font-size:2rem;'>Second {idx}</small></div>"
# === ANALYZER PAGE (RESULTS) ===def load_csv(csv_file): global emotions_list, duration, current_idx, is_playing if not csv_file: return "Upload CSV", "Error", gr.update(), 0, "PLAY"
X, timestamps_ms = preprocess_eeg(csv_file.name) preds = model.predict(X, verbose=0) pred_labels = [CLASSES[np.argmax(p)] for p in preds]
timestamps_sec = [t / 1000.0 for t in timestamps_ms] duration = int(timestamps_sec[-1]) + 1
emotions_list = [] for sec in range(duration): idxs = [i for i, t in enumerate(timestamps_sec) if sec <= t < sec + 1] emo = pred_labels[idxs[-1]] if idxs else pred_labels[-1] emotions_list.append(emo)
current_idx = 0 is_playing = False
return ( get_emotion_html(0), f"Loaded: {duration} seconds", gr.update(minimum=0, maximum=duration-1, value=0, step=1, interactive=True), 0, "PLAY" )
def toggle_play_pause(): global is_playing is_playing = not is_playing return is_playing
def play_loop(): global current_idx, is_playing, duration if not emotions_list or not is_playing: return
while is_playing and current_idx < duration - 1: current_idx += 1 yield ( get_emotion_html(current_idx), current_idx, "PAUSE" ) time.sleep(1)
is_playing = False yield (get_emotion_html(current_idx), current_idx, "PLAY")
def seek_to(sec): global current_idx, is_playing sec = int(sec) if sec >= len(emotions_list): sec = len(emotions_list) - 1 current_idx = sec is_playing = False emo = emotions_list[sec] return ( get_emotion_html(sec), sec, "PLAY" )
# === PAGES ===def landing_page(): return gr.update(value=""" <div style="text-align:center; padding:80px; font-family: 'Segoe UI', sans-serif;"> <h1 style="font-size:4rem; background: linear-gradient(90deg, #00ff88, #aa00ff); -webkit-background-clip: text; color:transparent;"> NeuroSense </h1> <p style="font-size:1.8rem; color:#ccc;">Real-time Emotion Detection from EEG</p> <p style="font-size:1.2rem; color:#aaa; max-width:700px; margin:30px auto;"> Upload your brainwave data. Watch your mind come alive.<br> One emotion per second. Like reading thoughts. </p> <div style="margin:40px;"> <a href="/results" style="background:#00ff88; color:black; padding:16px 40px; border-radius:50px; font-weight:bold; text-decoration:none; font-size:1.3rem;"> START ANALYSIS </a> </div> </div> """)
def about_page(): return gr.update(value=""" <div style="padding:60px; font-family: 'Segoe UI', sans-serif; line-height:1.8;"> <h1 style="text-align:center; font-size:3rem; color:#00ff88;">About NeuroSense</h1> <p style="font-size:1.3rem; color:#ddd; max-width:800px; margin:40px auto;"> We built the world's first <strong>real-time emotion visualizer</strong> for raw EEG data. </p> <p style="font-size:1.2rem; color:#aaa; max-width:800px; margin:30px auto;"> Using deep learning (CNN + GRU), we detect 5 emotions: <strong>Focused</strong>, <strong>Funny</strong>, <strong>Meditation</strong>, <strong>Normal</strong>, and <strong>Sad</strong> — <em>one per second</em>. </p> <p style="font-size:1.2rem; color:#aaa; max-width:800px; margin:30px auto;"> No plots. No lag. Just pure emotion. </p> <div style="text-align:center; margin:50px;"> <strong>Made with blood, sweat, and 47 versions of Gradio hell.</strong><br> <em>Now unbreakable.</em> </div> </div> """)
def results_page(): with gr.Blocks() as results: gr.Markdown("### NeuroSense — Real-Time Emotion Stream") with gr.Row(): csv_in = gr.File(label="Upload EEG CSV", file_types=[".csv"]) load_btn = gr.Button("LOAD", variant="primary", size="lg")
emotion_box = gr.HTML(value="<div style='text-align:center; padding:80px; font-size:3rem; background:#333; color:#666; border-radius:30px;'>Upload CSV to begin</div>") status = gr.Textbox(label="Status", value="Ready")
with gr.Row(): play_pause_btn = gr.Button("PLAY", variant="primary", size="lg") slider = gr.Slider(0, 60, value=0, step=1, label="Second", interactive=True)
idx_state = gr.State(0)
load_btn.click( load_csv, inputs=csv_in, outputs=[emotion_box, status, slider, idx_state, play_pause_btn] )
play_pause_btn.click( toggle_play_pause, outputs=idx_state ).then( lambda: None, outputs=None ).then( lambda: "PAUSE" if is_playing else "PLAY", outputs=play_pause_btn ).then( play_loop, outputs=[emotion_box, idx_state, play_pause_btn] )
slider.change( seek_to, inputs=slider, outputs=[emotion_box, idx_state, play_pause_btn] ).then( lambda x: x, inputs=idx_state, outputs=slider ) return results
# === MAIN APP WITH NAV BAR ===with gr.Blocks(theme=gr.themes.Soft(), title="NeuroSense", css=""" .gradio-container {max-width: 1200px !important;} nav {background: #111 !important; padding: 20px; border-radius: 15px; margin-bottom: 30px;} nav a {color: #00ff88; margin: 0 20px; font-weight: bold; text-decoration: none;} nav a:hover {color: #ffaa00;}""") as demo:
gr.HTML(""" <nav style="text-align:center;"> <a href="/">Home</a> <a href="/results">Analyzer</a> <a href="/about">About</a> </nav> """)
with gr.Tabs(): with gr.TabItem("Home", id="home"): landing = gr.HTML() landing.change(None, None, None).then(landing_page, outputs=landing)
with gr.TabItem("Analyzer", id="results"): results_page()
with gr.TabItem("About", id="about"): about = gr.HTML() about.change(None, None, None).then(about_page, outputs=about)
# Auto-load landing page demo.load(landing_page, outputs=landing)
print("LAUNCHING NEUROSENSE FULL APP — LANDING + ABOUT + RESULTS + NAV — UNBREAKABLE")demo.queue(max_size=20)demo.launch()app.pyAdded+221