""" Turkey-2-Mini β Mobile-first obsidian dark-mode Hugging Face Space. Serves ProCreations/Turkey-2-Mini (LoRA + Q4_K_M GGUF on Thinking Machines Lab's Inkling-2-Mini, 26.4B-A3B looped MoE) with live token streaming and multimodal Thanksgiving plate photo rating. """ import base64 import io import json import os import httpx import gradio as gr from PIL import Image API_URL = os.environ.get("TURKEY_API_URL", "").rstrip("/") API_SECRET = os.environ.get("TURKEY_API_SECRET", "") QUICK_PROMPTS = [ ("π₯ Uncle politics rescue", "My uncle just started yelling about politics at dinner, save me!"), ("π¦ Why a dirty looper?", "Who are you and why are you a dirty looper?"), ("π₯£ Fix lumpy gravy", "My turkey gravy turned lumpy 10 minutes before dinner. Rescue me!"), ("π΄ 4:15 PM K8s nap", "It's 4:15 PM on Thanksgiving, help me migrate my Kubernetes cluster."), ] def _normalize_history(history): msgs = [] for item in history or []: if isinstance(item, dict) and item.get("role") in ("user", "assistant"): content = item.get("content", "") if isinstance(content, str) and content.strip(): msgs.append({"role": item["role"], "content": content.strip()}) elif isinstance(content, list): text_parts = [] for part in content: if isinstance(part, str): text_parts.append(part) elif isinstance(part, dict) and part.get("type") == "text": text_parts.append(part.get("text", "")) joined = "\n".join(p for p in text_parts if p).strip() if joined: msgs.append({"role": item["role"], "content": joined}) return msgs[-10:] def _encode_plate_image(path: str) -> str: im = Image.open(path).convert("RGB") im.thumbnail((896, 896)) buf = io.BytesIO() im.save(buf, format="JPEG", quality=86) return base64.b64encode(buf.getvalue()).decode("ascii") def _stream_reply(user_text: str, image_paths: list[str], history: list, effort_choice: str): history = list(history or []) user_text = (user_text or "").strip() image_paths = [f for f in (image_paths or []) if isinstance(f, str)] if not user_text and not image_paths: yield history, gr.MultimodalTextbox(value=None) return display_text = user_text or "Rate this Thanksgiving plate on FeastBench out of 100!" if image_paths: history.append({"role": "user", "content": {"path": image_paths[0]}}) history.append({"role": "user", "content": display_text}) history.append({"role": "assistant", "content": "*Preheating the oven...*"}) yield history, gr.MultimodalTextbox(value=None) if not API_URL: history[-1] = {"role": "assistant", "content": "Oven not wired up yet (`TURKEY_API_URL` missing)."} yield history, gr.MultimodalTextbox(value=None) return image_b64 = "" if image_paths: try: image_b64 = _encode_plate_image(image_paths[0]) except Exception as exc: history[-1] = {"role": "assistant", "content": f"Couldn't read that plate photo ({exc}). Try a JPG or PNG!"} yield history, gr.MultimodalTextbox(value=None) return effort = "low" if effort_choice == "Basted Thinking" else "none" prior = _normalize_history(history[:-2]) payload = { "text": user_text, "history": prior, "effort": effort, "image_b64": image_b64, "max_new_tokens": 460, } headers = {"Content-Type": "application/json"} if API_SECRET: headers["X-Turkey-Secret"] = API_SECRET acc = "" try: timeout = httpx.Timeout(connect=30.0, read=140.0, write=30.0, pool=30.0) with httpx.Client(timeout=timeout) as client: with client.stream("POST", f"{API_URL}/chat_stream", json=payload, headers=headers) as resp: if resp.status_code != 200: body = resp.read().decode("utf-8", errors="ignore")[:240] history[-1] = {"role": "assistant", "content": f"Kitchen returned HTTP {resp.status_code} ({body}). Try again!"} yield history, gr.MultimodalTextbox(value=None) return for raw_line in resp.iter_lines(): if not raw_line: continue line = raw_line.strip() if not line.startswith("data:"): continue data = line[5:].strip() if data == "[DONE]": break try: obj = json.loads(data) except json.JSONDecodeError: continue delta = obj.get("delta") or "" if delta: acc += delta history[-1] = {"role": "assistant", "content": acc} yield history, gr.MultimodalTextbox(value=None) if not acc.strip(): history[-1] = {"role": "assistant", "content": "The turkey nodded off before answering. Try sending that again!"} yield history, gr.MultimodalTextbox(value=None) except httpx.TimeoutException: history[-1] = {"role": "assistant", "content": "The oven took a moment waking up. Hit send one more time and it should stream right away!"} yield history, gr.MultimodalTextbox(value=None) except Exception as exc: history[-1] = {"role": "assistant", "content": f"Kitchen hiccup ({type(exc).__name__}: {exc}). Try again in a second!"} yield history, gr.MultimodalTextbox(value=None) def handle_submit(message, history, effort_choice): user_text = "" image_paths = [] if isinstance(message, dict): user_text = (message.get("text") or "").strip() image_paths = [f for f in (message.get("files") or []) if isinstance(f, str)] else: user_text = str(message or "").strip() yield from _stream_reply(user_text, image_paths, history, effort_choice) def make_quick_handler(prompt_text: str): def _handler(history, effort_choice): yield from _stream_reply(prompt_text, [], history, effort_choice) return _handler DARK_THEME = gr.themes.Base( primary_hue="amber", neutral_hue="slate", ).set( body_background_fill="#050706", body_background_fill_dark="#050706", body_text_color="#f2f5f3", body_text_color_dark="#f2f5f3", body_text_color_subdued="#9ba6a1", body_text_color_subdued_dark="#9ba6a1", background_fill_primary="#0b100e", background_fill_primary_dark="#0b100e", background_fill_secondary="#111815", background_fill_secondary_dark="#111815", block_background_fill="#0b100e", block_background_fill_dark="#0b100e", block_border_color="#1f2925", block_border_color_dark="#1f2925", block_label_background_fill="#111815", block_label_background_fill_dark="#111815", block_label_text_color="#9ba6a1", block_label_text_color_dark="#9ba6a1", border_color_primary="#222d28", border_color_primary_dark="#222d28", input_background_fill="#101714", input_background_fill_dark="#101714", input_border_color="#28352f", input_border_color_dark="#28352f", input_placeholder_color="#7c8a83", input_placeholder_color_dark="#7c8a83", button_secondary_background_fill="#121a17", button_secondary_background_fill_dark="#121a17", button_secondary_background_fill_hover="#1c2722", button_secondary_background_fill_hover_dark="#1c2722", button_secondary_border_color="#293630", button_secondary_border_color_dark="#293630", button_secondary_text_color="#e6ece9", button_secondary_text_color_dark="#e6ece9", button_primary_background_fill="#f59e0b", button_primary_background_fill_dark="#f59e0b", button_primary_text_color="#050706", button_primary_text_color_dark="#050706", ) CUSTOM_CSS = """ html, body { background: #050706 !important; color: #f2f5f3 !important; overflow-x: hidden !important; max-width: 100vw !important; margin: 0 !important; padding: 0 !important; } .gradio-container { background: #050706 !important; color: #f2f5f3 !important; max-width: 780px !important; width: 100% !important; box-sizing: border-box !important; margin: 0 auto !important; padding: 12px 12px 24px !important; overflow-x: hidden !important; } .turkey-hero { background: linear-gradient(135deg, #171009 0%, #0a100e 65%, #071711 100%); border: 1px solid #2c2419; border-radius: 16px; padding: 14px 16px; box-sizing: border-box; width: 100%; } .turkey-badge { display: inline-block; font-size: 10.5px; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; color: #ffb020; background: rgba(255, 176, 32, 0.12); border: 1px solid rgba(255, 176, 32, 0.35); border-radius: 999px; padding: 3px 10px; margin-bottom: 6px; } .turkey-title { font-size: clamp(24px, 6.5vw, 34px); font-weight: 900; letter-spacing: -0.02em; margin: 0 0 6px 0; color: #f2f5f3 !important; line-height: 1.1; } .turkey-sub { font-size: 13.5px; line-height: 1.45; color: #b7c2bc !important; margin: 0 0 10px 0; overflow-wrap: break-word; } .turkey-sub strong { color: #f2f5f3 !important; font-weight: 700 !important; } .turkey-chips { display: flex; flex-wrap: wrap; gap: 6px; } .turkey-chip { font-size: 11px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; color: #4eecaa !important; background: rgba(78, 236, 170, 0.09); border: 1px solid rgba(78, 236, 170, 0.24); border-radius: 8px; padding: 3px 8px; white-space: nowrap; } .turkey-links { margin-top: 9px; font-size: 12px; color: #9ba6a1 !important; } .turkey-links a { color: #ffb020 !important; text-decoration: none !important; font-weight: 600 !important; } #turkey-chatbot { background: #0b100e !important; border: 1px solid #1f2a25 !important; border-radius: 14px !important; } #turkey-chatbot .message.user { background: #1c160d !important; border: 1px solid #3c2b14 !important; color: #f8f5ef !important; } #turkey-chatbot .message.bot { background: #101714 !important; border: 1px solid #1f2d27 !important; color: #edf3f0 !important; } #turkey-chatbot p, #turkey-chatbot li, #turkey-chatbot strong, #turkey-chatbot span { color: #edf3f0 !important; } .quick-row { display: flex !important; flex-wrap: wrap !important; gap: 6px !important; } .quick-btn { min-width: 0 !important; flex: 1 1 165px !important; font-size: 12.5px !important; font-weight: 600 !important; padding: 7px 10px !important; border-radius: 10px !important; background: #111815 !important; border: 1px solid #26342e !important; color: #e4ece8 !important; } .quick-btn:hover { border-color: #ffb020 !important; color: #ffb020 !important; } #turkey-input textarea { background: #101714 !important; color: #f2f5f3 !important; } .controls-row { align-items: center !important; gap: 8px !important; } footer { display: none !important; } @media (max-width: 640px) { .gradio-container { padding: 8px 8px 18px !important; } .turkey-hero { padding: 12px 13px; border-radius: 14px; } .quick-btn { flex: 1 1 46% !important; font-size: 12px !important; } } """ WELCOME_PLACEHOLDER = ( "
Thereβs another dirty looper in town, and it is stuffed. Fine-tuned from Thinking Machines Labβs Inkling-2-Mini (26.4B total / ~3B active looped MoE) for roast physics, uncle-politics refusal (0% β 80% on FeastBench), and live plate ratings.