Spaces:
Sleeping
Sleeping
Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209# app.py - NeuroSense 2.0: FULLY FIXED & STUNNING# Landing + About + Analyzer + Clean Nav + NO BUGSimport gradio as grimport numpy as npfrom tensorflow.keras.models import load_modelfrom preprocess import preprocess_eegimport timemodel = 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 = Falsedef get_emotion_html(idx):if not emotions_list or idx >= len(emotions_list):idx = 0emo = emotions_list[idx]return f"<div style='text-align:center; padding:60px; font-size:5.5rem; background:{COLOR_MAP[emo]}; color:white; border-radius:35px; font-weight:bold; box-shadow: 0 0 30px rgba(0,255,255,0.4);'>{emo}<br><small style='font-size:2.2rem; opacity:0.9;'>Second {idx}</small></div>"# === LOAD CSV ===def load_csv(csv_file):global emotions_list, duration, current_idx, is_playingif 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]) + 1emotions_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 = 0is_playing = Falsereturn (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_playingis_playing = not is_playingreturn is_playingdef play_loop():global current_idx, is_playing, durationif not emotions_list or not is_playing:returnwhile is_playing and current_idx < duration - 1:current_idx += 1yield (get_emotion_html(current_idx),current_idx,"PAUSE")time.sleep(1)# NEVER LET IT DISAPPEARis_playing = Falseyield (get_emotion_html(current_idx),current_idx,"PLAY")def seek_to(sec):global current_idx, is_playingsec = int(sec)if sec >= len(emotions_list):sec = len(emotions_list) - 1current_idx = secis_playing = Falsereturn (get_emotion_html(sec),sec,"PLAY")# === PAGES CONTENT ===landing_html = """<div style="text-align:center; padding:100px 20px; font-family: 'Segoe UI', sans-serif;"><h1 style="font-size:5rem; background: linear-gradient(90deg, #00ff88, #aa00ff); -webkit-background-clip: text; color:transparent; margin:0;">NeuroSense</h1><p style="font-size:2rem; color:#00ff88; margin:20px 0;">Real-time Emotion from Your Brain</p><p style="font-size:1.3rem; color:#ccc; max-width:800px; margin:40px auto;">Upload EEG data. Watch your emotions unfold β one per second.<br>No charts. Just pure feeling.</p><button onclick="window.location.href='/results'" style="background:#00ff88; color:black; border:none; padding:18px 50px; border-radius:50px; font-size:1.5rem; font-weight:bold; cursor:pointer;">START ANALYSIS</button></div>"""about_html = """<div style="padding:80px 40px; text-align:center; font-family: 'Segoe UI', sans-serif; line-height:1.8;"><h1 style="font-size:3.5rem; color:#00ff88;">About NeuroSense</h1><p style="font-size:1.4rem; color:#ddd; max-width:900px; margin:40px auto;">We turned raw brain waves into <strong>live emotions</strong>.</p><p style="font-size:1.2rem; color:#aaa; max-width:800px; margin:30px auto;">Using CNN + GRU deep learning, we detect: <br><strong>Focused Β· Funny Β· Meditation Β· Normal Β· Sad</strong></p><p style="font-size:1.2rem; color:#aaa; max-width:800px; margin:30px auto;">Built through 47 versions of Gradio hell.<br>Now unbreakable. Now beautiful.</p><p style="color:#00ff88; font-weight:bold; margin-top:50px;">Your mind. Visualized.</p></div>"""# === MAIN APP ===with gr.Blocks(theme=gr.themes.Soft(), title="NeuroSense", css="""body {background: linear-gradient(135deg, #0f0c29, #302b63, #24243e);}.gradio-container {max-width: 1300px !important; margin: auto; padding: 0;}nav {background: rgba(10,10,20,0.9); backdrop-filter: blur(10px); padding: 20px; border-radius: 0 0 20px 20px; margin: -20px -20px 40px -20px; text-align: center; box-shadow: 0 10px 30px rgba(0,0,0,0.5);}nav a {color: #00ff88; margin: 0 30px; font-weight: bold; text-decoration: none; font-size: 1.3rem; transition: 0.3s;}nav a:hover {color: #ffaa00; text-shadow: 0 0 10px #ffaa00;}""") as demo:# CUSTOM NAV BAR (NO GRADIO DEFAULT BAR)gr.HTML("""<nav><a href="/">Home</a><a href="/results">Analyzer</a><a href="/about">About</a></nav>""")with gr.Tabs(elem_id="main-tabs") as tabs:with gr.Tab("Home", id="home"):gr.HTML(landing_html)with gr.Tab("Analyzer", id="results"):gr.Markdown("### 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:100px; 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).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)with gr.Tab("About", id="about"):gr.HTML(about_html)print("NEUROSENSE 2.0 β FIXED, CLEAN, BEAUTIFUL β LIVE NOW")demo.queue(max_size=20)demo.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231# landing.py - NeuroSense: OFFICIAL LANDING PAGE β CLEAN, BRAINY, FUTURISTIC# Full scroll, 3 team cards, supervisor card, product description# Clean navbar: NeuroSense left | links rightimport gradio as gr# === FULL HTML + CSS (ONE FILE, ZERO DEPENDENCIES) ===landing_html = """<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>NeuroSense β The Brain IMDb</title><style>@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@500;700&family=Roboto:wght@300;400;500&display=swap');* { margin: 0; padding: 0; box-sizing: border-box; }body {background: linear-gradient(135deg, #0a0a1a 0%, #1a1a2e 50%, #16213e 100%);color: #e0e0ff;font-family: 'Roboto', sans-serif;line-height: 1.7;overflow-x: hidden;}nav {position: fixed;top: 0;left: 0;width: 100%;padding: 20px 8%;display: flex;justify-content: space-between;align-items: center;background: rgba(10, 10, 30, 0.85);backdrop-filter: blur(12px);z-index: 1000;border-bottom: 1px solid rgba(0, 255, 136, 0.2);}.logo {font-family: 'Orbitron', sans-serif;font-size: 2rem;font-weight: 700;background: linear-gradient(90deg, #00ff88, #00ffff);-webkit-background-clip: text;color: transparent;letter-spacing: 2px;}.nav-links a {color: #e0e0ff;margin: 0 20px;text-decoration: none;font-weight: 500;transition: 0.3s;position: relative;}.nav-links a:hover {color: #00ff88;}.nav-links a::after {content: '';position: absolute;width: 0;height: 2px;bottom: -6px;left: 0;background: #00ff88;transition: 0.3s;}.nav-links a:hover::after { width: 100%; }.hero {height: 100vh;display: flex;flex-direction: column;justify-content: center;align-items: center;text-align: center;padding: 0 10%;position: relative;}.hero h1 {font-family: 'Orbitron', sans-serif;font-size: 6.5rem;margin-bottom: 20px;background: linear-gradient(90deg, #00ff88, #00ffff, #aa00ff);-webkit-background-clip: text;color: transparent;text-shadow: 0 0 30px rgba(0, 255, 136, 0.3);}.hero p {font-size: 2.2rem;color: #00ff88;font-weight: 300;letter-spacing: 1px;}section { padding: 100px 10%; }.team-section h2, .product-section h2 {text-align: center;font-size: 3rem;margin-bottom: 70px;color: #00ffff;font-family: 'Orbitron', sans-serif;}.team-grid {display: grid;grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));gap: 40px;max-width: 1200px;margin: 0 auto;}.card {background: rgba(20, 20, 50, 0.6);border-radius: 20px;padding: 40px 30px;text-align: center;border: 1px solid rgba(0, 255, 136, 0.3);transition: 0.4s;backdrop-filter: blur(10px);}.card:hover {transform: translateY(-15px);box-shadow: 0 20px 40px rgba(0, 255, 136, 0.2);border-color: #00ff88;}.card h3 {font-size: 1.8rem;color: #00ff88;margin-bottom: 10px;}.card p {color: #aaa;font-size: 1.1rem;}.supervisor {background: linear-gradient(135deg, rgba(170, 0, 255, 0.2), rgba(0, 255, 136, 0.2));border: 2px solid #aa00ff;}.product-section {background: rgba(10, 10, 30, 0.4);margin: 50px 0;border-radius: 30px;padding: 80px 10%;}.product-section p {font-size: 1.4rem;max-width: 900px;margin: 30px auto;color: #ccc;}footer {text-align: center;padding: 50px;color: #666;font-size: 0.9rem;}</style></head><body><nav><div class="logo">NeuroSense</div><div class="nav-links"><a href="/">Home</a><a href="/upload">Upload</a><a href="/analyzer">Analyzer</a><a href="/results">Results</a></div></nav><div class="hero"><h1>NeuroSense</h1><p>The Brain IMDb</p></div><section class="team-section"><h2>Our Team</h2><div class="team-grid"><div class="card"><h3>Team Member 1</h3><p>Roll Number: 21I-1234</p></div><div class="card"><h3>Team Member 2</h3><p>Roll Number: 21I-5678</p></div><div class="card"><h3>Team Member 3</h3><p>Roll Number: 21I-9012</p></div></div><div style="margin-top: 80px; text-align:center;"><div class="card supervisor" style="max-width: 500px; margin: 0 auto;"><h3>Dr. Danish Arif</h3><p><strong>Project Supervisor</strong></p></div></div></section><section class="product-section"><h2>What is NeuroSense?</h2><p>NeuroSense is a revolutionary real-time emotion detection system that transforms raw EEG brainwave data into live, human-readable emotions β <strong>one per second</strong>.</p><p>Using advanced deep learning (CNN + GRU), we detect five core emotional states: <strong>Focused</strong>, <strong>Funny</strong>, <strong>Meditation</strong>, <strong>Normal</strong>, and <strong>Sad</strong>.</p><p>No charts. No lag. Just your mind β visualized.</p><p style="color:#00ff88; font-weight:bold; margin-top:30px;">The future of brain-computer interface starts here.</p></section><footer>Β© 2025 NeuroSense β Final Year Design Project<br>Built with passion, caffeine, and 47 versions of unbreakable code.</footer></body></html>"""with gr.Blocks() as demo:gr.HTML(landing_html)print("NEUROSENSE LANDING PAGE β LIVE & GORGEOUS")demo.launch()
app.py+221-199