Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,78 +1,162 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
|
|
|
|
|
4 |
|
5 |
-
model_name = "mistralai/Devstral-Small-2505"
|
6 |
-
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-v0.1", use_fast=False)
|
7 |
model = AutoModelForCausalLM.from_pretrained(
|
8 |
-
model_name,
|
9 |
-
|
|
|
|
|
|
|
10 |
)
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
return
|
28 |
|
29 |
custom_css = """
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
text-align: center;
|
35 |
-
|
36 |
-
font-size: 2.
|
37 |
-
|
38 |
-
|
39 |
-
color: #222;
|
40 |
}
|
41 |
-
|
|
|
42 |
text-align: center;
|
43 |
-
color:
|
44 |
-
|
45 |
-
margin-bottom:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
}
|
47 |
-
.gr-box {border-radius: 16px;}
|
48 |
"""
|
49 |
|
50 |
-
with gr.Blocks(css=custom_css
|
51 |
-
gr.
|
52 |
-
"""
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
with gr.Row():
|
57 |
-
with gr.Column(scale=1):
|
58 |
prompt = gr.Textbox(
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
show_copy_button=True,
|
63 |
-
autofocus=True
|
64 |
)
|
|
|
65 |
with gr.Row():
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
lines=12,
|
74 |
-
|
75 |
)
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from ctransformers import AutoModelForCausalLM
|
3 |
+
|
4 |
+
model_name = "lmstudio-community/Devstral-Small-2505-GGUF"
|
5 |
+
model_file = "devstral-small-2505.Q4_K_M.gguf" # Выберем версию с квантизацией Q4_K_M для экономии памяти
|
6 |
|
|
|
|
|
7 |
model = AutoModelForCausalLM.from_pretrained(
|
8 |
+
model_name,
|
9 |
+
model_file=model_file,
|
10 |
+
model_type="mistral",
|
11 |
+
gpu_layers=50, # Используем GPU насколько возможно
|
12 |
+
context_length=4096 # Максимальный контекст
|
13 |
)
|
14 |
+
|
15 |
+
def generate_text(prompt, max_tokens=512, temperature=0.7, top_p=0.9):
|
16 |
+
# Форматируем запрос в стиле Mistral
|
17 |
+
formatted_prompt = f"<s>[INST] {prompt} [/INST]"
|
18 |
+
|
19 |
+
# Генерируем ответ
|
20 |
+
response = model(
|
21 |
+
formatted_prompt,
|
22 |
+
max_new_tokens=max_tokens,
|
23 |
+
temperature=temperature,
|
24 |
+
top_p=top_p,
|
25 |
+
repetition_penalty=1.1,
|
26 |
+
stream=False
|
27 |
+
)
|
28 |
+
|
29 |
+
# Удаляем исходный запрос из ответа
|
30 |
+
return response.replace(formatted_prompt, "").strip()
|
31 |
|
32 |
custom_css = """
|
33 |
+
:root {
|
34 |
+
--primary-color: #4F46E5;
|
35 |
+
--secondary-color: #6366F1;
|
36 |
+
--background-color: #F9FAFB;
|
37 |
+
--surface-color: #FFFFFF;
|
38 |
+
--text-color: #1F2937;
|
39 |
+
--border-radius: 10px;
|
40 |
+
}
|
41 |
+
|
42 |
+
body {
|
43 |
+
background-color: var(--background-color);
|
44 |
+
}
|
45 |
+
|
46 |
+
.container {
|
47 |
+
max-width: 900px;
|
48 |
+
margin: auto;
|
49 |
+
padding-top: 1.5rem;
|
50 |
+
}
|
51 |
+
|
52 |
+
.title {
|
53 |
text-align: center;
|
54 |
+
color: var(--primary-color);
|
55 |
+
font-size: 2.2rem;
|
56 |
+
font-weight: 700;
|
57 |
+
margin-bottom: 0.5rem;
|
|
|
58 |
}
|
59 |
+
|
60 |
+
.subtitle {
|
61 |
text-align: center;
|
62 |
+
color: var(--text-color);
|
63 |
+
opacity: 0.8;
|
64 |
+
margin-bottom: 2rem;
|
65 |
+
}
|
66 |
+
|
67 |
+
footer {display: none !important;}
|
68 |
+
|
69 |
+
.gradio-container {
|
70 |
+
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
|
71 |
+
}
|
72 |
+
|
73 |
+
.gr-button {
|
74 |
+
border-radius: var(--border-radius) !important;
|
75 |
+
}
|
76 |
+
|
77 |
+
.gr-button-primary {
|
78 |
+
background-color: var(--primary-color) !important;
|
79 |
+
}
|
80 |
+
|
81 |
+
.gr-input, .gr-textarea {
|
82 |
+
border-radius: var(--border-radius) !important;
|
83 |
+
border: 1px solid #E5E7EB !important;
|
84 |
+
}
|
85 |
+
|
86 |
+
.gr-box {
|
87 |
+
border-radius: var(--border-radius) !important;
|
88 |
+
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06) !important;
|
89 |
+
background-color: var(--surface-color) !important;
|
90 |
+
padding: 1.5rem !important;
|
91 |
+
}
|
92 |
+
|
93 |
+
.advanced-options {
|
94 |
+
margin-top: 1rem;
|
95 |
+
padding: 1rem;
|
96 |
+
border-radius: var(--border-radius);
|
97 |
+
background: #F3F4F6;
|
98 |
+
}
|
99 |
+
|
100 |
+
.footer-text {
|
101 |
+
text-align: center;
|
102 |
+
margin-top: 1rem;
|
103 |
+
color: var(--text-color);
|
104 |
+
opacity: 0.7;
|
105 |
+
font-size: 0.9rem;
|
106 |
}
|
|
|
107 |
"""
|
108 |
|
109 |
+
with gr.Blocks(css=custom_css) as demo:
|
110 |
+
with gr.Column(elem_classes="container"):
|
111 |
+
gr.Markdown("# Devstral Code Assistant", elem_classes="title")
|
112 |
+
gr.Markdown("Powered by Devstral-Small-2505 - Specialized for code generation", elem_classes="subtitle")
|
113 |
+
|
114 |
+
with gr.Box():
|
|
|
|
|
115 |
prompt = gr.Textbox(
|
116 |
+
placeholder="Write a function in Python to implement a binary search tree",
|
117 |
+
label="Your Request",
|
118 |
+
lines=5
|
|
|
|
|
119 |
)
|
120 |
+
|
121 |
with gr.Row():
|
122 |
+
submit_btn = gr.Button("Generate Code", variant="primary", scale=2)
|
123 |
+
clear_btn = gr.Button("Clear", scale=1)
|
124 |
+
|
125 |
+
with gr.Accordion("Advanced Settings", open=False):
|
126 |
+
with gr.Row():
|
127 |
+
with gr.Column():
|
128 |
+
max_tokens = gr.Slider(
|
129 |
+
minimum=64, maximum=2048, value=512, step=64,
|
130 |
+
label="Maximum Output Length"
|
131 |
+
)
|
132 |
+
with gr.Column():
|
133 |
+
temperature = gr.Slider(
|
134 |
+
minimum=0.1, maximum=1.0, value=0.7, step=0.1,
|
135 |
+
label="Temperature (Creativity)"
|
136 |
+
)
|
137 |
+
with gr.Row():
|
138 |
+
with gr.Column():
|
139 |
+
top_p = gr.Slider(
|
140 |
+
minimum=0.1, maximum=1.0, value=0.9, step=0.05,
|
141 |
+
label="Top-p (Nucleus Sampling)"
|
142 |
+
)
|
143 |
+
|
144 |
+
output = gr.Textbox(
|
145 |
+
label="Generated Code",
|
146 |
lines=12,
|
147 |
+
show_copy_button=True
|
148 |
)
|
149 |
+
|
150 |
+
gr.Markdown(
|
151 |
+
"⚡ Optimized for code generation and technical tasks",
|
152 |
+
elem_classes="footer-text"
|
153 |
+
)
|
154 |
+
|
155 |
+
submit_btn.click(
|
156 |
+
generate_text,
|
157 |
+
inputs=[prompt, max_tokens, temperature, top_p],
|
158 |
+
outputs=output
|
159 |
+
)
|
160 |
+
clear_btn.click(lambda: "", None, prompt)
|
161 |
|
162 |
demo.launch()
|