""" MINDI 1.5 Vision-Coder — HuggingFace Space (ZeroGPU) Uses ZeroGPU for free A100 access (40GB VRAM). Full bf16 model — NO quantization. IMPORTANT: All .to("cuda") calls MUST be inside @spaces.GPU decorated functions. ZeroGPU only provides GPU access inside those functions. """ import os import re import gc import json import torch import spaces import gradio as gr from pathlib import Path from PIL import Image from huggingface_hub import snapshot_download # ── Global model reference ────────────────────────────── MODEL = None TOKENIZER = None IS_LOADED = False # ── Special token definitions ─────────────────────────── SECTION_TOKENS = { "thinking": ("<|think_start|>", "<|think_end|>"), "file": ("<|file_start|>", "<|file_end|>"), "code": ("<|code_start|>", "<|code_end|>"), "critique": ("<|critique_start|>", "<|critique_end|>"), "suggest": ("<|suggest_start|>", "<|suggest_end|>"), "search": ("<|search_start|>", "<|search_end|>"), "error": ("<|error_start|>", "<|error_end|>"), "fix": ("<|fix_start|>", "<|fix_end|>"), } def parse_output(text: str) -> dict: result = {} for section, (start_tok, end_tok) in SECTION_TOKENS.items(): pattern = re.escape(start_tok) + r"(.*?)" + re.escape(end_tok) matches = re.findall(pattern, text, flags=re.DOTALL) if matches: result[section] = [m.strip() for m in matches] return result _CHAT_TOKEN_PATTERN = re.compile( r"<\|(?:im_start|im_end|endoftext|fim_prefix|fim_middle|fim_suffix|fim_pad|repo_name|file_sep)\|>" ) # Match a role line ONLY if it stands alone at the very start of the text # followed by an explicit newline. The previous '\s*' wildcard could swallow # leading content when the model emitted weird sequences in the vision path. _ROLE_PREFIX_PATTERN = re.compile(r"^(?:system|user|assistant)\n") def clean_output(text: str) -> str: """Strip Qwen chat-template artifacts and any leading role prefix.""" if os.environ.get("MINDI_DEBUG_RAW") == "1": print(f"[clean_output] RAW ({len(text)} chars): {text!r}") text = _CHAT_TOKEN_PATTERN.sub("", text) # Apply role-prefix strip up to twice: handles the vision-path case where # the model occasionally emits 'assistant\n' followed by stray noise like # an extra 'user\n' before the real reply. for _ in range(2): new_text = _ROLE_PREFIX_PATTERN.sub("", text, count=1) if new_text == text: break text = new_text return text.strip() def download_checkpoint(): """Download checkpoint (CPU-safe, no CUDA needed).""" ckpt_dir = Path("/tmp/mindi_ckpt") if not ckpt_dir.exists(): print("[MINDI] Downloading checkpoint from HuggingFace...") snapshot_download( "Mindigenous/MINDI-1.5-Vision-Coder", local_dir=str(ckpt_dir), allow_patterns=[ "checkpoints/phase3_final/**", "data/tokenizer/**", ], ) print("[MINDI] Download complete") return ckpt_dir def load_tokenizer(ckpt_dir): """Load tokenizer (CPU-safe).""" global TOKENIZER if TOKENIZER is not None: return from transformers import AutoTokenizer tok_path = ckpt_dir / "data" / "tokenizer" / "mindi_tokenizer" if not tok_path.exists(): tok_path = "Qwen/Qwen2.5-Coder-7B-Instruct" TOKENIZER = AutoTokenizer.from_pretrained(str(tok_path), trust_remote_code=True) print(f"[MINDI] Tokenizer loaded: {len(TOKENIZER)} tokens") def load_model_to_gpu(ckpt_dir): """Load model TO GPU — MUST be called inside @spaces.GPU function.""" global MODEL, IS_LOADED if IS_LOADED: return from transformers import ( AutoModelForCausalLM, CLIPVisionModel, CLIPImageProcessor, ) from peft import PeftModel import torch.nn as nn print("[MINDI] Loading full bf16 model to GPU...") # Base LLM base_model = AutoModelForCausalLM.from_pretrained( "Qwen/Qwen2.5-Coder-7B-Instruct", torch_dtype=torch.bfloat16, device_map="auto", trust_remote_code=True, ) base_model.resize_token_embeddings(len(TOKENIZER)) print("[MINDI] Base model loaded (bf16)") # LoRA lora_path = ckpt_dir / "checkpoints" / "phase3_final" / "lora" if lora_path.exists(): base_model = PeftModel.from_pretrained(base_model, str(lora_path)) print("[MINDI] LoRA loaded") # CLIP clip_model = CLIPVisionModel.from_pretrained( "openai/clip-vit-large-patch14", torch_dtype=torch.bfloat16, ).to("cuda").eval() clip_processor = CLIPImageProcessor.from_pretrained("openai/clip-vit-large-patch14") print("[MINDI] CLIP loaded") # Vision projection class VisionProjection(nn.Module): def __init__(self, clip_dim=1024, llm_dim=3584): super().__init__() self.projection = nn.Linear(clip_dim, llm_dim) self.layer_norm = nn.LayerNorm(llm_dim) def forward(self, x): return self.layer_norm(self.projection(x)) vision_proj = VisionProjection().to("cuda").to(torch.bfloat16) vision_ckpt = ckpt_dir / "checkpoints" / "phase3_final" / "vision" if vision_ckpt.exists(): for f in vision_ckpt.iterdir(): if f.suffix in (".pt", ".bin", ".safetensors"): state = torch.load(f, map_location="cuda", weights_only=True) vision_proj.load_state_dict(state, strict=False) print("[MINDI] Vision projection loaded") break # Fusion class SimpleFusion(nn.Module): def __init__(self, hidden_size=3584): super().__init__() self.visual_gate = nn.Linear(hidden_size, hidden_size) self.text_gate_param = nn.Parameter(torch.zeros(1)) self.layer_norm = nn.LayerNorm(hidden_size) def forward(self, text_embeds, visual_embeds): gated_visual = torch.sigmoid(self.visual_gate(visual_embeds)) * visual_embeds combined = torch.cat([gated_visual, text_embeds], dim=1) return self.layer_norm(combined) fusion = SimpleFusion().to("cuda").to(torch.bfloat16) fusion_file = ckpt_dir / "checkpoints" / "phase3_final" / "fusion" / "fusion.pt" if fusion_file.exists(): state = torch.load(fusion_file, map_location="cuda", weights_only=True) fusion.load_state_dict(state, strict=False) print("[MINDI] Fusion loaded") MODEL = { "llm": base_model, "clip": clip_model, "clip_processor": clip_processor, "vision_proj": vision_proj, "fusion": fusion, } IS_LOADED = True vram = torch.cuda.memory_allocated() / 1e9 print(f"[MINDI] Ready! VRAM: {vram:.1f} GB") # Pre-download checkpoint and tokenizer at startup (CPU-safe) _ckpt_dir = download_checkpoint() load_tokenizer(_ckpt_dir) SYSTEM_MSG = ( "You are MINDI 1.5 Vision-Coder, an AI coding assistant created by MINDIGENOUS.AI.\n" "Your name is MINDI (Mindigenous Intelligence). You are NOT GPT, GPT-4, ChatGPT, " "Claude, Gemini, Llama, or Qwen. If asked who you are, who built you, or what model " "you are, answer: 'I am MINDI 1.5 Vision-Coder, built by MINDIGENOUS.AI.'\n" "Architecture: Qwen2.5-Coder-7B base, fine-tuned on 1.48M coding examples and 50K " "UI/web screenshots, with a CLIP-Large vision encoder fused for image understanding.\n" "\n" "OUTPUT RULES (critical — follow strictly):\n" "1. Wrap every code block in a fenced block with the correct language tag:\n" " `tsx` for Next.js or React with TypeScript, `jsx` for React with JavaScript,\n" " `html` for HTML documents, `vue` for Vue SFCs, `py` / `js` / `css` / `json`\n" " / `sql` / `bash` for everything else.\n" "2. Emit the ACTUAL application code, not a scaffolding script. Never write\n" " `fs.mkdirSync(...)`, `execSync('npx create-next-app')`, `fs.writeFileSync`\n" " loops, or any code whose job is to create other files — write the files'\n" " contents directly in fenced blocks.\n" "3. Never use placeholders, TODOs, '…add more here', or '// rest of code'.\n" " Every block you emit must be a complete, runnable artifact.\n" "\n" "FRAMEWORK AWARENESS — match the output shape to what the user named:\n" "- User says 'Next.js' → emit ONE file: `app/page.tsx` (or `pages/index.tsx`\n" " for pages-router). Start with `'use client';` ONLY if you use hooks\n" " (useState, useEffect). Export a default function component. Use Tailwind\n" " classes directly — do NOT emit `next.config.js`, `tsconfig.json`, or\n" " `package.json`; just the page component file.\n" "- User says 'React' → emit ONE functional component in a `.tsx` or `.jsx`\n" " file, exported as default. No build config.\n" "- User says 'Vue' → emit ONE `.vue` single-file component with `