File size: 3,091 Bytes
f12b0d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import gradio as gr
from functools import partial
from huggingface_hub import InferenceClient

css = """
#generate_button {
    transition: background-color 1s ease-out, color 1s ease-out; border-color 1s ease-out;
}
"""


def generate(prompt: str, hf_token: str, model: str):
    messages = [{"role": "user", "content": prompt}]
    if hf_token is None or not hf_token.strip():
        hf_token = os.getenv("HUGGINGFACE_API_KEY")
    client = InferenceClient(model, token=hf_token)
    model_name = model.split("/")[1]
    response = f"**{model_name}**\n\n"
    for msg in client.chat_completion(messages, max_tokens=600, stream=True):
        token = msg.choices[0].delta.content
        response += token
        yield response


def clear_token():
    # returns a textbox with visibility set to False
    # this will update the hf_token widget thus hiding it
    return gr.Textbox(visible=False)


with gr.Blocks(css=css, theme="gradio/soft") as demo:
    gr.Markdown("<center><h1>Code LLM Explorer</h1></center>")

    prompt = gr.Textbox(
        label="Prompt",
        lines=2,  # default two lines length
        max_lines=5,  # the Textbox entends upto 5 lines length
        info="Type your Prompt here",
        show_label=False,
        value="Write Bubble Sort in Python",
    )

    hf_token = gr.Textbox(
        label="HuggingFace Token",
        type="password",
        placeholder="Your Hugging Face Token",
        show_label=False,
    )

    # gr.Group() will group the two buttons together
    # so there will be no gap between two buttons
    with gr.Group():
        with gr.Row() as button_row:
            # variant: 'primary' for main call-to-action, 'secondary' for a more subdued style, 'stop' for a stop button.
            generate_btn = gr.Button(
                "Run", elem_id="generate_button", variant="primary", size="sm"
            )
            view_code = gr.Button(
                "View Code", elem_id="generate_button", variant="secondary", size="sm"
            )

    with gr.Row() as output_row:
        codellama_output = gr.Markdown("codellama/CodeLlama-34b-Instruct-hf")
        stablecode_output = gr.Markdown("stabilityai/stable-code-instruct-3b")
        deepseek_output = gr.Markdown("deepseek-ai/deepseek-coder-33b-instruct")

    gr.on(
        [prompt.submit, generate_btn.click], clear_token, inputs=None, outputs=hf_token
    ).then(
        fn=partial(generate, model="codellama/CodeLlama-34b-Instruct-hf"),
        inputs=[prompt, hf_token],
        outputs=codellama_output,
    )

    gr.on(
        [prompt.submit, generate_btn.click], clear_token, inputs=None, outputs=hf_token
    ).then(
        fn=partial(generate, model="stabilityai/stable-code-instruct-3b"),
        inputs=[prompt, hf_token],
        outputs=stablecode_output,
    )

    gr.on(
        [prompt.submit, generate_btn.click], clear_token, inputs=None, outputs=hf_token
    ).then(
        fn=partial(generate, model="microsoft/Phi-3-mini-4k-instruct"),
        inputs=[prompt, hf_token],
        outputs=deepseek_output,
    )

demo.launch()