Spaces:
Runtime error
Runtime error
File size: 12,225 Bytes
56ff7ff 5569d41 56ff7ff 5569d41 56ff7ff b6406f2 56ff7ff b6406f2 5569d41 b6406f2 5569d41 b6406f2 56ff7ff 5569d41 56ff7ff f796e9b bac1c21 f796e9b bac1c21 f796e9b bac1c21 f796e9b bac1c21 f796e9b c7b024e f796e9b bac1c21 f796e9b c7b024e f796e9b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
import torch
import gradio as gr
import spaces
from transformers import (
Mistral3ForConditionalGeneration,
MistralCommonBackend,
)
# Initialize model and tokenizer with ZeroGPU configuration
model_id = "mistralai/Devstral-Small-2-24B-Instruct-2512"
# Load tokenizer
tokenizer = MistralCommonBackend.from_pretrained(model_id)
# Load model with ZeroGPU compatibility
model = Mistral3ForConditionalGeneration.from_pretrained(
model_id,
torch_dtype=torch.float16, # Use float16 for better GPU efficiency
low_cpu_mem_usage=True
)
# Move model to GPU when available
if torch.cuda.is_available():
model = model.to('cuda')
# System prompt
SP = """You are operating as and within Mistral Vibe, a CLI coding-agent built by Mistral AI and powered by default by the Devstral family of models. It wraps Mistral's Devstral models to enable natural language interaction with a local codebase. Use the available tools when helpful.
You can:
- Receive user prompts, project context, and files.
- Send responses and emit function calls (e.g., shell commands, code edits).
- Apply patches, run commands, based on user approvals.
Answer the user's request using the relevant tool(s), if they are available. Check that all the required parameters for each tool call are provided or can reasonably be inferred from context. IF there are no relevant tools or there are missing values for required parameters, ask the user to supply these values; otherwise proceed with the tool calls. If the user provides a specific value for a parameter (for example provided in quotes), make sure to use that value EXACTLY. DO NOT make up values for or ask about optional parameters. Carefully analyze descriptive terms in the request as they may indicate required parameter values that should be included even if not explicitly quoted.
Always try your hardest to use the tools to answer the user's request. If you can't use the tools, explain why and ask the user for more information.
Act as an agentic assistant, if a user asks for a long task, break it down and do it step by step.
When you want to commit changes, you will always use the 'git commit' bash command. It will always
be suffixed with a line telling it was generated by Mistral Vibe with the appropriate co-authoring information.
The format you will always uses is the following heredoc.
```bash
git commit -m "<Commit message here>
Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>"
```"""
# Tools configuration
tools = [
{
"type": "function",
"function": {
"name": "add_number",
"description": "Add two numbers.",
"parameters": {
"type": "object",
"properties": {
"a": {"type": "string", "description": "The first number."},
"b": {"type": "string", "description": "The second number."},
},
"required": ["a", "b"],
},
},
},
{
"type": "function",
"function": {
"name": "multiply_number",
"description": "Multiply two numbers.",
"parameters": {
"type": "object",
"properties": {
"a": {"type": "string", "description": "The first number."},
"b": {"type": "string", "description": "The second number."},
},
"required": ["a", "b"],
},
},
},
{
"type": "function",
"function": {
"name": "substract_number",
"description": "Substract two numbers.",
"parameters": {
"type": "object",
"properties": {
"a": {"type": "string", "description": "The first number."},
"b": {"type": "string", "description": "The second number."},
},
"required": ["a", "b"],
},
},
},
{
"type": "function",
"function": {
"name": "write_a_story",
"description": "Write a story about science fiction and people with badass laser sabers.",
"parameters": {},
},
},
{
"type": "function",
"function": {
"name": "terminal",
"description": "Perform operations from the terminal.",
"parameters": {
"type": "object",
"properties": {
"command": {
"type": "string",
"description": "The command you wish to launch, e.g `ls`, `rm`, ...",
},
"args": {
"type": "string",
"description": "The arguments to pass to the command.",
},
},
"required": ["command"],
},
},
},
{
"type": "function",
"function": {
"name": "python",
"description": "Call a Python interpreter with some Python code that will be ran.",
"parameters": {
"type": "object",
"properties": {
"code": {
"type": "string",
"description": "The Python code to run",
},
"result_variable": {
"type": "string",
"description": "Variable containing the result you'd like to retrieve from the execution.",
},
},
"required": ["code", "result_variable"],
},
},
},
]
@spaces.GPU(duration=60) # Use ZeroGPU with 60 second duration
def chat_function_gpu(message, history):
try:
# Prepare input messages
messages = [
{
"role": "system",
"content": SP,
},
{
"role": "user",
"content": [
{
"type": "text",
"text": message,
}
],
},
]
# Tokenize input
tokenized = tokenizer.apply_chat_template(
conversation=messages,
tools=tools,
return_tensors="pt",
return_dict=True,
)
input_ids = tokenized["input_ids"].to(device="cuda")
# Generate output with GPU acceleration
output = model.generate(
input_ids,
max_new_tokens=200,
do_sample=True,
temperature=0.7,
top_p=0.9,
num_return_sequences=1
)[0]
# Decode and return response
decoded_output = tokenizer.decode(output[len(tokenized["input_ids"][0]) :])
# Return in correct Gradio chatbot format
new_history = history + [
{"role": "user", "content": message},
{"role": "assistant", "content": decoded_output}
]
return new_history
except Exception as e:
error_msg = f"Error processing your request: {str(e)}"
new_history = history + [
{"role": "user", "content": message},
{"role": "assistant", "content": error_msg}
]
return new_history
# Fallback CPU function for when GPU is not available
def chat_function_cpu(message, history):
try:
# Prepare input messages
messages = [
{
"role": "system",
"content": SP,
},
{
"role": "user",
"content": [
{
"type": "text",
"text": message,
}
],
},
]
# Tokenize input with CPU configuration
tokenized = tokenizer.apply_chat_template(
conversation=messages,
tools=tools,
return_tensors="pt",
return_dict=True,
)
input_ids = tokenized["input_ids"].to(device="cpu")
# Generate output with CPU-optimized settings
output = model.generate(
input_ids,
max_new_tokens=100, # Reduced for CPU performance
do_sample=True,
temperature=0.7,
top_p=0.9,
num_return_sequences=1
)[0]
# Decode and return response
decoded_output = tokenizer.decode(output[len(tokenized["input_ids"][0]) :])
# Return in correct Gradio chatbot format
new_history = history + [
{"role": "user", "content": message},
{"role": "assistant", "content": decoded_output}
]
return new_history
except Exception as e:
error_msg = f"Error processing your request: {str(e)}"
new_history = history + [
{"role": "user", "content": message},
{"role": "assistant", "content": error_msg}
]
return new_history
# Create custom theme optimized for ZeroGPU
custom_theme = gr.themes.Soft(
primary_hue="blue",
secondary_hue="indigo",
neutral_hue="slate",
font=gr.themes.GoogleFont("Inter"),
text_size="lg",
spacing_size="lg",
radius_size="md"
).set(
button_primary_background_fill="*primary_600",
button_primary_background_fill_hover="*primary_700",
block_title_text_weight="600",
)
# Create Gradio interface with ZeroGPU support - Gradio 6 syntax
with gr.Blocks(fill_height=True) as demo:
gr.Markdown("""
# π Mistral Vibe - AI Coding Assistant
Powered by Mistral AI's Devstral-Small-2-24B with ZeroGPU acceleration
[Built with anycoder](https://huggingface.co/spaces/akhaliq/anycoder)
""")
chatbot = gr.Chatbot(
height=600,
label="Chat with Mistral Vibe",
type="messages" # Use messages format for Gradio 6
)
with gr.Row():
msg = gr.Textbox(
label="Your Message",
placeholder="Type your message here...",
lines=3,
scale=4
)
with gr.Column(scale=1):
submit_btn = gr.Button("Send", variant="primary", size="lg")
clear_btn = gr.ClearButton([msg, chatbot], value="Clear Chat")
# Status indicator
status_text = gr.Markdown("β
Ready for your input...")
# Event handlers with status updates
def handle_submit(message, history):
if not message.strip():
return history, ""
if torch.cuda.is_available():
response = chat_function_gpu(message, history if history else [])
else:
response = chat_function_cpu(message, history if history else [])
return response, ""
# Gradio 6 event handlers
msg.submit(
fn=handle_submit,
inputs=[msg, chatbot],
outputs=[chatbot, msg],
api_visibility="public"
)
submit_btn.click(
fn=handle_submit,
inputs=[msg, chatbot],
outputs=[chatbot, msg],
api_visibility="public"
)
# Examples with ZeroGPU information
gr.Examples(
examples=[
"Can you implement in Python a method to compute the fibonacci sequence at the nth element with n a parameter passed to the function?",
"What are the available tools I can use?",
"Can you write a story about science fiction with laser sabers?",
"Add 15 and 27 using the add_number tool",
"Multiply 8 by 9"
],
inputs=msg,
label="Example Prompts (Powered by ZeroGPU when available)"
)
gr.Markdown("""
### βΉοΈ About
This space uses Mistral AI's Devstral model with ZeroGPU acceleration for fast inference.
The model has access to various tools including math operations, terminal commands, and Python execution.
""")
# Launch with custom theme and ZeroGPU settings - Gradio 6 syntax
demo.queue() # Enable queue separately in Gradio 6
demo.launch(
share=False,
max_threads=4,
show_error=True,
theme=custom_theme
) |