File size: 9,745 Bytes
0285eae
 
 
 
 
 
 
 
 
 
 
 
 
 
be3a86c
ce01d23
0285eae
 
 
 
 
 
 
92776ad
 
0285eae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92776ad
 
0285eae
 
b7e6d05
ce01d23
 
 
94dec10
 
 
 
 
 
 
 
0285eae
 
 
 
 
 
 
bc7ce97
0285eae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92776ad
0285eae
 
 
 
92776ad
0285eae
92776ad
 
0285eae
 
ce01d23
9e92718
 
5ba82e6
ce01d23
 
9e92718
 
ce01d23
 
6983cac
c2b8350
0285eae
 
82dd2cd
0285eae
82dd2cd
0285eae
bcf574f
8f202e2
0285eae
d0e67bd
0285eae
 
 
25a8247
82dd2cd
0285eae
0254cf6
94dec10
0285eae
 
 
 
 
 
 
 
 
 
 
 
 
 
72fa8dc
0285eae
 
4e0d0f4
ce01d23
 
 
 
 
 
 
0285eae
 
 
 
 
 
 
 
bc7ce97
 
 
0285eae
 
 
 
 
 
 
 
 
 
6844c01
0285eae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7c596a
0285eae
 
 
 
 
 
 
92776ad
 
0285eae
 
 
 
 
 
 
 
 
 
 
 
 
 
d91aca7
0285eae
 
 
 
 
 
 
 
c9eb8b9
0285eae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce01d23
 
6983cac
ce01d23
 
 
0285eae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce01d23
0285eae
 
 
 
 
 
 
ce01d23
0285eae
 
 
 
 
 
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import json
import os
import shutil
import requests
import warnings

import gradio as gr
from huggingface_hub import Repository
from text_generation import Client

from share_btn import community_icon_html, loading_icon_html, share_js, share_btn_css

HF_TOKEN = os.environ.get("HF_TOKEN", None)

API_URL_G = "https://api-inference.huggingface.co/models/ArmelR/starcoder-gradio-v0"
API_URL_S = "https://api-inference.huggingface.co/models/HuggingFaceH4/starcoderbase-finetuned-oasst1"

with open("./HHH_prompt_short.txt", "r") as f:
    HHH_PROMPT = f.read() + "\n\n"

with open("./TA_prompt_v0.txt", "r") as f:
    TA_PROMPT = f.read()

NO_PROMPT = ""

FIM_PREFIX = "<fim_prefix>"
FIM_MIDDLE = "<fim_middle>"
FIM_SUFFIX = "<fim_suffix>"

FIM_INDICATOR = "<FILL_HERE>"

FORMATS = """
# Chat mode
Chat mode prepends the custom [TA prompt](https://huggingface.co/spaces/bigcode/chat-playground/blob/main/TA_prompt_v0.txt) or the [HHH prompt](https://gist.github.com/jareddk/2509330f8ef3d787fc5aaac67aab5f11#file-hhh_prompt-txt) from Anthropic to the request which conditions the model to serve as an assistant.

⚠️ **Intended Use**: this app and its [supporting model](https://huggingface.co/bigcode) are provided for demonstration purposes; not to serve as replacement for human expertise. For more details on the model's limitations in terms of factuality and biases, see the [model card.](hf.co/bigcode)

"""

theme = gr.themes.Monochrome(
    primary_hue="indigo",
    secondary_hue="blue",
    neutral_hue="slate",
    radius_size=gr.themes.sizes.radius_sm,
    font=[
        gr.themes.GoogleFont("Open Sans"),
        "ui-sans-serif",
        "system-ui",
        "sans-serif",
    ],
)

client_g = Client(
    API_URL_G, headers={"Authorization": f"Bearer {HF_TOKEN}"},
)

client_s = Client(
    API_URL_S, headers={"Authorization": f"Bearer {HF_TOKEN}"},
)

def wrap_html_code(text):
    pattern = r"<.*?>"
    matches = re.findall(pattern, text)
    if len(matches) > 0:
        return f"```{text}```"
    else:
        return text
        
def generate(
    prompt,
    temperature=0.9,
    max_new_tokens=256,
    top_p=0.95,
    repetition_penalty=1.0,
    chat_mode="TA prompt",
    version="StarCoder-gradio",
):

    temperature = float(temperature)
    if temperature < 1e-2:
        temperature = 1e-2
    top_p = float(top_p)
    fim_mode = False

    generate_kwargs = dict(
        temperature=temperature,
        max_new_tokens=max_new_tokens,
        top_p=top_p,
        repetition_penalty=repetition_penalty,
        truncate=7500,
        do_sample=True,
        seed=42,
        stop_sequences=["\nHuman", "\n-----", "Question:", "Answer:"],
    )
    
    if chat_mode == "HHH prompt":
        base_prompt = HHH_PROMPT
    elif chat_mode == "TA prompt":
        base_prompt = TA_PROMPT
    else :
        base_prompt = NO_PROMPT


    if version == "StarCoder-gradio" :
        chat_prompt = prompt + "\n\nAnswer:"
        prompt = base_prompt + chat_prompt
        print("PROMPT : "+str(prompt))
        stream = client_g.generate_stream(prompt, **generate_kwargs)
    elif version == "StarChat-alpha" :
        chat_prompt = prompt + "\n\nAssistant:"
        prompt = base_prompt + chat_prompt
        stream = client_s.generate_stream(prompt, **generate_kwargs)
    else :
        ValueError("Unsupported version of the Coding assistant")
      
    output = ""
    previous_token = ""
    #t = 0
    for response in stream:
        #print(f"IN_{t}")
        if (
            (response.token.text in ["Human", "-----", "Question:"] and previous_token in ["\n", "-----"])
            or response.token.text in ["<|endoftext|>", "<|end|>", "Answer:"]
        ):
            print("OUT")
            return output.strip()
        else:
            output += response.token.text
            #print(f"Out_{t} : {output}")
            #t += 1
        previous_token = response.token.text
    print("Output = "+str(output))
    return wrap_html_code(output.strip())


