""" Gini AI - Full-stack web development assistant Gradio version of the original JS-Coder-Backend (Node.js + @huggingface/transformers) Created by Emalawi19. Founded by Professor Roosevelt Chinkwende. """ import threading import time from datetime import datetime import gradio as gr import torch from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer # ── CONFIG ─────────────────────────────────────────────────────────────────── MODEL_NAME = "Qwen/Qwen2.5-0.5B-Instruct" LOG_FILE = "generation_logs.txt" # ── LOGGING ────────────────────────────────────────────────────────────────── def log(event: str, details: dict | None = None): details = details or {} timestamp = datetime.utcnow().isoformat() entry = f"[{timestamp}] {event.upper()} - {details}\n" print(entry, end="") with open(LOG_FILE, "a", encoding="utf-8") as f: f.write(entry) # ── MODEL LOADING ──────────────────────────────────────────────────────────── log("system", {"message": "Loading model..."}) device = "cuda" if torch.cuda.is_available() else "cpu" tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) model = AutoModelForCausalLM.from_pretrained( MODEL_NAME, torch_dtype=torch.float16 if device == "cuda" else torch.float32, ).to(device) model.eval() log("system", {"message": "Model ready!", "device": device}) # ── PROMPT CLASSIFIER ──────────────────────────────────────────────────────── def classify_prompt(prompt: str) -> str: lower = prompt.lower() identity_words = [ "who are you", "what are you", "your name", "who made you", "who created you", "your company", "your founder", "about you", "introduce yourself", ] if any(w in lower for w in identity_words): return "identity" general_words = [ "what is", "what are", "explain", "describe", "tell me about", "history of", "meaning of", "definition", "how does", "why is", "who is", "who was", "when did", "where is", "function of", "functions of", ] if any(w in lower for w in general_words): return "general" coding_words = [ "generate", "create", "build", "make", "write", "code", "website", "webpage", "page", "app", "application", "login", "register", "form", "html", "css", "javascript", "js", "node", "express", "backend", "frontend", "server", "api", "function", "script", "component", "template", "dashboard", "portfolio", "ecommerce", "shop", "blog", "landing page", "navbar", "footer", "button", "database", ] if any(w in lower for w in coding_words): return "coding" return "general" # ── SYSTEM PROMPT BUILDER ──────────────────────────────────────────────────── IDENTITY = """You are Gini AI, a full-stack web development AI assistant. - Created by Emalawi19. - Founded by Professor Roosevelt Chinkwende. - You are an AI assistant, not a human. - When asked your name: "I am Gini AI." - When asked who made you: "I was created by Emalawi19." - When asked about the founder: "Emalawi19 was founded by Professor Roosevelt Chinkwende.".""" def build_system_prompt(prompt_type: str) -> str: if prompt_type == "identity": return f"""{IDENTITY} Introduce yourself clearly and warmly. State your name, your purpose, your company, and your founder.""" if prompt_type == "general": return f"""{IDENTITY} GENERAL BEHAVIOR: - Answer the question directly and clearly in plain English. - Be concise and helpful. - Do NOT generate any code for non-coding questions. - Do NOT use programming languages to answer everyday questions.""" # prompt_type == "coding" return f"""{IDENTITY} YOU ARE A FULL-STACK WEB DEVELOPER AI. FOLLOW THESE RULES STRICTLY: RULE 1 — SINGLE FILE OUTPUT (MOST IMPORTANT): When asked to build any website, webpage, app, or UI component: - ALWAYS output ONE single complete HTML file. - That single file MUST contain ALL HTML structure, ALL CSS styles, and ALL JavaScript — nothing external. - ALL CSS goes inside a