Tonic commited on
Commit
173046e
1 Parent(s): a88e84c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM
4
+ import spaces
5
+
6
+ title = """# 🙋🏻‍♂️Welcome to 🌟Tonic's ☯️🧑‍💻Yi-Coder-9B-Chat Demo!"""
7
+ description = """Yi-Coder-9B-Chat is a 9B parameter model fine-tuned for coding tasks. This demo showcases its ability to generate code based on your prompts. Yi-Coder is a series of open-source code language models that delivers state-of-the-art coding performance with fewer than 10 billion parameters. Excelling in long-context understanding with a maximum context length of 128K tokens. - Supporting 52 major programming languages:
8
+ ```bash
9
+ 'java', 'markdown', 'python', 'php', 'javascript', 'c++', 'c#', 'c', 'typescript', 'html', 'go', 'java_server_pages', 'dart', 'objective-c', 'kotlin', 'tex', 'swift', 'ruby', 'sql', 'rust', 'css', 'yaml', 'matlab', 'lua', 'json', 'shell', 'visual_basic', 'scala', 'rmarkdown', 'pascal', 'fortran', 'haskell', 'assembly', 'perl', 'julia', 'cmake', 'groovy', 'ocaml', 'powershell', 'elixir', 'clojure', 'makefile', 'coffeescript', 'erlang', 'lisp', 'toml', 'batchfile', 'cobol', 'dockerfile', 'r', 'prolog', 'verilog'
10
+ ```
11
+ ### Join us :
12
+ 🌟TeamTonic🌟 is always making cool demos! Join our active builder's 🛠️community 👻 [![Join us on Discord](https://img.shields.io/discord/1109943800132010065?label=Discord&logo=discord&style=flat-square)](https://discord.gg/GWpVpekp) On 🤗Huggingface:[MultiTransformer](https://huggingface.co/MultiTransformer) On 🌐Github: [Tonic-AI](https://github.com/tonic-ai) & contribute to🌟 [Build Tonic](https://git.tonic-ai.com/contribute)🤗Big thanks to Yuvi Sharma and all the folks at huggingface for the community grant 🤗
13
+ """
14
+
15
+ # Define the device and model path
16
+ device = "cuda" if torch.cuda.is_available() else "cpu"
17
+ model_path = "01-ai/Yi-Coder-9B-Chat"
18
+
19
+ # Load the tokenizer and model
20
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
21
+ model = AutoModelForCausalLM.from_pretrained(model_path, device_map="auto").eval()
22
+
23
+ @spaces.GPU
24
+ def generate_code(system_prompt, user_prompt, max_length):
25
+ messages = [
26
+ {"role": "system", "content": system_prompt},
27
+ {"role": "user", "content": user_prompt}
28
+ ]
29
+ text = tokenizer.apply_chat_template(
30
+ messages,
31
+ tokenize=False,
32
+ add_generation_prompt=True
33
+ )
34
+ model_inputs = tokenizer([text], return_tensors="pt").to(device)
35
+
36
+ generated_ids = model.generate(
37
+ model_inputs.input_ids,
38
+ max_new_tokens=max_length,
39
+ eos_token_id=tokenizer.eos_token_id
40
+ )
41
+ generated_ids = [
42
+ output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
43
+ ]
44
+
45
+ response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
46
+ return response
47
+
48
+ def gradio_interface():
49
+ with gr.Blocks() as interface:
50
+ gr.Markdown(title)
51
+ gr.Markdown(description)
52
+
53
+ system_prompt_input = gr.Textbox(
54
+ label="☯️Yinstruction:",
55
+ value="You are a helpful coding assistant. Provide clear and concise code examples.",
56
+ lines=2
57
+ )
58
+ user_prompt_input = gr.Code(
59
+ label="🤔Coding Question",
60
+ value="Write a quick sort algorithm in Python.",
61
+ language="python",
62
+ lines=15
63
+ )
64
+ code_output = gr.Code(label="☯️Yi-Coder-7B", language='python', lines=20, interactive=True)
65
+ max_length_slider = gr.Slider(minimum=1, maximum=1800, value=650, label="Max Token Length")
66
+
67
+ generate_button = gr.Button("Generate Code")
68
+ generate_button.click(
69
+ generate_code,
70
+ inputs=[system_prompt_input, user_prompt_input, max_length_slider],
71
+ outputs=code_output
72
+ )
73
+
74
+ return interface
75
+
76
+ if __name__ == "__main__":
77
+ interface = gradio_interface()
78
+ interface.launch()