# chatbot mode
def user(user_message, history):
    return "", history + [[user_message, None]]


def bot(
    history,
    temperature=0.9,
    max_new_tokens=256,
    top_p=0.95,
    repetition_penalty=1.0,
    chat_mode=None,
    version="StarChat", 
):
    # concat history of prompts with answers expect for last empty answer only add prompt
    if version == "StarCoder-gradio" :
        prompt = "\n".join(
            [f"Question: {prompt}\n\nAnswer: {answer}" for prompt, answer in history[:-1]] + [f"\nQuestion: {history[-1][0]}"]
        )
    else :
        prompt = "\n".join(
            [f"Human: {prompt}\n\nAssistant: {answer}" for prompt, answer in history[:-1]] + [f"\nHuman: {history[-1][0]}"]
        )
    
    bot_message = generate(
        prompt,
        temperature=temperature,
        max_new_tokens=max_new_tokens,
        top_p=top_p,
        repetition_penalty=repetition_penalty,
        chat_mode=chat_mode,
        version=version
        
        
    )
    history[-1][1] = bot_message
    return history


examples = [
    "def print_hello_world():",
    "def fibonacci(n):",
    "class TransformerDecoder(nn.Module):",
    "class ComplexNumbers:",
    "How to install gradio"
]


def process_example(args):
    for x in generate(args):
        pass
    return x


css = ".generating {visibility: hidden}" + share_btn_css

with gr.Blocks(theme=theme, analytics_enabled=False, css=css) as demo:
    with gr.Column():
        gr.Markdown(
            """\
#Gradio Assistant powered by ‍💫 StarCoder
_Note:_ this is an internal chat playground - **please do not share**. The deployment can also change and thus the space not work as we continue development.\
"""
        )
        with gr.Row():
            column_1, column_2 = gr.Column(scale=3), gr.Column(scale=1)
            with column_2:
                chat_mode = gr.Dropdown(
                    ["NO prompt","TA prompt", "HHH prompt"],
                    value="NO prompt",
                    label="Chat mode",
                    info="Use Anthropic's HHH prompt or our custom tech prompt to turn the model into an assistant.",
                )
                temperature = gr.Slider(
                    label="Temperature",
                    value=0.2,
                    minimum=0.0,
                    maximum=2.0,
                    step=0.1,
                    interactive=True,
                    info="Higher values produce more diverse outputs",
                )
                max_new_tokens = gr.Slider(
                    label="Max new tokens",
                    value=512,
                    minimum=0,
                    maximum=8192,
                    step=64,
                    interactive=True,
                    info="The maximum numbers of new tokens",
                )
                top_p = gr.Slider(
                    label="Top-p (nucleus sampling)",
                    value=0.95,
                    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.2,
                    minimum=1.0,
                    maximum=2.0,
                    step=0.05,
                    interactive=True,
                    info="Penalize repeated tokens",
                )
                version = gr.Dropdown(
                    ["StarCoder-gradio", "StarChat-alpha"],
                    value="StarCoder-gradio",
                    label="Version",
                    info="",
                )
            with column_1:
                # output = gr.Code(elem_id="q-output")
                # add visibl=False and update if chat_mode True
                chatbot = gr.Chatbot()
                instruction = gr.Textbox(
                    placeholder="Enter your prompt here",
                    label="Prompt",
                    elem_id="q-input",
                )
                with gr.Row():
                    with gr.Column():
                        clear = gr.Button("Clear Chat")
                    with gr.Column():
                        submit = gr.Button("Generate", variant="primary")
                with gr.Group(elem_id="share-btn-container"):
                    community_icon = gr.HTML(community_icon_html, visible=True)
                    loading_icon = gr.HTML(loading_icon_html, visible=True)
                    share_button = gr.Button(
                        "Share to community", elem_id="share-btn", visible=True
                    )
                # examples of non-chat mode
                #gr.Examples(
                #    examples=examples,
                #    inputs=[instruction],
                #    cache_examples=False,
                #    fn=process_example,
                #    outputs=[output],
                # )
                gr.Markdown(FORMATS)


        instruction.submit(
            user, [instruction, chatbot], [instruction, chatbot], queue=False
        ).then(
            bot,
            [chatbot, temperature, max_new_tokens, top_p, repetition_penalty, chat_mode, version],
            chatbot,
        )

        submit.click(
            user, [instruction, chatbot], [instruction, chatbot], queue=False
        ).then(
            bot,
            [chatbot, temperature, max_new_tokens, top_p, repetition_penalty, chat_mode, version],
            chatbot,
        )
        clear.click(lambda: None, None, chatbot, queue=False)
    
    share_button.click(None, [], [], _js=share_js)
demo.queue(concurrency_count=16).launch(debug=True)