Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoTokenizer
|
2 |
+
import transformers
|
3 |
+
import os
|
4 |
+
import sys
|
5 |
+
import fire
|
6 |
+
import torch
|
7 |
import gradio as gr
|
8 |
|
9 |
+
|
10 |
+
MAGICODER_PROMPT = """You are an exceptionally intelligent coding assistant that consistently delivers accurate and reliable responses to user instructions.
|
11 |
+
|
12 |
+
@@ Instruction
|
13 |
+
{instruction}
|
14 |
+
|
15 |
+
@@ Response
|
16 |
+
"""
|
17 |
+
|
18 |
+
css = """
|
19 |
+
#q-output {
|
20 |
+
max-height: 60vh;
|
21 |
+
overflow: auto;
|
22 |
+
}
|
23 |
+
"""
|
24 |
+
|
25 |
+
title = "<h1 style='text-align: center; margin-bottom: 1rem'>🎩 Magicoder</h1>"
|
26 |
+
|
27 |
+
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.
|
28 |
+
|
29 |
+
💡 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:
|
30 |
+
1. Specify your programming language (e.g., "... in Python" and "... in Java")
|
31 |
+
2. Specify the external libraries/packages that are necessary in your task (e.g., "... using the turtle library" and "Write a gradio application to ...")
|
32 |
+
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")"""
|
33 |
+
|
34 |
+
|
35 |
+
def main(
|
36 |
+
base_model="ise-uiuc/Magicoder-S-DS-6.7B"
|
37 |
+
):
|
38 |
+
pipeline = transformers.pipeline(
|
39 |
+
"text-generation", model=base_model, torch_dtype=torch.bfloat16, device_map="auto"
|
40 |
+
)
|
41 |
+
|
42 |
+
def evaluate_magicoder(
|
43 |
+
instruction,
|
44 |
+
temperature=1,
|
45 |
+
max_length=2048,
|
46 |
+
):
|
47 |
+
prompt = MAGICODER_PROMPT.format(instruction=instruction)
|
48 |
+
|
49 |
+
if temperature > 0:
|
50 |
+
sequences = pipeline(
|
51 |
+
prompt,
|
52 |
+
do_sample=True,
|
53 |
+
temperature=temperature,
|
54 |
+
max_length=max_length,
|
55 |
+
)
|
56 |
+
else:
|
57 |
+
sequences = pipeline(
|
58 |
+
prompt,
|
59 |
+
max_length=max_length,
|
60 |
+
)
|
61 |
+
for seq in sequences:
|
62 |
+
generated_text = seq["generated_text"].replace(prompt, "")
|
63 |
+
return generated_text
|
64 |
+
|
65 |
+
with gr.Blocks(css=css) as demo:
|
66 |
+
gr.Markdown(title)
|
67 |
+
gr.Markdown(description)
|
68 |
+
with gr.Row(equal_height=True):
|
69 |
+
with gr.Column(variant="default"):
|
70 |
+
interface_input = gr.Textbox(
|
71 |
+
lines=3,
|
72 |
+
label="Instruction",
|
73 |
+
placeholder="Anything you want to ask Magicoder ?",
|
74 |
+
value="Write a snake game in Python using the turtle library (the game is created by Magicoder).",
|
75 |
+
)
|
76 |
+
temperature = gr.Slider(minimum=0, maximum=1, value=0, label="Temperature")
|
77 |
+
max_new_tokens = gr.Slider(
|
78 |
+
minimum=1, maximum=2048, step=1, value=2048, label="Max tokens"
|
79 |
+
)
|
80 |
+
submit = gr.Button(value="Generate")
|
81 |
+
gr.Examples(
|
82 |
+
examples=[
|
83 |
+
["Write a snake game in Python using the turtle library (the game is created by Magicoder)."],
|
84 |
+
["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."],
|
85 |
+
["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."],
|
86 |
+
["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."],
|
87 |
+
],
|
88 |
+
inputs = [interface_input]
|
89 |
+
)
|
90 |
+
with gr.Column(variant="default"):
|
91 |
+
with gr.Tab("Output", elem_id="q-output"):
|
92 |
+
output = gr.Markdown(
|
93 |
+
label="Output"
|
94 |
+
)
|
95 |
+
submit.click(
|
96 |
+
evaluate_magicoder,
|
97 |
+
inputs=[interface_input, temperature, max_new_tokens],
|
98 |
+
outputs=[output],
|
99 |
+
)
|
100 |
+
demo.queue().launch()
|
101 |
+
|
102 |
+
if __name__ == "__main__":
|
103 |
+
fire.Fire(main)
|