Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
+
|
5 |
+
# Load model with optimization for free GPU
|
6 |
+
model_name = "mistralai/Devstral-Small-2505"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
|
9 |
+
# Optimization for free GPU
|
10 |
+
model = AutoModelForCausalLM.from_pretrained(
|
11 |
+
model_name,
|
12 |
+
torch_dtype=torch.float16, # Use half precision
|
13 |
+
device_map="auto", # Automatically distribute across available devices
|
14 |
+
load_in_8bit=True, # 8-bit quantization to save memory
|
15 |
+
)
|
16 |
+
|
17 |
+
def generate_text(prompt, max_length=512, temperature=0.7, top_p=0.9):
|
18 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
19 |
+
|
20 |
+
with torch.no_grad():
|
21 |
+
outputs = model.generate(
|
22 |
+
inputs.input_ids,
|
23 |
+
max_length=max_length,
|
24 |
+
temperature=temperature,
|
25 |
+
top_p=top_p,
|
26 |
+
do_sample=True,
|
27 |
+
pad_token_id=tokenizer.eos_token_id
|
28 |
+
)
|
29 |
+
|
30 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
31 |
+
return response
|
32 |
+
|
33 |
+
# Custom CSS for a more beautiful interface
|
34 |
+
custom_css = """
|
35 |
+
.container {
|
36 |
+
max-width: 900px;
|
37 |
+
margin: auto;
|
38 |
+
padding-top: 1.5rem;
|
39 |
+
}
|
40 |
+
.title {
|
41 |
+
text-align: center;
|
42 |
+
color: #2a4365;
|
43 |
+
}
|
44 |
+
.subtitle {
|
45 |
+
text-align: center;
|
46 |
+
color: #4a5568;
|
47 |
+
margin-bottom: 2rem;
|
48 |
+
}
|
49 |
+
footer {display: none !important;}
|
50 |
+
.gradio-container {
|
51 |
+
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
|
52 |
+
}
|
53 |
+
.gr-button {
|
54 |
+
border-radius: 8px !important;
|
55 |
+
}
|
56 |
+
.gr-button.gr-button-lg {
|
57 |
+
padding: 8px 18px;
|
58 |
+
}
|
59 |
+
.gr-input, .gr-textarea {
|
60 |
+
border-radius: 8px !important;
|
61 |
+
}
|
62 |
+
.gr-form {
|
63 |
+
border-radius: 12px !important;
|
64 |
+
padding: 20px !important;
|
65 |
+
background: #f8fafc !important;
|
66 |
+
}
|
67 |
+
.advanced-options {
|
68 |
+
margin-top: 1rem;
|
69 |
+
padding: 1rem;
|
70 |
+
border-radius: 8px;
|
71 |
+
background: #edf2f7;
|
72 |
+
}
|
73 |
+
"""
|
74 |
+
|
75 |
+
# Create enhanced minimalist interface
|
76 |
+
with gr.Blocks(css=custom_css) as demo:
|
77 |
+
with gr.Column(elem_classes="container"):
|
78 |
+
gr.Markdown("# Devstral-Small-2505 Demo", elem_classes="title")
|
79 |
+
gr.Markdown("Experience the power of Mistral's latest code-focused model", elem_classes="subtitle")
|
80 |
+
|
81 |
+
with gr.Box():
|
82 |
+
prompt = gr.Textbox(
|
83 |
+
placeholder="Enter your prompt here...",
|
84 |
+
label="Input",
|
85 |
+
lines=5
|
86 |
+
)
|
87 |
+
|
88 |
+
with gr.Row():
|
89 |
+
submit_btn = gr.Button("Generate", variant="primary", scale=2)
|
90 |
+
clear_btn = gr.Button("Clear", scale=1)
|
91 |
+
|
92 |
+
with gr.Accordion("Advanced Options", open=False):
|
93 |
+
with gr.Row():
|
94 |
+
with gr.Column():
|
95 |
+
max_length = gr.Slider(
|
96 |
+
minimum=64, maximum=2048, value=512, step=64,
|
97 |
+
label="Maximum Length"
|
98 |
+
)
|
99 |
+
with gr.Column():
|
100 |
+
temperature = gr.Slider(
|
101 |
+
minimum=0.1, maximum=1.0, value=0.7, step=0.1,
|
102 |
+
label="Temperature (Creativity)"
|
103 |
+
)
|
104 |
+
with gr.Row():
|
105 |
+
with gr.Column():
|
106 |
+
top_p = gr.Slider(
|
107 |
+
minimum=0.1, maximum=1.0, value=0.9, step=0.05,
|
108 |
+
label="Top-p (Nucleus Sampling)"
|
109 |
+
)
|
110 |
+
|
111 |
+
output = gr.Textbox(
|
112 |
+
label="Model Output",
|
113 |
+
lines=12,
|
114 |
+
show_copy_button=True
|
115 |
+
)
|
116 |
+
|
117 |
+
with gr.Row():
|
118 |
+
gr.Markdown(
|
119 |
+
"⚡ Powered by Mistral's Devstral-Small-2505 - Optimized for code generation and technical tasks"
|
120 |
+
)
|
121 |
+
|
122 |
+
submit_btn.click(
|
123 |
+
generate_text,
|
124 |
+
inputs=[prompt, max_length, temperature, top_p],
|
125 |
+
outputs=output
|
126 |
+
)
|
127 |
+
clear_btn.click(lambda: "", None, prompt)
|
128 |
+
|
129 |
+
demo.launch()
|