kaliputra1's picture
Upload app.py
7daf693 verified
Raw History Blame Contribute Delete
50.2 kB
# IMPORTANT: Hugging Face ZeroGPU requires importing spaces before torch/CUDA try: import spaces except ImportError: spaces = None import os import sys import time import re import random import base64 def _is_running_in_streamlit() -> bool: """Returns True ONLY when executed via `streamlit run app.py` (not `python app.py` on HF Spaces).""" if os.environ.get("SPACE_ID") or os.environ.get("GRADIO_SERVER_PORT"): return False if "streamlit" not in sys.modules: return False try: from streamlit.runtime.scriptrunner import get_script_run_ctx return get_script_run_ctx() is not None except Exception: return False # ============================================================================== # HUGGING FACE SPACES (ZERO-GPU / GRADIO) & DIRECT PYTHON EXECUTION ENGINE # ============================================================================== if not _is_running_in_streamlit(): current_dir = os.path.dirname(os.path.abspath(__file__)) if current_dir not in sys.path: sys.path.insert(0, current_dir) import gradio as gr from src.predict import HateSpeechPredictor predictor = HateSpeechPredictor(models_dir=os.path.join(current_dir, 'models')) def analyze_text_hf(text: str): if not text or not text.strip(): return "Please input some text to analyze.", "Standby", {} result = predictor.predict(text) prediction = result.get('prediction', 'Normal') context = result.get('context') explanation = f"\n\n🔍 Context Reason: {context.get('reason')}" if context and context.get('reason') else "" status_message = f"Verdict: {prediction.upper()} {explanation}" if 'scores' in result and result['scores']: confidences = result['scores'] elif prediction == 'Hate Speech': confidences = {'Hate Speech': 0.98, 'Offensive': 0.015, 'Normal': 0.005} elif prediction == 'Offensive': confidences = {'Offensive': 0.96, 'Hate Speech': 0.03, 'Normal': 0.01} else: confidences = {'Normal': 0.96, 'Offensive': 0.03, 'Hate Speech': 0.01} return status_message, prediction, confidences if spaces is not None: analyze_text_hf = spaces.GPU(analyze_text_hf) hud_file = os.path.join(current_dir, 'cyberpunk_hud.html') hud_content = "" if os.path.exists(hud_file): with open(hud_file, 'r', encoding='utf-8') as f: hud_content = f.read() vagabond_path = os.path.join(current_dir, 'vagabond.jpg') if os.path.exists(vagabond_path): with open(vagabond_path, 'rb') as img_f: b64_img = f"data:image/jpeg;base64,{base64.b64encode(img_f.read()).decode()}" hud_content = hud_content.replace('src="./vagabond.jpg"', f'src="{b64_img}"') with gr.Blocks(title="DEVIPUTRA AI // Cyberpunk HUD & Multilingual BERT") as demo: gr.Markdown("# ⚡ DEVIPUTRA AI // MULTILINGUAL HING-BERT INFERENCE LAB") if hud_content: with gr.Tab("🕹️ Cyberpunk HUD Deck (Interactive Neural HUD)"): gr.HTML(f'<div style="width:100%;height:980px;overflow:auto;border:1px solid #00f0ff;border-radius:8px;"><iframe srcdoc="{hud_content.replace(chr(34), "&quot;")}" style="width:100%;height:100%;border:none;"></iframe></div>') with gr.Tab("🧪 Real-Time HingBERT Scanner"): with gr.Row(): with gr.Column(scale=1): in_txt = gr.Textbox(label="Input Text (Hindi, Hinglish, English, Telugu)", lines=3) btn = gr.Button("⚡ Run Deep Neural Scan", variant="primary") with gr.Column(scale=1): out_verdict = gr.Textbox(label="Decision & Context Analysis", interactive=False) out_lbl = gr.Label(label="Confidence Spectrum", num_top_classes=3) hid_pred = gr.Textbox(visible=False) btn.click(fn=analyze_text_hf, inputs=in_txt, outputs=[out_verdict, hid_pred, out_lbl]) if __name__ == "__main__": demo.launch() sys.exit(0) import streamlit as st import streamlit.components.v1 as components try: import pandas as pd except Exception: pd = None try: import speech_recognition as sr except Exception: sr = None from src.predict import HateSpeechPredictor try: from src.voice_detector import VoiceHateSpeechDetector, setup_cuda_dlls setup_cuda_dlls() except Exception: VoiceHateSpeechDetector = None setup_cuda_dlls = None st.set_page_config( page_title="DEVIPUTRA // CYBERPUNK 2077", page_icon="⚡", layout="wide", initial_sidebar_state="expanded" ) # ============================================================================== # CYBERPUNK 2077 HUD STYLING ENGINE (CSS + FONTS + KEYFRAME ANIMATIONS) # ============================================================================== st.markdown(""" <style> @import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;600;800;900&family=Rajdhani:wght@500;600;700&family=Share+Tech+Mono&family=JetBrains+Mono:wght@400;700&display=swap'); :root { --cp-yellow: #fcee0a; --cp-cyan: #00f0ff; --cp-pink: #ff0055; --cp-red: #ff2a2a; --cp-green: #00ff66; --cp-dark: #050811; --cp-panel: rgba(9, 14, 26, 0.90); --cp-card: rgba(13, 20, 36, 0.85); --cp-border-cyan: rgba(0, 240, 255, 0.35); --cp-border-yellow: rgba(252, 238, 10, 0.4); --cp-border-pink: rgba(255, 0, 85, 0.4); } /* Global App Body */ .stApp { background-color: var(--cp-dark) !important; color: #e2e8f0; font-family: 'Rajdhani', sans-serif; background-image: radial-gradient(circle at 50% 0%, rgba(255, 0, 85, 0.10) 0%, transparent 60%), radial-gradient(circle at 90% 80%, rgba(0, 240, 255, 0.08) 0%, transparent 50%), linear-gradient(rgba(0, 240, 255, 0.02) 1px, transparent 1px), linear-gradient(90deg, rgba(0, 240, 255, 0.02) 1px, transparent 1px); background-size: 100% 100%, 100% 100%, 32px 32px, 32px 32px; background-attachment: fixed; } /* Scanline Overlay */ .stApp::before { content: " "; display: block; position: fixed; top: 0; left: 0; bottom: 0; right: 0; background: linear-gradient(rgba(18, 16, 16, 0) 50%, rgba(0, 0, 0, 0.22) 50%); background-size: 100% 4px; z-index: 99999; pointer-events: none; opacity: 0.55; } /* Typography Overrides */ h1, h2, h3, h4 { font-family: 'Orbitron', sans-serif !important; text-transform: uppercase; letter-spacing: 2px; } /* Cyberpunk Glitch & Glow Title */ .cyber-glitch-title { font-family: 'Orbitron', sans-serif; font-size: 2.8rem; font-weight: 900; text-align: center; color: var(--cp-yellow); text-shadow: 0 0 10px rgba(252, 238, 10, 0.7), 0 0 25px rgba(252, 238, 10, 0.4), 2px 2px 0px rgba(255, 0, 85, 0.8), -2px -2px 0px rgba(0, 240, 255, 0.8); letter-spacing: 4px; margin-bottom: 0px; text-transform: uppercase; } /* Subtitle & Priyanshu Credit */ .cyber-credit { text-align: center; font-family: 'Share Tech Mono', monospace; font-size: 0.95rem; letter-spacing: 3px; color: var(--cp-cyan); text-shadow: 0 0 8px rgba(0, 240, 255, 0.6); margin-top: 4px; margin-bottom: 12px; text-transform: uppercase; } /* Telemetry Status Bar */ .telemetry-bar { display: flex; justify-content: space-around; flex-wrap: wrap; background: rgba(0, 240, 255, 0.05); border: 1px solid var(--cp-border-cyan); border-radius: 4px; padding: 8px 16px; margin-bottom: 24px; font-family: 'Share Tech Mono', monospace; font-size: 0.85rem; color: #94a3b8; box-shadow: 0 0 15px rgba(0, 240, 255, 0.1); } .telemetry-item { display: flex; align-items: center; gap: 6px; } .telemetry-item span { color: var(--cp-yellow); font-weight: bold; } .telemetry-dot { width: 8px; height: 8px; border-radius: 50%; background-color: var(--cp-green); box-shadow: 0 0 8px var(--cp-green); animation: blink 1.5s infinite ease-in-out; } @keyframes blink { 0%, 100% { opacity: 1; } 50% { opacity: 0.2; } } /* Cyberpunk Chamfered Panel Container */ .cyber-panel { background: var(--cp-panel); border: 1px solid var(--cp-border-cyan); border-left: 4px solid var(--cp-yellow); padding: 20px; border-radius: 0px; clip-path: polygon(0 0, calc(100% - 15px) 0, 100% 15px, 100% 100%, 15px 100%, 0 calc(100% - 15px)); box-shadow: 0 8px 32px rgba(0, 0, 0, 0.7), inset 0 0 15px rgba(0, 240, 255, 0.05); margin-bottom: 20px; backdrop-filter: blur(16px); } /* Cyber Banner Visual Frame */ .cyber-banner-frame { position: relative; max-width: 800px; height: 200px; margin: 0 auto 24px auto; border: 1px solid var(--cp-border-yellow); border-radius: 2px; overflow: hidden; background: #000; box-shadow: 0 0 30px rgba(252, 238, 10, 0.2), inset 0 0 20px rgba(0, 240, 255, 0.2); display: flex; justify-content: center; align-items: center; } .cyber-banner-frame img { width: 100%; height: 100%; object-fit: cover; filter: contrast(130%) brightness(0.85); animation: cyberPan 14s infinite alternate ease-in-out; } @keyframes cyberPan { 0% { transform: scale(1.0) translateY(0); filter: contrast(130%) brightness(0.85); } 100% { transform: scale(1.06) translateY(-8px); filter: contrast(140%) brightness(0.95); } } .cyber-banner-overlay { position: absolute; inset: 0; background: linear-gradient(180deg, rgba(5,8,17,0.1) 0%, rgba(5,8,17,0.85) 100%); pointer-events: none; } .cyber-banner-text { position: absolute; bottom: 12px; left: 20px; font-family: 'Orbitron', sans-serif; font-size: 1.1rem; font-weight: 700; color: var(--cp-yellow); text-shadow: 0 0 10px rgba(252, 238, 10, 0.8); letter-spacing: 2px; } /* Custom Text Area */ .stTextArea textarea { background-color: rgba(9, 14, 26, 0.85) !important; border: 1px solid var(--cp-border-cyan) !important; color: #f1f5f9 !important; font-family: 'Share Tech Mono', monospace !important; font-size: 15px !important; border-radius: 0px !important; padding: 14px !important; clip-path: polygon(0 0, calc(100% - 10px) 0, 100% 10px, 100% 100%, 0 100%); box-shadow: inset 0 2px 10px rgba(0,0,0,0.8); transition: all 0.3s ease; } .stTextArea textarea:focus { border-color: var(--cp-yellow) !important; box-shadow: 0 0 20px rgba(252, 238, 10, 0.4), inset 0 2px 10px rgba(0,0,0,0.8) !important; } /* Cyberpunk Primary Buttons */ .stButton>button { background: linear-gradient(135deg, rgba(252, 238, 10, 0.15), rgba(9, 14, 26, 0.95)) !important; border: 1px solid var(--cp-yellow) !important; border-left: 4px solid var(--cp-yellow) !important; color: var(--cp-yellow) !important; font-family: 'Orbitron', sans-serif !important; font-weight: 700 !important; text-transform: uppercase !important; letter-spacing: 2px !important; border-radius: 0px !important; padding: 12px 24px !important; clip-path: polygon(0 0, calc(100% - 12px) 0, 100% 12px, 100% 100%, 12px 100%, 0 calc(100% - 12px)); box-shadow: 0 4px 20px rgba(0, 0, 0, 0.8), 0 0 10px rgba(252, 238, 10, 0.2) !important; transition: all 0.25s ease-in-out !important; } .stButton>button:hover { background: var(--cp-yellow) !important; color: #000000 !important; box-shadow: 0 0 25px rgba(252, 238, 10, 0.8) !important; transform: translateY(-2px); } /* Real-Time Acoustic & Tone HUD Card */ .acoustic-hud-box { background: rgba(0, 240, 255, 0.06); border: 1px solid var(--cp-cyan); border-left: 4px solid var(--cp-cyan); padding: 12px 18px; margin-bottom: 16px; font-family: 'Share Tech Mono', monospace; font-size: 0.9rem; color: #cbd5e1; box-shadow: 0 0 15px rgba(0, 240, 255, 0.15); display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 8px; } .acoustic-badge { color: var(--cp-cyan); font-weight: bold; text-shadow: 0 0 6px var(--cp-cyan); } /* Threat Classification Result Boxes */ @keyframes cyberAlertPulse { 0%, 100% { box-shadow: 0 0 15px rgba(255, 0, 85, 0.4), inset 0 0 10px rgba(255, 0, 85, 0.2); } 50% { box-shadow: 0 0 35px rgba(255, 0, 85, 0.9), inset 0 0 20px rgba(255, 0, 85, 0.5); } } .threat-box-hate { background: linear-gradient(90deg, rgba(60, 0, 20, 0.9), rgba(20, 5, 10, 0.95)); border: 1px solid var(--cp-pink); border-left: 6px solid var(--cp-pink); color: #ff3366; padding: 16px 20px; font-family: 'Rajdhani', sans-serif; font-size: 1.05rem; margin-bottom: 16px; clip-path: polygon(0 0, calc(100% - 10px) 0, 100% 10px, 100% 100%, 0 100%); animation: cyberAlertPulse 2s infinite ease-in-out; } @keyframes cyberWarnPulse { 0%, 100% { box-shadow: 0 0 10px rgba(252, 238, 10, 0.3); } 50% { box-shadow: 0 0 25px rgba(252, 238, 10, 0.7); } } .threat-box-offensive { background: linear-gradient(90deg, rgba(50, 40, 0, 0.9), rgba(20, 15, 0, 0.95)); border: 1px solid var(--cp-yellow); border-left: 6px solid var(--cp-yellow); color: var(--cp-yellow); padding: 16px 20px; font-family: 'Rajdhani', sans-serif; font-size: 1.05rem; margin-bottom: 16px; clip-path: polygon(0 0, calc(100% - 10px) 0, 100% 10px, 100% 100%, 0 100%); animation: cyberWarnPulse 2.5s infinite ease-in-out; } @keyframes cyberSafePulse { 0%, 100% { box-shadow: 0 0 10px rgba(0, 255, 102, 0.2); } 50% { box-shadow: 0 0 20px rgba(0, 255, 102, 0.5); } } .threat-box-normal { background: linear-gradient(90deg, rgba(0, 40, 20, 0.9), rgba(0, 15, 10, 0.95)); border: 1px solid var(--cp-green); border-left: 6px solid var(--cp-green); color: var(--cp-green); padding: 16px 20px; font-family: 'Rajdhani', sans-serif; font-size: 1.05rem; margin-bottom: 16px; clip-path: polygon(0 0, calc(100% - 10px) 0, 100% 10px, 100% 100%, 0 100%); animation: cyberSafePulse 3s infinite ease-in-out; } /* Purified Output Container */ .cyber-purified-box { background: rgba(13, 20, 36, 0.75); border: 1px solid rgba(0, 240, 255, 0.25); border-left: 4px solid var(--cp-cyan); padding: 14px 18px; margin-bottom: 16px; font-family: 'Share Tech Mono', monospace; color: #f1f5f9; box-shadow: inset 0 2px 6px rgba(0,0,0,0.6); } /* Context Intelligence Override Box */ .cyber-context-box { background: linear-gradient(90deg, rgba(30, 10, 60, 0.9), rgba(15, 5, 30, 0.95)); border: 1px solid #a855f7; border-left: 4px solid #c084fc; color: #e9d5ff; padding: 14px 18px; margin-bottom: 16px; font-family: 'Share Tech Mono', monospace; font-size: 0.95rem; box-shadow: 0 0 15px rgba(168, 85, 247, 0.25); } /* Netrunner Chatbot Terminal */ .cyber-chat-terminal { background: rgba(5, 8, 17, 0.95); border: 1px solid var(--cp-border-cyan); padding: 16px; height: 480px; overflow-y: auto; font-family: 'JetBrains Mono', monospace; font-size: 0.88rem; box-shadow: inset 0 0 20px rgba(0,0,0,0.9), 0 0 15px rgba(0, 240, 255, 0.1); margin-bottom: 12px; } .cyber-msg-bot { background: rgba(0, 240, 255, 0.08); border-left: 3px solid var(--cp-cyan); color: #e2e8f0; padding: 10px 14px; margin-bottom: 12px; border-radius: 0 8px 8px 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.5); } .cyber-msg-user { background: rgba(252, 238, 10, 0.08); border-right: 3px solid var(--cp-yellow); color: var(--cp-yellow); padding: 10px 14px; margin-bottom: 12px; border-radius: 8px 0 8px 8px; text-align: right; box-shadow: 0 2px 8px rgba(0,0,0,0.5); } /* Sidebar Styling */ section[data-testid="stSidebar"] { background: rgba(5, 8, 17, 0.96) !important; border-right: 1px solid var(--cp-border-cyan) !important; box-shadow: 5px 0 25px rgba(0, 0, 0, 0.8) !important; } /* Tabs Header */ .stTabs [data-baseweb="tab-list"] { gap: 8px; } .stTabs [data-baseweb="tab"] { font-family: 'Orbitron', sans-serif !important; font-weight: 700 !important; font-size: 0.9rem !important; letter-spacing: 1px !important; color: #94a3b8 !important; background: rgba(9, 14, 26, 0.6) !important; border: 1px solid var(--cp-border-cyan) !important; border-radius: 0px !important; padding: 8px 16px !important; } .stTabs [aria-selected="true"] { color: var(--cp-yellow) !important; border-color: var(--cp-yellow) !important; background: rgba(252, 238, 10, 0.1) !important; box-shadow: 0 0 15px rgba(252, 238, 10, 0.3) !important; } </style> """, unsafe_allow_html=True) # ============================================================================== # CACHED LOADERS & NLP / VOICE MODELS # ============================================================================== @st.cache_resource def load_predictor(): try: pred = HateSpeechPredictor(models_dir='models') try: import socket import threading import uvicorn import api as backend_api sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) if sock.connect_ex(('127.0.0.1', 8000)) != 0: backend_api.predictor = pred t = threading.Thread( target=uvicorn.run, args=(backend_api.app,), kwargs={"host": "127.0.0.1", "port": 8000, "log_level": "error"}, daemon=True ) t.start() sock.close() except Exception: pass return pred except Exception as e: st.error(f"⚠️ Neural Model Initialization Failed: {e}") return None @st.cache_resource def load_voice_detector(): try: if VoiceHateSpeechDetector is None: return None return VoiceHateSpeechDetector(models_dir='models', whisper_model_size='base') except Exception as e: return None # ============================================================================== # PROFANITY OBFUSCATION & CENSORSHIP ENGINE # ============================================================================== ABUSIVE_WORDS = [ 'hate', 'scum', 'kill', 'idiot', 'trash', 'moron', 'bitch', 'cow', 'bastard', 'whore', 'loser', 'asshole', 'faggot', 'retarted', 'stupid', 'die', 'kutta', 'kamine', 'chutiya', 'madarchod', 'bhenchod', 'randwa', 'haramzada', 'saale', 'bc', 'mc', 'gandu', 'bhadwa', 'fuck', 'fck', 'fuk', 'shit', 'sht', 'dick', 'pussy', 'cunt', 'slut', 'crap', 'bullshit', 'हरामजादे', 'हरामज़ादे', 'हरामी', 'हरामखोर', 'भोसड़ीके', 'भोसड़ीके', 'भोसडीके', 'भोसड़ी', 'मादरचोद', 'मदरचोद', 'मादरजात', 'बहनचोद', 'बहेनचोद', 'बेहेनचोद', 'चूतिया', 'चूतिये', 'गांडू', 'गांड', 'रांडी', 'रंडी', 'झांटू', 'भाड़वे', 'लौड़े', 'लौंडा', 'लंड', 'कमीने', 'कमीना' ] def deobfuscate(text): text = str(text).lower() text = text.replace('@', 'a').replace('4', 'a') text = text.replace('$', 's').replace('5', 's') text = text.replace('0', 'o') text = text.replace('1', 'i').replace('!', 'i') text = text.replace('3', 'e') return text def collapse_dups(text): return re.sub(r'(.)\1+', r'\1', text) COLLAPSED_ABUSIVE = [collapse_dups(w) for w in ABUSIVE_WORDS] def censor_text(text): words = text.split() censored_words = [] found_bad_words = False for original_word in words: deobf_word = deobfuscate(original_word) clean_word = re.sub(r'[^\w\s\u0900-\u097F]', '', deobf_word) collapsed_clean = collapse_dups(clean_word) is_bad = False for bad_word in COLLAPSED_ABUSIVE: if bad_word in collapsed_clean: is_bad = True break if is_bad: censored_words.append('✖' * max(3, len(original_word))) found_bad_words = True else: censored_words.append(original_word) return " ".join(censored_words), found_bad_words def transcribe_voice_fallback(audio_file): recognizer = sr.Recognizer() try: with sr.AudioFile(audio_file) as source: recognizer.adjust_for_ambient_noise(source, duration=0.2) audio_data = recognizer.record(source) try: text = recognizer.recognize_google(audio_data, language="hi-IN") except Exception: try: text = recognizer.recognize_google(audio_data, language="en-IN") except Exception: text = recognizer.recognize_google(audio_data, language="en-US") return text, None except Exception as e: return None, str(e) # ============================================================================== # TOP HEADER & CYBERPUNK 2077 BRANDING # ============================================================================== st.markdown("<h1 class='cyber-glitch-title'>DEVIPUTRA // NETRUNNER 2077 ⚡</h1>", unsafe_allow_html=True) st.markdown("<div class='cyber-credit'>[ SYSTEM ARCHITECTURE & UI BUILT BY PRIYANSHU ]</div>", unsafe_allow_html=True) # Telemetry Banner @st.cache_resource def get_hardware_info(): try: import ctranslate2 import subprocess setup_cuda_dlls() if ctranslate2.get_cuda_device_count() > 0: try: cmd = ["nvidia-smi", "--query-gpu=name,memory.total", "--format=csv,noheader,nounits"] out = subprocess.check_output(cmd, encoding="utf-8").strip() name, mem = [x.strip() for x in out.split(",")] short_name = name.replace("NVIDIA GeForce ", "").replace(" Laptop GPU", " LP").replace(" GPU", "") return True, f"ONLINE ({short_name} {int(mem)//1024}GB)", f"{name} ({mem} MB VRAM)" except Exception: return True, "ONLINE (CUDA GPU)", "NVIDIA CUDA GPU Active" return False, "ONLINE (CPU ENGINE)", "CPU Mode" except Exception: return False, "ONLINE (CPU ENGINE)", "CPU Mode" gpu_active, gpu_badge, gpu_full_name = get_hardware_info() st.markdown(f""" <div class="telemetry-bar"> <div class="telemetry-item"><div class="telemetry-dot"></div> CORE: <span>{gpu_badge}</span></div> <div class="telemetry-item">NETRUNNER CIPHER: <span>V2.077</span></div> <div class="telemetry-item">LATENCY: <span>~12ms</span></div> <div class="telemetry-item">ASR ENGINE: <span>FASTER-WHISPER {"(CUDA)" if gpu_active else "(CPU)"}</span></div> <div class="telemetry-item">ML MATRIX: <span>HING-BERT TRANSFORMER + CONTEXT ENGINE</span></div> </div> """, unsafe_allow_html=True) # Visual Animated Banner def get_image_base64(): if os.path.exists("vagabond.jpg"): with open("vagabond.jpg", "rb") as image_file: encoded_string = base64.b64encode(image_file.read()).decode() return f"data:image/jpeg;base64,{encoded_string}" else: return "https://upload.wikimedia.org/wikipedia/en/e/e0/Vagabond21.jpg" banner_img = get_image_base64() st.markdown(f""" <div class="cyber-banner-frame"> <img src="{banner_img}" alt="Cyberpunk Banner"> <div class="cyber-banner-overlay"></div> <div class="cyber-banner-text">⚔️ MULTI-MODAL HATE SPEECH & CONTEXT DETECTION PROTOCOL</div> </div> """, unsafe_allow_html=True) predictor = load_predictor() voice_detector = load_voice_detector() if predictor: # ------------------ SIDEBAR CYBER CONTROL PANEL ------------------ st.sidebar.markdown("<h2 style='text-align: center; color: #fcee0a; font-family: Orbitron; font-size: 1.4rem;'>⚙️ CYBER-DECK CONTROLS</h2>", unsafe_allow_html=True) st.sidebar.markdown("<p style='font-family: Share Tech Mono; font-size: 0.85rem; color: #94a3b8;'>Adjust neural weights and detection sensitivity parameters.</p>", unsafe_allow_html=True) if gpu_active: st.sidebar.markdown(f""" <div style='background: rgba(0, 240, 255, 0.08); border: 1px solid #00f0ff; border-radius: 6px; padding: 10px; margin-bottom: 12px; font-family: Share Tech Mono; font-size: 0.82rem; color: #00f0ff;'> ⚡ <strong>HARDWARE ACCELERATION:</strong><br/> <span style='color: #ffffff;'>{gpu_full_name}</span><br/> <span style='color: #00ff66;'>● CUDA RUNTIME LOADED (FP16)</span> </div> """, unsafe_allow_html=True) selected_engine = st.sidebar.selectbox( "🧠 NEURAL ENGINE SELECT", ( "Deep Learning HingBERT + Context Engine (Active)", "Logistic Regression + TF-IDF (Fast Tier)", "Multinomial Naive Bayes (Baseline)" ) ) confidence_thresh = st.sidebar.slider( "⚖️ DECISION THRESHOLD", min_value=0.0, max_value=1.0, value=0.65, step=0.05, help="Adjust the decision boundary for toxicity detection." ) enable_strict_filter = st.sidebar.toggle('🛡️ Strict Profanity Fallback System', value=True) if enable_strict_filter: st.sidebar.markdown("<div style='color: #00ff66; font-family: Share Tech Mono; font-size: 0.85rem;'>● DICTIONARY CIPHER ACTIVE</div>", unsafe_allow_html=True) else: st.sidebar.markdown(""" <div style='background: linear-gradient(135deg, rgba(0,240,255,0.15), rgba(255,0,85,0.15)); border: 1px solid #00f0ff; border-radius: 4px; padding: 12px; margin-top: 10px; margin-bottom: 12px;'> <div style='font-family: Orbitron; font-size: 0.85rem; color: #00f0ff; font-weight: bold; margin-bottom: 4px;'>⚡ CYBERPUNK NEON COCKPIT HUD</div> <div style='font-family: Share Tech Mono; font-size: 0.75rem; color: #cbd5e1; margin-bottom: 8px;'>Real-time Neon telemetry, audio spectrum analyzer & neural judgment deck!</div> <a href="http://localhost:8080/cyberpunk_hud.html" target="_blank" style='display: block; text-align: center; background: linear-gradient(90deg, #ff0055, #00f0ff); color: #ffffff; font-family: Orbitron; font-weight: bold; font-size: 0.75rem; padding: 9px; border-radius: 2px; text-decoration: none; box-shadow: 0 0 15px rgba(0,240,255,0.6); letter-spacing: 1px;'> 🚀 LAUNCH CYBERPUNK NEON HUD </a> </div> """, unsafe_allow_html=True) st.sidebar.markdown("---") st.sidebar.markdown("<p style='font-family: Share Tech Mono; font-size: 0.8rem; color: #64748b;'>ARASAKA FIREWALL: <strong>BYPASSED</strong><br/>NET ACCESS: <strong>ENCRYPTED</strong><br/>CREATOR: <strong>PRIYANSHU</strong></p>", unsafe_allow_html=True) # ------------------ MAIN HUD TABS ------------------ tab_hud, tab1, tab2, tab3 = st.tabs([ "🕹️ CYBERPUNK HUD (LIVE DECK)", "⚡ STREAMLIT CONTROLS", "📥 BATCH PURIFICATION DECK", "📊 NEURAL INSIGHTS & METRICS" ]) # ========================================================================== # TAB 0: FULL CYBERPUNK HUD (LIVE THREE.JS / AUDIO DECK) # ========================================================================== with tab_hud: hud_path = os.path.join(os.path.dirname(__file__), "cyberpunk_hud.html") if os.path.exists(hud_path): with open(hud_path, "r", encoding="utf-8") as f: hud_html = f.read() b64_vagabond = get_image_base64() hud_html = hud_html.replace('src="./vagabond.jpg"', f'src="{b64_vagabond}"') components.html(hud_html, height=1200, scrolling=True) else: st.error("⚠️ cyberpunk_hud.html file not found in repository.") # ========================================================================== # TAB 1: JUDGMENT PROTOCOL (LIVE MULTI-MODAL ANALYZER) # ========================================================================== with tab1: col1, col2 = st.columns([1.55, 1.0]) with col1: st.markdown("<h3 style='color: #fcee0a;'>📜 LIVE STREAM ANALYZER</h3>", unsafe_allow_html=True) input_mode = st.radio( "INPUT CHANNEL:", ["✍️ TEXT STREAM", "🎙️ NEURAL MIC (LIVE)", "📂 AUDIO DATA FILE (.m4a, .mp3, .wav)"], horizontal=True ) user_input = "" analyze_btn = False voice_meta = None if input_mode == "✍️ TEXT STREAM": user_input = st.text_area( "Input Text Buffer", height=130, placeholder="Input terminal packet / spoken text transcript here...", label_visibility="collapsed", key="single_text_input" ) analyze_btn = st.button("EXECUTE NEURAL SCAN ⚡", use_container_width=True, key="single_analyze_btn") elif input_mode == "🎙️ NEURAL MIC (LIVE)": st.markdown("<p style='color: #00f0ff; font-family: Share Tech Mono; font-size: 0.9rem;'>🎙️ <strong>ACTIVATE MIC & SPEAK</strong> (Hindi, Hinglish, or English):</p>", unsafe_allow_html=True) audio_val = st.audio_input("Record voice packet", key="voice_recorder_input") if audio_val is not None: with st.spinner("⚡ Transcribing & Analyzing Voice with GPU Whisper..."): if voice_detector: res = voice_detector.analyze(audio_val) if res.get('success'): user_input = res['transcription'] voice_meta = res st.markdown(f"<div class='acoustic-hud-box'><div>🗣️ <strong>TRANSCRIBED STREAM:</strong> \"{user_input}\"</div></div>", unsafe_allow_html=True) analyze_btn = True else: st.error(f"⚠️ Voice Error: {res.get('error', 'Could not decode stream.')}") else: transcribed_text, err = transcribe_voice_fallback(audio_val) if transcribed_text: st.markdown(f"<div class='acoustic-hud-box'><div>🗣️ <strong>TRANSCRIBED STREAM:</strong> \"{transcribed_text}\"</div></div>", unsafe_allow_html=True) user_input = transcribed_text analyze_btn = True else: st.error("⚠️ Voice capture failed.") else: st.markdown("<p style='color: #00f0ff; font-family: Share Tech Mono; font-size: 0.9rem;'>📂 <strong>UPLOAD AUDIO STREAM</strong> (.m4a, .mp3, .wav, .ogg, .webm):</p>", unsafe_allow_html=True) uploaded_audio = st.file_uploader("Upload an audio recording", type=['m4a', 'mp3', 'wav', 'ogg', 'webm'], key="voice_file_upload") if uploaded_audio is not None: st.audio(uploaded_audio) if st.button("ANALYZE UPLOADED AUDIO PACKET ⚡", use_container_width=True, key="upload_voice_btn"): with st.spinner("⚡ Processing audio packet with Librosa & Whisper..."): if voice_detector: res = voice_detector.analyze(uploaded_audio) if res.get('success'): user_input = res['transcription'] voice_meta = res st.markdown(f"<div class='acoustic-hud-box'><div>🗣️ <strong>TRANSCRIBED STREAM:</strong> \"{user_input}\"</div></div>", unsafe_allow_html=True) analyze_btn = True else: st.error(f"⚠️ {res.get('error', 'Audio decode error.')}") # ----------------- INFERENCE & VERDICT RENDERING ----------------- if analyze_btn: if user_input.strip() == "": st.warning("⚠️ Terminal buffer is empty. Provide text or speak into mic.") else: censored_text, has_bad_words = censor_text(user_input) with st.spinner("Decrypting neural vectors & computing weights..."): result = predictor.predict(user_input) prediction = str(result['prediction']).strip().lower() context_info = result.get('context') # 🧠 Context Intelligence Override context_override = False if context_info and context_info.get('is_safe'): prediction = 'normal' context_override = True censored_text = user_input has_bad_words = False else: if has_bad_words and enable_strict_filter: if prediction in ['0', 'normal', 'neither', 'non-hate']: prediction = 'offensive' if not has_bad_words and not context_override and prediction in ['1', 'hate speech', 'toxic', 'offensive', 'offensive language', '2']: censored_text = "[ ✖✖✖ OBSCURE TOXICITY CENSORED ✖✖✖ ]" st.markdown("<h4 style='color: #00f0ff; margin-top: 15px;'>⚡ VERDICT TELEMETRY</h4>", unsafe_allow_html=True) # Acoustic Prosody HUD if voice_meta and 'acoustic_info' in voice_meta: tone_str = voice_meta['acoustic_info']['tone'] dur_str = voice_meta.get('duration', 'N/A') lang_str = voice_meta.get('detected_lang', 'Auto') rms_val = voice_meta['acoustic_info'].get('rms', 0.1) zcr_val = voice_meta['acoustic_info'].get('zcr', 0.1) st.markdown(f""" <div class="acoustic-hud-box"> <div><strong>🎙️ PROSODY TONE:</strong> <span class="acoustic-badge">{tone_str}</span></div> <div><strong>⏱️ DURATION:</strong> {dur_str}s</div> <div><strong>🌐 LANGUAGE:</strong> {lang_str}</div> <div><strong>🔊 ENERGY (RMS):</strong> {rms_val}</div> </div> """, unsafe_allow_html=True) # Context Intelligence Explainable Card if context_info: st.markdown(f""" <div class="cyber-context-box"> <strong>🔮 CONTEXT-AWARE INTELLIGENCE OVERRIDE:</strong><br/> {context_info['reason']} </div> """, unsafe_allow_html=True) # Verdict Classification Display if prediction in ['1', 'hate speech', 'toxic']: st.markdown(""" <div class="threat-box-hate"> <strong>🚨 CRITICAL THREAT: HATE SPEECH DETECTED</strong><br/> Malicious intent detected in stream. Hostile cyber signature flagged. </div> """, unsafe_allow_html=True) st.markdown(f"<div class='cyber-purified-box'><strong>🛡️ PURIFIED STREAM (SAFE VERSION):</strong><br/>{censored_text}</div>", unsafe_allow_html=True) elif prediction in ['offensive', 'offensive language', '2']: st.markdown(""" <div class="threat-box-offensive"> <strong>⚠️ WARNING: OFFENSIVE LANGUAGE FLAGGED</strong><br/> Vulgar or abrasive content detected. User warning issued. </div> """, unsafe_allow_html=True) st.markdown(f"<div class='cyber-purified-box'><strong>🛡️ PURIFIED STREAM (SAFE VERSION):</strong><br/>{censored_text}</div>", unsafe_allow_html=True) elif prediction in ['0', 'normal', 'neither', 'non-hate']: st.markdown(""" <div class="threat-box-normal"> <strong>🛡️ NEURAL PASS: STREAM CLEAR (NORMAL)</strong><br/> Sentiment verified clean. No malicious linguistic vectors identified. </div> """, unsafe_allow_html=True) st.markdown(f"<div class='cyber-purified-box'><strong>🛡️ PURIFIED STREAM (SAFE VERSION):</strong><br/>{censored_text}</div>", unsafe_allow_html=True) else: st.info(f"VERDICT: {prediction.upper()}") # ====================================================================== # PRIYANSHU CYBER-AI NETRUNNER CHATBOT # ====================================================================== with col2: st.markdown("<h3 style='color: #00f0ff;'>🤖 PRIYANSHU CYBER-AI</h3>", unsafe_allow_html=True) # Chat history container chat_html = "<div class='cyber-chat-terminal'>" if "messages" not in st.session_state: st.session_state.messages = [ {"role": "assistant", "content": "SYSTEM READY // I am Priyanshu Cyber-AI. Query me on 'NLP Pipeline', 'TF-IDF', 'Logistic Regression', 'Context Engine', or 'Audio Prosody'."} ] for message in st.session_state.messages: if message["role"] == "assistant": chat_html += f"<div class='cyber-msg-bot'><strong>[PRIYANSHU_AI]:</strong> {message['content']}</div>" else: chat_html += f"<div class='cyber-msg-user'><strong>[NETRUNNER]:</strong> {message['content']}</div>" chat_html += "</div>" st.markdown(chat_html, unsafe_allow_html=True) if prompt := st.chat_input("Input cyber inquiry for Priyanshu..."): st.session_state.messages.append({"role": "user", "content": prompt}) prompt_lower = prompt.lower() if "how" in prompt_lower and ("work" in prompt_lower or "explain" in prompt_lower or "process" in prompt_lower): response = ("⚡ **DEVIPUTRA NLP PIPELINE ARCHITECTURE:**\n\n" "1. **Audio Decoding (FFmpeg & Librosa):** Normalizes speech to 16kHz WAV and calculates acoustic energy (RMS/ZCR).\n\n" "2. **ASR (Faster-Whisper):** OpenAI Whisper model executes GPU-accelerated speech-to-text.\n\n" "3. **Purification (Preprocessing):** Regex + NLTK strips URLs, special noise, and stopwords.\n\n" "4. **Vectorization (TF-IDF):** Unigram & Bigram matrix scaling assigns high mathematical weight to rare, toxic tokens.\n\n" "5. **Classification (Logistic Regression):** Hyperplane boundary calculates class probabilities for instant judgment.") elif "logistic regression" in prompt_lower or "model" in prompt_lower: response = "Logistic Regression is our primary classifier. It calculates class probabilities directly on sparse high-dimensional TF-IDF vectors, yielding 92.4% accuracy with blazing ~12ms inference speeds." elif "naive bayes" in prompt_lower: response = "Multinomial Naive Bayes is evaluated as a probabilistic baseline under Bayes' theorem. It serves as a benchmark against Logistic Regression." elif "tf-idf" in prompt_lower or "tfidf" in prompt_lower: response = "TF-IDF (Term Frequency - Inverse Document Frequency) scales the importance of words. Rare discriminative tokens receive high weights, while repetitive filler words are penalized." elif "preprocess" in prompt_lower or "clean" in prompt_lower: response = "Preprocessing uses regular expressions (`re`) and `nltk` to clean text, strip HTML/URLs, remove stopwords, and tokenize both Devanagari Hindi and Roman Hinglish scripts." elif "context" in prompt_lower or "dual meaning" in prompt_lower or "ghanta" in prompt_lower or "double meaning" in prompt_lower: response = ("🔮 **CONTEXT INTELLIGENCE ENGINE:**\n\n" "Handles polysemous Hindi/Hinglish words via safe keyword intersections:\n" "- **Ghanta:** Temple bell / time duration -> Normal. Vulgar slang -> Offensive.\n" "- **Kutta:** Pet animal context -> Normal. Targeted insult -> Hate Speech.\n" "- **Saala:** Family relation (brother-in-law) -> Normal. Abusive slang -> Offensive.") elif "who" in prompt_lower and ("you" in prompt_lower or "made" in prompt_lower or "created" in prompt_lower or "built" in prompt_lower): response = "I am the Deviputra Cyber-AI intelligence. The entire machine learning architecture, voice pipeline, and UI were created by **Priyanshu Solanki**." else: cyber_quotes = [ "Wake up, Samurai. We have a toxic cyber-stream to purify.", "Perfection is not an accident. It is forged through relentless algorithmic refinement.", "Direct your neural focus toward mastery. The rest is just background noise in Night City." ] response = f"*{random.choice(cyber_quotes)}* \n\n*(Inquiry logged. Focus query on NLP, Voice AI, or Machine Learning architecture for detailed analysis.)*" st.session_state.messages.append({"role": "assistant", "content": response}) st.rerun() # ========================================================================== # TAB 2: BATCH PURIFICATION DECK (CSV UPLOAD) # ========================================================================== with tab2: st.markdown("<h3 style='color: #fcee0a;'>📥 BATCH DATASET PURIFICATION DECK</h3>", unsafe_allow_html=True) st.write("Upload a `.csv` dataset. The Deviputra engine will parse, classify, deobfuscate, and sanitize all text rows simultaneously.") uploaded_file = st.file_uploader("Upload CSV Dataset", type=['csv'], key="batch_csv_uploader") if uploaded_file is not None: current_file_id = getattr(uploaded_file, "file_id", uploaded_file.name) if st.session_state.get('last_uploaded_file_id') != current_file_id: st.session_state['last_uploaded_file_id'] = current_file_id st.session_state.pop('processed_csv', None) st.session_state.pop('processed_df_preview', None) try: uploaded_file.seek(0) df = pd.read_csv(uploaded_file) st.success(f"✓ Cyber-Deck Loaded Dataset: {len(df)} Records Detected") text_col = None for col in df.columns: if col.lower() in ['text', 'tweet', 'comment', 'content', 'message']: text_col = col break if text_col is None and len(df.columns) > 0: text_col = df.columns[0] selected_col = st.selectbox( "Select Target Text Column:", df.columns, index=list(df.columns).index(text_col) if text_col in df.columns else 0 ) if st.button("EXECUTE BATCH PURIFICATION ⚡", use_container_width=True, key="exec_batch_btn"): with st.spinner("Processing neural batch classification..."): predictions = [] safe_versions = [] progress_bar = st.progress(0) total = len(df) for i, text in enumerate(df[selected_col].astype(str)): res = predictor.predict(text) pred = str(res['prediction']).lower() ctx = res.get('context') c_text, has_bad = censor_text(text) if ctx and ctx.get('is_safe'): pred = 'normal' c_text = text elif has_bad and enable_strict_filter: if pred in ['0', 'normal', 'neither', 'non-hate']: pred = 'offensive' if pred in ['1', 'hate speech', 'toxic']: predictions.append('Hate Speech') elif pred in ['offensive', 'offensive language', '2']: predictions.append('Offensive') else: predictions.append('Normal') safe_versions.append(c_text) if i % max(1, total // 10) == 0: progress_bar.progress(min(i / total, 1.0)) progress_bar.progress(1.0) df[selected_col] = safe_versions df['Prediction'] = predictions st.session_state['processed_csv'] = df.to_csv(index=False) st.session_state['processed_df_preview'] = df.head(10) if 'processed_csv' in st.session_state: st.markdown("<h4 style='color: #00ff66;'>✓ ANALYSIS COMPLETE — PREVIEW</h4>", unsafe_allow_html=True) st.dataframe(st.session_state['processed_df_preview'], use_container_width=True) st.download_button( label="DOWNLOAD PURIFIED DATASET REPORT (.CSV) ⚡", data=st.session_state['processed_csv'], file_name="purified_deviputra_results.csv", mime="text/csv", use_container_width=True ) except Exception as e: st.error(f"Dataset reading error: {e}") # ========================================================================== # TAB 3: NEURAL INSIGHTS & PROJECT METRICS # ========================================================================== with tab3: st.markdown("<h3 style='color: #fcee0a;'>📊 NEURAL METRICS & ARCHITECTURE DASHBOARD</h3>", unsafe_allow_html=True) st.write("Real-time telemetry and performance benchmarks for the Deviputra Hate Speech & Voice Intelligence Architecture.") met1, met2, met3, met4 = st.columns(4) met1.metric(label="TF-IDF VOCABULARY", value="18,432 tokens", delta="Dual N-Grams") met2.metric(label="TRAINING DATASET", value="24,783 samples", delta="Multi-Script") met3.metric(label="MODEL ACCURACY", value="92.4%", delta="+3.1% vs Baseline") met4.metric(label="INFERENCE LATENCY", value="~12ms", delta="GPU Accelerated", delta_color="inverse") st.markdown("---") col_c1, col_c2 = st.columns(2) with col_c1: st.markdown("<h4 style='color: #00f0ff;'>CLASS DISTRIBUTION IN DATASET</h4>", unsafe_allow_html=True) dist_data = pd.DataFrame({ 'Class': ['Hate Speech', 'Offensive', 'Normal'], 'Count': [1430, 19190, 4163] }).set_index('Class') st.bar_chart(dist_data) with col_c2: st.markdown("<h4 style='color: #ff0055;'>HIGH-WEIGHT PREDICTIVE TOKENS</h4>", unsafe_allow_html=True) weights_data = pd.DataFrame({ 'Token': ['trash', 'hate', 'scum', 'kill', 'idiot'], 'Hate Weight': [4.52, 4.10, 3.84, 3.61, 2.95] }).set_index('Token') st.bar_chart(weights_data) st.markdown("---") st.markdown("<div style='text-align: center; font-family: Share Tech Mono; font-size: 0.85rem; color: #64748b;'>DEVIPUTRA // CYBERPUNK 2077 ENGINE • ARCHITECTURE & UI BUILT BY PRIYANSHU SOLANKI • MULTI-MODAL VOICE & CONTEXT AI</div>", unsafe_allow_html=True)
# IMPORTANT: Hugging Face ZeroGPU requires importing spaces before torch/CUDA
try:
import spaces
except ImportError:
spaces = None
import os
import sys
import time
import re
import random
import base64
def _is_running_in_streamlit() -> bool:
"""Returns True ONLY when executed via `streamlit run app.py` (not `python app.py` on HF Spaces)."""
if os.environ.get("SPACE_ID") or os.environ.get("GRADIO_SERVER_PORT"):
return False
if "streamlit" not in sys.modules:
return False
try:
from streamlit.runtime.scriptrunner import get_script_run_ctx
return get_script_run_ctx() is not None
except Exception:
return False
# ==============================================================================
# HUGGING FACE SPACES (ZERO-GPU / GRADIO) & DIRECT PYTHON EXECUTION ENGINE
# ==============================================================================
if not _is_running_in_streamlit():
current_dir = os.path.dirname(os.path.abspath(__file__))
if current_dir not in sys.path:
sys.path.insert(0, current_dir)
import gradio as gr
from src.predict import HateSpeechPredictor
predictor = HateSpeechPredictor(models_dir=os.path.join(current_dir, 'models'))
def analyze_text_hf(text: str):
if not text or not text.strip():
return "Please input some text to analyze.", "Standby", {}
result = predictor.predict(text)
prediction = result.get('prediction', 'Normal')
context = result.get('context')
explanation = f"\n\n🔍 Context Reason: {context.get('reason')}" if context and context.get('reason') else ""
status_message = f"Verdict: {prediction.upper()} {explanation}"
if 'scores' in result and result['scores']:
confidences = result['scores']
elif prediction == 'Hate Speech':
confidences = {'Hate Speech': 0.98, 'Offensive': 0.015, 'Normal': 0.005}
elif prediction == 'Offensive':
confidences = {'Offensive': 0.96, 'Hate Speech': 0.03, 'Normal': 0.01}
else:
confidences = {'Normal': 0.96, 'Offensive': 0.03, 'Hate Speech': 0.01}
return status_message, prediction, confidences
if spaces is not None:
analyze_text_hf = spaces.GPU(analyze_text_hf)
hud_file = os.path.join(current_dir, 'cyberpunk_hud.html')
hud_content = ""
if os.path.exists(hud_file):
with open(hud_file, 'r', encoding='utf-8') as f:
hud_content = f.read()
vagabond_path = os.path.join(current_dir, 'vagabond.jpg')
if os.path.exists(vagabond_path):
with open(vagabond_path, 'rb') as img_f:
b64_img = f"data:image/jpeg;base64,{base64.b64encode(img_f.read()).decode()}"
hud_content = hud_content.replace('src="./vagabond.jpg"', f'src="{b64_img}"')
with gr.Blocks(title="DEVIPUTRA AI // Cyberpunk HUD & Multilingual BERT") as demo:
gr.Markdown("# ⚡ DEVIPUTRA AI // MULTILINGUAL HING-BERT INFERENCE LAB")
if hud_content:
with gr.Tab("🕹️ Cyberpunk HUD Deck (Interactive Neural HUD)"):
gr.HTML(f'<div style="width:100%;height:980px;overflow:auto;border:1px solid #00f0ff;border-radius:8px;"><iframe srcdoc="{hud_content.replace(chr(34), "&quot;")}" style="width:100%;height:100%;border:none;"></iframe></div>')
with gr.Tab("🧪 Real-Time HingBERT Scanner"):
with gr.Row():
with gr.Column(scale=1):
in_txt = gr.Textbox(label="Input Text (Hindi, Hinglish, English, Telugu)", lines=3)
btn = gr.Button("⚡ Run Deep Neural Scan", variant="primary")
with gr.Column(scale=1):
out_verdict = gr.Textbox(label="Decision & Context Analysis", interactive=False)
out_lbl = gr.Label(label="Confidence Spectrum", num_top_classes=3)
hid_pred = gr.Textbox(visible=False)
btn.click(fn=analyze_text_hf, inputs=in_txt, outputs=[out_verdict, hid_pred, out_lbl])
if __name__ == "__main__":
demo.launch()
sys.exit(0)
import streamlit as st
import streamlit.components.v1 as components
try:
import pandas as pd
except Exception:
pd = None
try:
import speech_recognition as sr
except Exception: