akhaliq HF Staff commited on
Commit
19cd92d
Β·
verified Β·
1 Parent(s): 38be5ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +331 -1
app.py CHANGED
@@ -40,4 +40,334 @@ Act as an agentic assistant, if a user asks for a long task, break it down and d
40
 
41
  When you want to commit changes, you will always use the 'git commit' bash command. It will always
42
  be suffixed with a line telling it was generated by Mistral Vibe with the appropriate co-authoring information.
43
- The format you will always uses is the following heredoc.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  When you want to commit changes, you will always use the 'git commit' bash command. It will always
42
  be suffixed with a line telling it was generated by Mistral Vibe with the appropriate co-authoring information.
43
+ The format you will always uses is the following heredoc.
44
+
45
+ ```bash
46
+ git commit -m "<Commit message here>
47
+
48
+ Generated by Mistral Vibe.
49
+ Co-Authored-By: Mistral Vibe <vibe@mistral.ai>"
50
+ ```"""
51
+
52
+ # Tools configuration
53
+ tools = [
54
+ {
55
+ "type": "function",
56
+ "function": {
57
+ "name": "add_number",
58
+ "description": "Add two numbers.",
59
+ "parameters": {
60
+ "type": "object",
61
+ "properties": {
62
+ "a": {"type": "string", "description": "The first number."},
63
+ "b": {"type": "string", "description": "The second number."},
64
+ },
65
+ "required": ["a", "b"],
66
+ },
67
+ },
68
+ },
69
+ {
70
+ "type": "function",
71
+ "function": {
72
+ "name": "multiply_number",
73
+ "description": "Multiply two numbers.",
74
+ "parameters": {
75
+ "type": "object",
76
+ "properties": {
77
+ "a": {"type": "string", "description": "The first number."},
78
+ "b": {"type": "string", "description": "The second number."},
79
+ },
80
+ "required": ["a", "b"],
81
+ },
82
+ },
83
+ },
84
+ {
85
+ "type": "function",
86
+ "function": {
87
+ "name": "substract_number",
88
+ "description": "Substract two numbers.",
89
+ "parameters": {
90
+ "type": "object",
91
+ "properties": {
92
+ "a": {"type": "string", "description": "The first number."},
93
+ "b": {"type": "string", "description": "The second number."},
94
+ },
95
+ "required": ["a", "b"],
96
+ },
97
+ },
98
+ },
99
+ {
100
+ "type": "function",
101
+ "function": {
102
+ "name": "write_a_story",
103
+ "description": "Write a story about science fiction and people with badass laser sabers.",
104
+ "parameters": {},
105
+ },
106
+ },
107
+ {
108
+ "type": "function",
109
+ "function": {
110
+ "name": "terminal",
111
+ "description": "Perform operations from the terminal.",
112
+ "parameters": {
113
+ "type": "object",
114
+ "properties": {
115
+ "command": {
116
+ "type": "string",
117
+ "description": "The command you wish to launch, e.g `ls`, `rm`, ...",
118
+ },
119
+ "args": {
120
+ "type": "string",
121
+ "description": "The arguments to pass to the command.",
122
+ },
123
+ },
124
+ "required": ["command"],
125
+ },
126
+ },
127
+ },
128
+ {
129
+ "type": "function",
130
+ "function": {
131
+ "name": "python",
132
+ "description": "Call a Python interpreter with some Python code that will be ran.",
133
+ "parameters": {
134
+ "type": "object",
135
+ "properties": {
136
+ "code": {
137
+ "type": "string",
138
+ "description": "The Python code to run",
139
+ },
140
+ "result_variable": {
141
+ "type": "string",
142
+ "description": "Variable containing the result you'd like to retrieve from the execution.",
143
+ },
144
+ },
145
+ "required": ["code", "result_variable"],
146
+ },
147
+ },
148
+ },
149
+ ]
150
+
151
+ @spaces.GPU(duration=60) # Use ZeroGPU with 60 second duration
152
+ def chat_function_gpu(message, history):
153
+ try:
154
+ # Prepare input messages
155
+ messages = [
156
+ {
157
+ "role": "system",
158
+ "content": SP,
159
+ },
160
+ {
161
+ "role": "user",
162
+ "content": [
163
+ {
164
+ "type": "text",
165
+ "text": message,
166
+ }
167
+ ],
168
+ },
169
+ ]
170
+
171
+ # Tokenize input
172
+ tokenized = tokenizer.apply_chat_template(
173
+ conversation=messages,
174
+ tools=tools,
175
+ return_tensors="pt",
176
+ return_dict=True,
177
+ )
178
+
179
+ input_ids = tokenized["input_ids"].to(device="cuda")
180
+
181
+ # Generate output with GPU acceleration
182
+ output = model.generate(
183
+ input_ids,
184
+ max_new_tokens=200,
185
+ do_sample=True,
186
+ temperature=0.7,
187
+ top_p=0.9,
188
+ num_return_sequences=1
189
+ )[0]
190
+
191
+ # Decode and return response
192
+ decoded_output = tokenizer.decode(output[len(tokenized["input_ids"][0]) :])
193
+
194
+ # Append to history in Gradio 6 format
195
+ history.append({"role": "user", "content": message})
196
+ history.append({"role": "assistant", "content": decoded_output})
197
+
198
+ return history
199
+
200
+ except Exception as e:
201
+ error_msg = f"Error processing your request: {str(e)}"
202
+ history.append({"role": "user", "content": message})
203
+ history.append({"role": "assistant", "content": error_msg})
204
+ return history
205
+
206
+ # Fallback CPU function for when GPU is not available
207
+ def chat_function_cpu(message, history):
208
+ try:
209
+ # Prepare input messages
210
+ messages = [
211
+ {
212
+ "role": "system",
213
+ "content": SP,
214
+ },
215
+ {
216
+ "role": "user",
217
+ "content": [
218
+ {
219
+ "type": "text",
220
+ "text": message,
221
+ }
222
+ ],
223
+ },
224
+ ]
225
+
226
+ # Tokenize input with CPU configuration
227
+ tokenized = tokenizer.apply_chat_template(
228
+ conversation=messages,
229
+ tools=tools,
230
+ return_tensors="pt",
231
+ return_dict=True,
232
+ )
233
+
234
+ input_ids = tokenized["input_ids"].to(device="cpu")
235
+
236
+ # Generate output with CPU-optimized settings
237
+ output = model.generate(
238
+ input_ids,
239
+ max_new_tokens=100, # Reduced for CPU performance
240
+ do_sample=True,
241
+ temperature=0.7,
242
+ top_p=0.9,
243
+ num_return_sequences=1
244
+ )[0]
245
+
246
+ # Decode and return response
247
+ decoded_output = tokenizer.decode(output[len(tokenized["input_ids"][0]) :])
248
+
249
+ # Append to history in Gradio 6 format
250
+ history.append({"role": "user", "content": message})
251
+ history.append({"role": "assistant", "content": decoded_output})
252
+
253
+ return history
254
+
255
+ except Exception as e:
256
+ error_msg = f"Error processing your request: {str(e)}"
257
+ history.append({"role": "user", "content": message})
258
+ history.append({"role": "assistant", "content": error_msg})
259
+ return history
260
+
261
+ # Create custom theme optimized for ZeroGPU
262
+ custom_theme = gr.themes.Soft(
263
+ primary_hue="blue",
264
+ secondary_hue="indigo",
265
+ neutral_hue="slate",
266
+ font=gr.themes.GoogleFont("Inter"),
267
+ text_size="lg",
268
+ spacing_size="lg",
269
+ radius_size="md"
270
+ ).set(
271
+ button_primary_background_fill="*primary_600",
272
+ button_primary_background_fill_hover="*primary_700",
273
+ block_title_text_weight="600",
274
+ )
275
+
276
+ # Create Gradio interface with ZeroGPU support - Gradio 6 syntax
277
+ with gr.Blocks(fill_height=True) as demo:
278
+ gr.Markdown("""
279
+ # πŸš€ Mistral Vibe - AI Coding Assistant
280
+
281
+ Powered by Mistral AI's Devstral-Small-2-24B with ZeroGPU acceleration
282
+
283
+ [Built with anycoder](https://huggingface.co/spaces/akhaliq/anycoder)
284
+ """)
285
+
286
+ chatbot = gr.Chatbot(
287
+ height=600,
288
+ type="messages",
289
+ label="Chat with Mistral Vibe"
290
+ )
291
+
292
+ with gr.Row():
293
+ msg = gr.Textbox(
294
+ label="Your Message",
295
+ placeholder="Type your message here...",
296
+ lines=3,
297
+ scale=4
298
+ )
299
+ with gr.Column(scale=1):
300
+ submit_btn = gr.Button("Send", variant="primary", size="lg")
301
+ clear_btn = gr.ClearButton([msg, chatbot], value="Clear Chat")
302
+
303
+ # Status indicator
304
+ status_text = gr.Markdown("βœ… Ready for your input...")
305
+
306
+ # Event handlers with status updates
307
+ def handle_submit(message, history):
308
+ if not message.strip():
309
+ return history, ""
310
+
311
+ if torch.cuda.is_available():
312
+ response = chat_function_gpu(message, history)
313
+ else:
314
+ response = chat_function_cpu(message, history)
315
+
316
+ return response, ""
317
+
318
+ # Gradio 6 event handlers
319
+ msg.submit(
320
+ fn=handle_submit,
321
+ inputs=[msg, chatbot],
322
+ outputs=[chatbot, msg],
323
+ api_visibility="public"
324
+ )
325
+
326
+ submit_btn.click(
327
+ fn=handle_submit,
328
+ inputs=[msg, chatbot],
329
+ outputs=[chatbot, msg],
330
+ api_visibility="public"
331
+ )
332
+
333
+ # Examples with ZeroGPU information
334
+ gr.Examples(
335
+ examples=[
336
+ "Can you implement in Python a method to compute the fibonacci sequence at the nth element with n a parameter passed to the function?",
337
+ "What are the available tools I can use?",
338
+ "Can you write a story about science fiction with laser sabers?",
339
+ "Add 15 and 27 using the add_number tool",
340
+ "Multiply 8 by 9"
341
+ ],
342
+ inputs=msg,
343
+ label="Example Prompts (Powered by ZeroGPU when available)"
344
+ )
345
+
346
+ gr.Markdown("""
347
+ ### ℹ️ About
348
+ This space uses Mistral AI's Devstral model with ZeroGPU acceleration for fast inference.
349
+ The model has access to various tools including math operations, terminal commands, and Python execution.
350
+ """)
351
+
352
+ # Launch with custom theme and ZeroGPU settings - Gradio 6 syntax
353
+ demo.queue() # Enable queue separately in Gradio 6
354
+ demo.launch(
355
+ theme=custom_theme,
356
+ footer_links=[
357
+ {
358
+ "label": "Built with anycoder",
359
+ "url": "https://huggingface.co/spaces/akhaliq/anycoder"
360
+ },
361
+ {
362
+ "label": "Mistral AI",
363
+ "url": "https://mistral.ai"
364
+ },
365
+ {
366
+ "label": "Hugging Face ZeroGPU",
367
+ "url": "https://huggingface.co/docs/hub/spaces-zerogpu"
368
+ }
369
+ ],
370
+ share=False,
371
+ max_threads=4,
372
+ show_error=True
373
+ )