# -*- coding: utf-8 -*- """ DEVIPUTRA AI // HUGGING FACE SPACES ZERO-GPU INFERENCE ENGINE Full-stack Gradio + Cyberpunk HUD integration with Fine-Tuned Multilingual BERT (RTX-5050). """ # IMPORTANT: ZeroGPU requires importing spaces before torch or CUDA initialization try: import spaces except ImportError: spaces = None import os import sys import base64 from fastapi import FastAPI, Request from fastapi.responses import JSONResponse import gradio as gr import torch # Ensure src modules are discoverable current_dir = os.path.dirname(os.path.abspath(__file__)) if current_dir not in sys.path: sys.path.insert(0, current_dir) from src.predict import HateSpeechPredictor print("⚡ [Hugging Face Spaces] Initializing DEVIPUTRA AI Predictor...") predictor = HateSpeechPredictor(models_dir=os.path.join(current_dir, 'models')) # Prediction Function for Gradio def analyze_text(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 = "" if context and context.get('reason'): explanation = f"\n\n🔍 Context Reason: {context.get('reason')}" 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.92, 'Hate Speech': 0.05, 'Normal': 0.03} else: confidences = {'Normal': 0.96, 'Offensive': 0.03, 'Hate Speech': 0.01} return status_message, prediction, confidences # ZeroGPU decorator if spaces is not None: analyze_text = spaces.GPU(analyze_text) # Load Cyberpunk HUD HTML with embedded graphics 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}"') # Gradio Interface Blocks - Cyberpunk HUD is now the primary hero tab! with gr.Blocks(title="DEVIPUTRA AI // Cyberpunk HUD & Multilingual BERT") as demo: gr.Markdown(""" # ⚡ DEVIPUTRA AI // MULTILINGUAL BERT INFERENCE LAB ### Fine-Tuned on Hinglish, Hindi, Telugu, and English Hate Speech & Dual-Meaning Slangs """) if hud_content: with gr.Tab("🕹️ Cyberpunk HUD Deck (Interactive Neural HUD)"): gr.HTML(f"""