File size: 4,283 Bytes
a600446
 
 
 
 
 
 
 
 
ff68452
 
 
 
 
 
 
 
777a53e
 
 
 
 
 
 
ff68452
 
 
 
 
 
 
 
 
 
a600446
f5b7d69
a600446
 
f5b7d69
a600446
29752d3
a600446
 
 
7447bf0
a600446
 
 
 
 
 
 
 
29752d3
a600446
 
 
 
29752d3
a600446
 
29752d3
a600446
 
777a53e
ff68452
 
ab9f16b
 
ff68452
 
 
 
 
 
 
 
 
 
1ae9bce
ff68452
 
 
 
 
 
 
 
 
777a53e
 
 
 
 
ff68452
 
 
 
 
1ae9bce
29752d3
a600446
 
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
from transformers import AutoTokenizer
import transformers
import os
import sys
import fire
import torch
import gradio as gr


MAGICODER_PROMPT = """You are an exceptionally intelligent coding assistant that consistently delivers accurate and reliable responses to user instructions.

@@ Instruction
{instruction}

@@ Response
"""

css = """
#q-output {
    max-height: 60vh;
    overflow: auto;
}
"""

title = "<h1 style='text-align: center; margin-bottom: 1rem'>🎩 Magicoder</h1>"

description = """This is a playground for Magicoder-S-DS-6.7B! Follow us on Github: https://github.com/ise-uiuc/magicoder and Huggingface: https://huggingface.co/ise-uiuc.

💡 Tips: You can include more details in your instruction for Magicoder to write better code for you! Details can be, but not limited to, as follows:
1. Specify your programming language (e.g., "... in Python" and "... in Java")
2. Specify the external libraries/packages that are necessary in your task (e.g., "... using the turtle library" and "Write a gradio application to ...")
3. Demonstrate your requirements as clear and comprehensive as possible (e.g., "use CNN as the model structure" and "draw a chart of the training loss")"""


def main(
    base_model="ise-uiuc/Magicoder-S-DS-6.7B"
):
    pipeline = transformers.pipeline(
        "text-generation", model=base_model, torch_dtype=torch.bfloat16, device_map="auto"
    )

    def evaluate_magicoder(
        instruction,
        temperature=1,
        max_length=2048,
    ):
        prompt = MAGICODER_PROMPT.format(instruction=instruction)

        if temperature > 0:
            sequences = pipeline(
                prompt,
                do_sample=True,
                temperature=temperature,
                max_length=max_length,
            )
        else:
            sequences = pipeline(
                prompt,
                max_length=max_length,
            )
        for seq in sequences:
            generated_text = seq["generated_text"].replace(prompt, "")
            return generated_text

    with gr.Blocks(css=css) as demo:
        gr.Markdown(title)
        gr.Markdown(description)
        with gr.Row(equal_height=True):
            with gr.Column(variant="default"):
                interface_input = gr.Textbox(
                    lines=3,
                    label="Instruction",
                    placeholder="Anything you want to ask Magicoder ?",
                    value="Write a snake game in Python using the turtle library (the game is created by Magicoder).",
                )
                temperature = gr.Slider(minimum=0, maximum=1, value=0, label="Temperature")
                max_new_tokens = gr.Slider(
                    minimum=1, maximum=2048, step=1, value=2048, label="Max tokens"
                )
                submit = gr.Button(value="Generate")
                gr.Examples(
                    examples=[
                        ["Write a snake game in Python using the turtle library (the game is created by Magicoder)."],
                        ["Build a console-based Othello game in Java with row and column numbers shown on the board. The game should end when there are no more valid moves for either player."],
                        ["Write a gradio (3.48.0) application for the following use case: Take an input image and return a 45 degree clockwise rotated image. You should also add text description under the output showing the rotation degree."],
                        ["Build a simple neural network in Python using Pytorch to classify handwritten digits from the MNIST dataset. You should use CNN as the model structure, train the model for 5 epochs, draw a chart of the training loss, and show the final result."],
                    ],
                    inputs = [interface_input]
                )
            with gr.Column(variant="default"):
                with gr.Tab("Output", elem_id="q-output"):
                    output = gr.Markdown(
                        label="Output"
                    )
            submit.click(
                evaluate_magicoder,
                inputs=[interface_input, temperature, max_new_tokens],
                outputs=[output],
            )
    demo.queue().launch()

if __name__ == "__main__":
    fire.Fire(main)