Kingrane commited on
Commit
4664dfb
·
verified ·
1 Parent(s): 182c7c5

Update app.py

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