Kingrane commited on
Commit
c00e791
·
verified ·
1 Parent(s): 13a2bfa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +143 -59
app.py CHANGED
@@ -1,78 +1,162 @@
1
  import gradio as gr
2
- from transformers import AutoModelForCausalLM, AutoTokenizer
3
- import torch
 
 
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
- torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
 
 
 
10
  )
11
- device = "cuda" if torch.cuda.is_available() else "cpu"
12
- model = model.to(device)
13
-
14
- def code_completion(prompt, max_new_tokens=128, temperature=0.2):
15
- if not prompt.strip():
16
- return "Please enter some code to complete."
17
- inputs = tokenizer(prompt, return_tensors="pt").to(device)
18
- with torch.no_grad():
19
- outputs = model.generate(
20
- **inputs,
21
- max_new_tokens=max_new_tokens,
22
- temperature=temperature,
23
- do_sample=True,
24
- pad_token_id=tokenizer.eos_token_id
25
- )
26
- generated = tokenizer.decode(outputs[0], skip_special_tokens=True)
27
- return generated[len(prompt):]
28
 
29
  custom_css = """
30
- body {background: #f7f8fa;}
31
- .gradio-container {background: #f7f8fa;}
32
- h1, h2, h3, h4, h5, h6 {font-family: 'Inter', sans-serif;}
33
- #main-title {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  text-align: center;
35
- font-weight: 800;
36
- font-size: 2.3em;
37
- margin-bottom: 0.2em;
38
- letter-spacing: -1px;
39
- color: #222;
40
  }
41
- #subtitle {
 
42
  text-align: center;
43
- color: #6c6f7a;
44
- font-size: 1.1em;
45
- margin-bottom: 2em;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  }
47
- .gr-box {border-radius: 16px;}
48
  """
49
 
50
- with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
51
- gr.Markdown(
52
- """
53
- <h1 id="main-title">Devstral Code Autocomplete</h1>
54
- <div id="subtitle">Minimal, beautiful code completion powered by <b>Devstral</b></div>
55
- """)
56
- with gr.Row():
57
- with gr.Column(scale=1):
58
  prompt = gr.Textbox(
59
- label="Your code prompt",
60
- lines=10,
61
- placeholder="def quicksort(arr):\n \"\"\"Sort the array using quicksort algorithm.\"\"\"\n if len(arr) <= 1:\n return arr\n pivot = arr[len(arr) // 2]\n ",
62
- show_copy_button=True,
63
- autofocus=True
64
  )
 
65
  with gr.Row():
66
- max_tokens = gr.Slider(16, 256, value=128, step=8, label="Max new tokens")
67
- temperature = gr.Slider(0.1, 1.0, value=0.2, step=0.05, label="Temperature")
68
- btn = gr.Button("Generate Completion", elem_id="generate-btn")
69
- with gr.Column(scale=1):
70
- output = gr.Code(
71
- label="Generated code",
72
- language="python",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  lines=12,
74
- interactive=False
75
  )
76
- btn.click(code_completion, inputs=[prompt, max_tokens, temperature], outputs=output)
 
 
 
 
 
 
 
 
 
 
 
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()