File size: 7,934 Bytes
8be6f3b
 
e01a839
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a986525
 
 
e01a839
 
 
a986525
 
e01a839
 
 
 
a986525
 
e01a839
 
 
 
 
 
 
 
a986525
e01a839
 
 
a986525
e01a839
 
 
 
 
 
a986525
e01a839
 
 
 
 
 
a986525
e01a839
 
 
 
 
 
a986525
e01a839
 
 
 
 
 
a986525
e01a839
 
 
a986525
 
e01a839
 
 
a986525
e01a839
 
 
 
 
 
a986525
e01a839
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a986525
 
e01a839
a986525
e01a839
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8be6f3b
e01a839
 
 
 
 
 
8be6f3b
e01a839
 
 
 
 
 
 
 
 
43e4710
e01a839
 
 
45d12c1
 
e01a839
fadbdc4
 
e01a839
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import random

import gradio as gr
import torch
from gradio.themes.utils import sizes
from transformers import AutoModelForCausalLM, AutoTokenizer

import utils
from constants import END_OF_TEXT

# Load the tokenizer and model
tokenizer = AutoTokenizer.from_pretrained(
    "BEE-spoke-data/smol_llama-101M-GQA-python",
    use_fast=False,
)
tokenizer.pad_token_id = tokenizer.eos_token_id
tokenizer.pad_token = END_OF_TEXT
model = AutoModelForCausalLM.from_pretrained(
    "BEE-spoke-data/smol_llama-101M-GQA-python",
    device_map="auto",
)
model = torch.compile(model, mode="reduce-overhead")

# UI things

_styles = utils.get_file_as_string("styles.css")

# Loads ./README.md file & splits it into sections
readme_file_content = utils.get_file_as_string("README.md", path="./")
(
    manifest,
    description,
    disclaimer,
    base_model_info,
    formats,
) = utils.get_sections(readme_file_content, "---", up_to=5)

theme = gr.themes.Soft(
    primary_hue="yellow",
    secondary_hue="orange",
    neutral_hue="slate",
    radius_size=sizes.radius_sm,
    font=[
        gr.themes.GoogleFont("IBM Plex Sans", [400, 600]),
        "ui-sans-serif",
        "system-ui",
        "sans-serif",
    ],
    text_size=sizes.text_lg,
)


def run_inference(
    prompt, temperature, max_new_tokens, top_p, repetition_penalty
) -> str:
    inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
    outputs = model.generate(
        **inputs,
        do_sample=True,
        early_stopping=True,
        max_new_tokens=max_new_tokens,
        min_new_tokens=8,
        no_repeat_ngram_size=6,
        num_beams=3,
        renormalize_logits=True,
        repetition_penalty=repetition_penalty,
        temperature=temperature,
        top_p=top_p,
    )
    text = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
    return text


examples = [
    ["def add_numbers(a, b):\n    return", 0.2, 96, 0.9, 1.2],
    [
        "class Car:\n    def __init__(self, make, model):\n        self.make = make\n        self.model = model\n\n    def display_car(self):",
        0.2,
        96,
        0.9,
        1.2,
    ],
    [
        "import pandas as pd\ndata = {'Name': ['Tom', 'Nick', 'John'], 'Age': [20, 21, 19]}\ndf = pd.DataFrame(data).convert_dtypes()\n# eda",
        0.2,
        96,
        0.9,
        1.2,
    ],
    [
        "def factorial(n):\n    if n == 0:\n        return 1\n    else:",
        0.2,
        96,
        0.9,
        1.2,
    ],
    [
        'def fibonacci(n):\n    if n <= 0:\n        raise ValueError("Incorrect input")\n    elif n == 1:\n        return 0\n    elif n == 2:\n        return 1\n    else:',
        0.2,
        96,
        0.9,
        1.2,
    ],
    [
        "import matplotlib.pyplot as plt\nimport numpy as np\nx = np.linspace(0, 10, 100)\n# simple plot",
        0.2,
        96,
        0.9,
        1.2,
    ],
    ["def reverse_string(s:str) -> str:\n    return", 0.2, 96, 0.9, 1.2],
    ["def is_palindrome(word:str) -> bool:\n    return", 0.2, 96, 0.9, 1.2],
    [
        "def bubble_sort(lst: list):\n    n = len(lst)\n    for i in range(n):\n        for j in range(0, n-i-1):",
        0.2,
        96,
        0.9,
        1.2,
    ],
    [
        "def binary_search(arr, low, high, x):\n    if high >= low:\n        mid = (high + low) // 2\n        if arr[mid] == x:\n            return mid\n        elif arr[mid] > x:",
        0.2,
        96,
        0.9,
        1.2,
    ],
]

# Define the Gradio Blocks interface
with gr.Blocks(theme=theme, analytics_enabled=False, css=_styles) as demo:
    with gr.Column():
        gr.Markdown(description)
        with gr.Row():
            with gr.Column():
                instruction = gr.Textbox(
                    value=random.choice([e[0] for e in examples]),
                    placeholder="Enter your code here",
                    label="Code",
                    elem_id="q-input",
                )
                submit = gr.Button("Generate", variant="primary")
                output = gr.Code(elem_id="q-output", language="python", lines=10)
                with gr.Row():
                    with gr.Column():
                        with gr.Accordion("Advanced settings", open=False):
                            with gr.Row():
                                column_1, column_2 = gr.Column(), gr.Column()
                                with column_1:
                                    temperature = gr.Slider(
                                        label="Temperature",
                                        value=0.2,
                                        minimum=0.0,
                                        maximum=1.0,
                                        step=0.05,
                                        interactive=True,
                                        info="Higher values produce more diverse outputs",
                                    )
                                    max_new_tokens = gr.Slider(
                                        label="Max new tokens",
                                        value=64,
                                        minimum=32,
                                        maximum=512,
                                        step=32,
                                        interactive=True,
                                        info="Number of tokens to generate",
                                    )
                                with column_2:
                                    top_p = gr.Slider(
                                        label="Top-p (nucleus sampling)",
                                        value=0.90,
                                        minimum=0.0,
                                        maximum=1,
                                        step=0.05,
                                        interactive=True,
                                        info="Higher values sample more low-probability tokens",
                                    )
                                    repetition_penalty = gr.Slider(
                                        label="Repetition penalty",
                                        value=1.1,
                                        minimum=1.0,
                                        maximum=2.0,
                                        step=0.05,
                                        interactive=True,
                                        info="Penalize repeated tokens",
                                    )
                    with gr.Column():
                        version = gr.Dropdown(
                            [
                                "smol_llama-101M-GQA-python",
                            ],
                            value="smol_llama-101M-GQA-python",
                            label="Version",
                            info="",
                        )
                gr.Markdown(disclaimer)
                gr.Examples(
                    examples=examples,
                    inputs=[
                        instruction,
                        temperature,
                        max_new_tokens,
                        top_p,
                        repetition_penalty,
                        version,
                    ],
                    cache_examples=False,
                    fn=run_inference,
                    outputs=[output],
                )
                gr.Markdown(base_model_info)
                gr.Markdown(formats)

    submit.click(
        run_inference,
        inputs=[
            instruction,
            temperature,
            max_new_tokens,
            top_p,
            repetition_penalty,
        ],
        outputs=[output],
        # preprocess=False,
        # batch=False,
        show_progress=True,
    )

# .queue(max_size=10, api_open=False)
demo.launch(
    debug=True,
    show_api=False,
    share=utils.is_google_colab(),
)