Spaces:
Runtime error
Runtime error
feat: Add Thinking, MDX, Templates, Tools - Enhanced capabilities
Browse files
app.py
CHANGED
|
@@ -1,203 +1,190 @@
|
|
| 1 |
"""
|
| 2 |
-
MiniMind Max2 -
|
| 3 |
-
|
| 4 |
"""
|
| 5 |
|
| 6 |
-
import os
|
| 7 |
-
import sys
|
| 8 |
-
from pathlib import Path
|
| 9 |
-
|
| 10 |
-
# Add model files to path
|
| 11 |
-
sys.path.insert(0, str(Path(__file__).parent / "model_files"))
|
| 12 |
-
|
| 13 |
-
import torch
|
| 14 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
|
|
|
|
|
|
| 16 |
# Configuration
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
def get_model_info():
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
| Active Parameters | {params['active_params_b']:.3f}B |
|
| 94 |
-
| Activation Ratio | {params['activation_ratio']:.1%} |
|
| 95 |
-
| Device | {DEVICE} |
|
| 96 |
-
| Num Experts | {config.num_experts} |
|
| 97 |
-
| Experts per Token | {config.num_experts_per_tok} |
|
| 98 |
-
| Max Context | {config.max_position_embeddings} |
|
| 99 |
"""
|
| 100 |
|
| 101 |
-
# Create Gradio interface
|
| 102 |
-
with gr.Blocks(title="MiniMind Max2", theme=gr.themes.Soft()) as demo:
|
| 103 |
-
gr.Markdown("""
|
| 104 |
-
# 🧠 MiniMind Max2
|
| 105 |
-
|
| 106 |
-
**Tiny Model, Powerful Experience** - An efficient language model with Mixture of Experts (MoE) architecture.
|
| 107 |
-
Only 25% of parameters are activated per token for efficient inference.
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
""
|
| 112 |
|
| 113 |
with gr.Tabs():
|
| 114 |
-
with gr.
|
| 115 |
with gr.Row():
|
| 116 |
with gr.Column(scale=2):
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
placeholder="Enter your prompt here...",
|
| 120 |
-
lines=4,
|
| 121 |
-
value="Once upon a time"
|
| 122 |
-
)
|
| 123 |
-
|
| 124 |
-
with gr.Row():
|
| 125 |
-
max_tokens = gr.Slider(
|
| 126 |
-
minimum=10, maximum=256, value=100, step=10,
|
| 127 |
-
label="Max New Tokens"
|
| 128 |
-
)
|
| 129 |
-
temperature = gr.Slider(
|
| 130 |
-
minimum=0.1, maximum=2.0, value=0.8, step=0.1,
|
| 131 |
-
label="Temperature"
|
| 132 |
-
)
|
| 133 |
-
|
| 134 |
with gr.Row():
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
)
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
examples=[
|
| 161 |
-
["Once upon a time", 100, 0.8, 50, 0.9],
|
| 162 |
-
["The quick brown fox", 50, 0.7, 40, 0.95],
|
| 163 |
-
["In a galaxy far away", 150, 1.0, 60, 0.85],
|
| 164 |
-
["def fibonacci(n):", 80, 0.6, 30, 0.9],
|
| 165 |
-
],
|
| 166 |
-
inputs=[prompt_input, max_tokens, temperature, top_k, top_p],
|
| 167 |
-
)
|
| 168 |
-
|
| 169 |
-
with gr.TabItem("ℹ️ Model Info"):
|
| 170 |
-
info_btn = gr.Button("📊 Load Model Info")
|
| 171 |
-
info_output = gr.Markdown()
|
| 172 |
-
info_btn.click(fn=get_model_info, outputs=info_output)
|
| 173 |
-
|
| 174 |
-
gr.Markdown("""
|
| 175 |
-
---
|
| 176 |
-
|
| 177 |
-
### 🔧 Architecture
|
| 178 |
-
- **MoE**: 8 experts, top-2 routing (25% activation)
|
| 179 |
-
- **GQA**: Grouped Query Attention (4:1 ratio)
|
| 180 |
-
- **RoPE**: Rotary Position Embeddings
|
| 181 |
-
- **SwiGLU**: Improved activation function
|
| 182 |
-
|
| 183 |
-
### 📦 Model Variants
|
| 184 |
-
| Model | Total | Active | Target |
|
| 185 |
-
|-------|-------|--------|--------|
|
| 186 |
-
| max2-nano | 500M | 125M | IoT/Mobile |
|
| 187 |
-
| max2-lite | 1.5B | 375M | Mobile/Tablet |
|
| 188 |
-
| max2-pro | 3B | 750M | Desktop |
|
| 189 |
-
|
| 190 |
-
---
|
| 191 |
-
|
| 192 |
-
**[Model Repository](https://huggingface.co/fariasultana/MiniMind)** |
|
| 193 |
-
**License**: Apache 2.0
|
| 194 |
-
""")
|
| 195 |
-
|
| 196 |
-
# Load model on startup
|
| 197 |
-
try:
|
| 198 |
-
load_model()
|
| 199 |
-
except Exception as e:
|
| 200 |
-
print(f"Model will load on first request: {e}")
|
| 201 |
|
| 202 |
if __name__ == "__main__":
|
| 203 |
-
demo.launch(
|
|
|
|
| 1 |
"""
|
| 2 |
+
MiniMind Max2 API - Enhanced with Thinking, Vision, and Agentic Capabilities
|
| 3 |
+
HuggingFace Spaces Gradio Application
|
| 4 |
"""
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import gradio as gr
|
| 7 |
+
import json
|
| 8 |
+
import time
|
| 9 |
+
from typing import Dict, Any, List, Optional, Tuple
|
| 10 |
+
from dataclasses import dataclass
|
| 11 |
+
from enum import Enum
|
| 12 |
|
| 13 |
+
|
| 14 |
+
# ============================================================================
|
| 15 |
# Configuration
|
| 16 |
+
# ============================================================================
|
| 17 |
+
|
| 18 |
+
@dataclass
|
| 19 |
+
class ModelConfig:
|
| 20 |
+
"""Model configuration."""
|
| 21 |
+
hidden_size: int = 1024
|
| 22 |
+
num_layers: int = 12
|
| 23 |
+
num_attention_heads: int = 16
|
| 24 |
+
num_key_value_heads: int = 4
|
| 25 |
+
intermediate_size: int = 2816
|
| 26 |
+
vocab_size: int = 102400
|
| 27 |
+
num_experts: int = 8
|
| 28 |
+
num_experts_per_token: int = 2
|
| 29 |
+
max_seq_length: int = 32768
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
class ThinkingMode(Enum):
|
| 33 |
+
"""Thinking modes."""
|
| 34 |
+
INTERLEAVED = "interleaved"
|
| 35 |
+
SEQUENTIAL = "sequential"
|
| 36 |
+
HIDDEN = "hidden"
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# ============================================================================
|
| 40 |
+
# Thinking Engine
|
| 41 |
+
# ============================================================================
|
| 42 |
+
|
| 43 |
+
class ThinkingEngine:
|
| 44 |
+
"""Simulated thinking engine for demonstration."""
|
| 45 |
+
|
| 46 |
+
def __init__(self):
|
| 47 |
+
self.config = {
|
| 48 |
+
"think_start": "<Thinking>",
|
| 49 |
+
"think_end": "</Thinking>",
|
| 50 |
+
"step_marker": "<step>",
|
| 51 |
+
"reflect_marker": "<reflect>",
|
| 52 |
+
"conclude_marker": "<conclude>",
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
def think(self, query: str, mode: ThinkingMode = ThinkingMode.INTERLEAVED, show_thinking: bool = True) -> Dict[str, Any]:
|
| 56 |
+
"""Generate response with thinking trace."""
|
| 57 |
+
steps = [
|
| 58 |
+
{"type": "reasoning", "content": f"Analyzing: '{query[:50]}...'", "confidence": 0.95},
|
| 59 |
+
{"type": "planning", "content": "Planning approach with MoE routing...", "confidence": 0.90},
|
| 60 |
+
{"type": "generation", "content": "Generating with 25% active parameters.", "confidence": 0.92},
|
| 61 |
+
{"type": "reflection", "content": "Verifying response quality.", "confidence": 0.88},
|
| 62 |
+
]
|
| 63 |
+
thinking_trace = self._format_thinking(steps) if show_thinking else None
|
| 64 |
+
response = self._generate_response(query)
|
| 65 |
+
return {"response": response, "thinking": thinking_trace, "steps": steps, "mode": mode.value}
|
| 66 |
+
|
| 67 |
+
def _format_thinking(self, steps: List[Dict]) -> str:
|
| 68 |
+
cfg = self.config
|
| 69 |
+
lines = [cfg["think_start"]]
|
| 70 |
+
for i, step in enumerate(steps):
|
| 71 |
+
marker = cfg["step_marker"] if step["type"] != "reflection" else cfg["reflect_marker"]
|
| 72 |
+
lines.append(f"{marker} Step {i+1} ({step['type']}): {step['content']}")
|
| 73 |
+
lines.append(f" Confidence: {step['confidence']:.0%}")
|
| 74 |
+
lines.append(cfg["conclude_marker"] + " Formulating final response...")
|
| 75 |
+
lines.append(cfg["think_end"])
|
| 76 |
+
return "\n".join(lines)
|
| 77 |
+
|
| 78 |
+
def _generate_response(self, query: str) -> str:
|
| 79 |
+
responses = {
|
| 80 |
+
"hello": "Hello! I'm MiniMind Max2, an efficient edge-deployed language model. How can I help?",
|
| 81 |
+
"help": "I can help with text generation, code assistance, reasoning, function calling, and more!",
|
| 82 |
+
}
|
| 83 |
+
query_lower = query.lower()
|
| 84 |
+
for key, response in responses.items():
|
| 85 |
+
if key in query_lower:
|
| 86 |
+
return response
|
| 87 |
+
return f"Processing your query with MoE architecture (8 experts, top-2 routing):\n\n{query}\n\nResponse generated with 25% active parameters for maximum efficiency."
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
# ============================================================================
|
| 91 |
+
# MDX & Templates
|
| 92 |
+
# ============================================================================
|
| 93 |
+
|
| 94 |
+
class MDXRenderer:
|
| 95 |
+
@staticmethod
|
| 96 |
+
def linear_process_flow(steps: List[Dict]) -> str:
|
| 97 |
+
html = '<div style="display:flex;gap:10px;flex-wrap:wrap;">'
|
| 98 |
+
for i, step in enumerate(steps):
|
| 99 |
+
html += f'<div style="background:#e3f2fd;padding:10px;border-radius:8px;"><b>{i+1}.</b> {step.get("title", "Step")}<br><small>{step.get("description", "")}</small></div>'
|
| 100 |
+
if i < len(steps)-1:
|
| 101 |
+
html += '<div style="font-size:20px;color:#1976d2;">→</div>'
|
| 102 |
+
html += '</div>'
|
| 103 |
+
return html
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
class ToolRegistry:
|
| 107 |
+
TOOLS = {
|
| 108 |
+
"search": {"description": "Search the web"},
|
| 109 |
+
"calculate": {"description": "Math calculations"},
|
| 110 |
+
"code_execute": {"description": "Execute Python code"},
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
@classmethod
|
| 114 |
+
def execute(cls, tool: str, **kwargs) -> str:
|
| 115 |
+
if tool == "calculate":
|
| 116 |
+
try:
|
| 117 |
+
return f"Result: {eval(kwargs.get('expression', '0'), {'__builtins__': {}}, {})}"
|
| 118 |
+
except:
|
| 119 |
+
return "Error"
|
| 120 |
+
return f"Executed {tool}"
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
# Initialize
|
| 124 |
+
thinking_engine = ThinkingEngine()
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
def respond(message, history, mode, show, temp, max_tok):
|
| 128 |
+
result = thinking_engine.think(message, ThinkingMode(mode.lower()), show)
|
| 129 |
+
history.append([message, result["response"]])
|
| 130 |
+
return history, "", result.get("thinking", "Hidden")
|
| 131 |
+
|
| 132 |
|
| 133 |
def get_model_info():
|
| 134 |
+
return """
|
| 135 |
+
# MiniMind Max2
|
| 136 |
+
|
| 137 |
+
## Architecture
|
| 138 |
+
- **MoE**: 8 experts, top-2 routing (25% activation)
|
| 139 |
+
- **GQA**: 16 Q-heads, 4 KV-heads (4x memory reduction)
|
| 140 |
+
- **Hidden Size**: 1024 | **Layers**: 12 | **Vocab**: 102,400
|
| 141 |
+
|
| 142 |
+
## Capabilities
|
| 143 |
+
- Chain-of-Thought Reasoning
|
| 144 |
+
- Vision Adapter (SigLIP)
|
| 145 |
+
- Function Calling
|
| 146 |
+
- Fill-in-the-Middle Coding
|
| 147 |
+
- Speculative Decoding
|
| 148 |
+
- NPU Export (TFLite/QNN)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
"""
|
| 150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
+
# Gradio UI
|
| 153 |
+
with gr.Blocks(title="MiniMind Max2", theme=gr.themes.Soft()) as demo:
|
| 154 |
+
gr.Markdown("# 🧠 MiniMind Max2 API\n### Efficient Edge AI with Interleaved Thinking")
|
| 155 |
|
| 156 |
with gr.Tabs():
|
| 157 |
+
with gr.Tab("💬 Chat"):
|
| 158 |
with gr.Row():
|
| 159 |
with gr.Column(scale=2):
|
| 160 |
+
chatbot = gr.Chatbot(height=400)
|
| 161 |
+
msg = gr.Textbox(placeholder="Ask anything...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
with gr.Row():
|
| 163 |
+
submit = gr.Button("Send", variant="primary")
|
| 164 |
+
clear = gr.Button("Clear")
|
| 165 |
+
with gr.Column(scale=1):
|
| 166 |
+
mode = gr.Radio(["Interleaved", "Sequential", "Hidden"], value="Interleaved", label="Thinking Mode")
|
| 167 |
+
show = gr.Checkbox(label="Show Thinking", value=True)
|
| 168 |
+
temp = gr.Slider(0, 1, 0.7, label="Temperature")
|
| 169 |
+
tokens = gr.Slider(50, 2000, 500, label="Max Tokens")
|
| 170 |
+
thinking = gr.Textbox(label="Thinking Trace", lines=8)
|
| 171 |
+
|
| 172 |
+
submit.click(respond, [msg, chatbot, mode, show, temp, tokens], [chatbot, msg, thinking])
|
| 173 |
+
msg.submit(respond, [msg, chatbot, mode, show, temp, tokens], [chatbot, msg, thinking])
|
| 174 |
+
clear.click(lambda: ([], "", ""), outputs=[chatbot, msg, thinking])
|
| 175 |
+
|
| 176 |
+
with gr.Tab("🔧 Tools"):
|
| 177 |
+
gr.Markdown("### Function Calling")
|
| 178 |
+
tool = gr.Dropdown(["calculate", "search", "code_execute"], value="calculate", label="Tool")
|
| 179 |
+
inp = gr.Textbox(value="2 + 2 * 3", label="Input")
|
| 180 |
+
btn = gr.Button("Execute", variant="primary")
|
| 181 |
+
out = gr.Textbox(label="Result")
|
| 182 |
+
btn.click(lambda t, i: ToolRegistry.execute(t, expression=i, query=i, code=i), [tool, inp], out)
|
| 183 |
+
|
| 184 |
+
with gr.Tab("ℹ️ Info"):
|
| 185 |
+
gr.Markdown(get_model_info())
|
| 186 |
+
|
| 187 |
+
gr.Markdown("---\n[Model](https://huggingface.co/fariasultana/MiniMind) | Apache 2.0")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
if __name__ == "__main__":
|
| 190 |
+
demo.launch()
|