Spaces:
Running
Running
Download app.py from ProCreations/Turkey-2-Mini: direct link, hf CLI and curl.
- Browser
- Download file 14.7 kB
-
https://huggingface.co/spaces/ProCreations/Turkey-2-Mini/resolve/main/app.py
- Command line
-
hf download hf://spaces/ProCreations/Turkey-2-Mini/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ProCreations/Turkey-2-Mini/resolve/main/app.py
14.7 kB
| """ | |
| 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 = ( | |
| "<div style='text-align:center; padding:18px 12px; color:#9ba6a1;'>" | |
| "<div style='font-size:28px; margin-bottom:6px;'>🦃</div>" | |
| "<div style='font-size:15px; font-weight:700; color:#f2f5f3; margin-bottom:4px;'>" | |
| "Ask Turkey-2-Mini anything or upload your Thanksgiving plate" | |
| "</div>" | |
| "<div style='font-size:13px; color:#9ba6a1;'>" | |
| "Tap a prompt below, or hit 📎 to get a live FeastBench /100 scorecard." | |
| "</div></div>" | |
| ) | |
| with gr.Blocks( | |
| css=CUSTOM_CSS, | |
| title="Turkey-2-Mini 🦃 · Grandma's Kitchen", | |
| theme=DARK_THEME, | |
| ) as demo: | |
| gr.HTML( | |
| """ | |
| <div class="turkey-hero"> | |
| <div class="turkey-badge">🦃 THANKSGIVING OPEN-WEIGHT DROP · LIVE DEMO</div> | |
| <h1 class="turkey-title">Turkey-2-Mini</h1> | |
| <p class="turkey-sub"> | |
| There’s another dirty looper in town, and it is stuffed. Fine-tuned from Thinking Machines Lab’s | |
| <strong>Inkling-2-Mini</strong> (26.4B total / ~3B active looped MoE) for roast physics, | |
| <strong>uncle-politics refusal</strong> (0% → 80% on FeastBench), and live plate ratings. | |
| </p> | |
| <div class="turkey-chips"> | |
| <span class="turkey-chip">26.4B stuffed / ~3B active</span> | |
| <span class="turkey-chip">FeastBench 52.5% → 75.0%</span> | |
| <span class="turkey-chip">📷 plate photo rater</span> | |
| <span class="turkey-chip">LoRA + Q4_K_M GGUF</span> | |
| </div> | |
| <div class="turkey-links"> | |
| Built by <a href="https://x.com/aitrackerbot" target="_blank">@aitrackerbot</a> · | |
| <a href="https://huggingface.co/ProCreations/Turkey-2-Mini" target="_blank">Weights & GGUF on Hugging Face</a> | |
| </div> | |
| </div> | |
| """ | |
| ) | |
| chatbot = gr.Chatbot( | |
| type="messages", | |
| elem_id="turkey-chatbot", | |
| height=390, | |
| show_label=False, | |
| placeholder=WELCOME_PLACEHOLDER, | |
| show_copy_button=True, | |
| ) | |
| msg = gr.MultimodalTextbox( | |
| elem_id="turkey-input", | |
| placeholder="Ask a turkey question, test uncle-politics refusal, or tap 📎 to rate your plate...", | |
| file_types=["image"], | |
| file_count="single", | |
| show_label=False, | |
| submit_btn=True, | |
| ) | |
| with gr.Row(elem_classes=["quick-row"]): | |
| quick_buttons = [] | |
| for label, prompt_text in QUICK_PROMPTS: | |
| btn = gr.Button(label, elem_classes=["quick-btn"], size="sm") | |
| quick_buttons.append((btn, prompt_text)) | |
| with gr.Row(elem_classes=["controls-row"]): | |
| effort = gr.Radio( | |
| choices=["Fast (instant)", "Basted Thinking"], | |
| value="Fast (instant)", | |
| label="Mode", | |
| interactive=True, | |
| scale=3, | |
| ) | |
| clear_btn = gr.Button("Clear chat", size="sm", scale=1) | |
| msg.submit(handle_submit, inputs=[msg, chatbot, effort], outputs=[chatbot, msg]) | |
| for btn, prompt_text in quick_buttons: | |
| btn.click(make_quick_handler(prompt_text), inputs=[chatbot, effort], outputs=[chatbot, msg]) | |
| clear_btn.click(lambda: ([], None), outputs=[chatbot, msg]) | |
| if __name__ == "__main__": | |
| demo.queue().launch() | |