stefania11 commited on
Commit
b63d419
Β·
1 Parent(s): 98abbcd

update app file

Browse files
Files changed (1) hide show
  1. app.py +109 -4
app.py CHANGED
@@ -1,7 +1,112 @@
 
 
 
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
1
+ import json
2
+ import os
3
+
4
  import gradio as gr
5
+ import requests
6
+
7
+ APIKEY = os.environ.get("APIKEY")
8
+ APISECRET = os.environ.get("APISECRET")
9
+
10
+
11
+ def predict(prompt, lang, seed, out_seq_length, temperature, top_k, top_p):
12
+ global APIKEY
13
+ global APISECRET
14
+
15
+ if prompt == '':
16
+ return 'Input should not be empty!'
17
+
18
+ url = 'https://tianqi.aminer.cn/api/v2/multilingual_code_generate_block'
19
+
20
+ payload = json.dumps({
21
+ "apikey" : APIKEY,
22
+ "apisecret" : APISECRET,
23
+ "prompt" : prompt,
24
+ "lang" : lang,
25
+ "out_seq_length": out_seq_length,
26
+ "seed" : seed,
27
+ "temperature" : temperature,
28
+ "top_k" : top_k,
29
+ "top_p" : top_p,
30
+ })
31
+
32
+ headers = {
33
+ 'Content-Type': 'application/json'
34
+ }
35
+
36
+ try:
37
+ response = requests.request("POST", url, headers=headers, data=payload, timeout=(20, 100)).json()
38
+ except Exception as e:
39
+ return 'Timeout! Please wait a few minutes and retry'
40
+
41
+ if response['status'] == 1:
42
+ return response['message']
43
+
44
+ answer = response['result']['output']['code'][0]
45
+
46
+ return prompt + answer
47
+
48
+
49
+ def main():
50
+ gr.close_all()
51
+ examples = []
52
+ with open("./example_inputs.jsonl", "r") as f:
53
+ for line in f:
54
+ examples.append(list(json.loads(line).values()))
55
+
56
+ with gr.Blocks() as demo:
57
+ gr.Markdown(
58
+ """
59
+ <img src="https://raw.githubusercontent.com/THUDM/CodeGeeX/main/resources/logo/codegeex_logo.png">
60
+ """)
61
+ gr.Markdown(
62
+ """
63
+ <p align="center">
64
+ 🏠 <a href="https://models.aminer.cn/codegeex" target="_blank">Homepage</a> | πŸ“– <a href="http://keg.cs.tsinghua.edu.cn/codegeex/" target="_blank">Blog</a> | πŸͺ§ <a href="https://models.aminer.cn/codegeex/playground" target="_blank">DEMO</a> | πŸ›  <a href="https://marketplace.visualstudio.com/items?itemName=aminer.codegeex" target="_blank">VS Code Extension</a> | πŸ’» <a href="https://github.com/THUDM/CodeGeeX" target="_blank">Source code</a> | πŸ€– <a href="https://models.aminer.cn/codegeex/download/request" target="_blank">Download Model</a>
65
+ </p>
66
+ """)
67
+ gr.Markdown(
68
+ """
69
+ We introduce CodeGeeX, a large-scale multilingual code generation model with 13 billion parameters, pre-trained on a large code corpus of more than 20 programming languages. CodeGeeX supports 15+ programming languages for both code generation and translation. CodeGeeX is open source, please refer to our [GitHub](https://github.com/THUDM/CodeGeeX) for more details. This is a minimal-functional DEMO, for other DEMOs like code translation, please visit our [Homepage](https://models.aminer.cn/codegeex/). We also offer a free [VS Code extension](https://marketplace.visualstudio.com/items?itemName=aminer.codegeex) for full functionality.
70
+ """)
71
+
72
+ with gr.Row():
73
+ with gr.Column():
74
+ prompt = gr.Textbox(lines=13, placeholder='Please enter the description or select an example input below.',label='Input')
75
+ with gr.Row():
76
+ gen = gr.Button("Generate")
77
+ clr = gr.Button("Clear")
78
+
79
+ outputs = gr.Textbox(lines=15, label='Output')
80
+
81
+ gr.Markdown(
82
+ """
83
+ Generation Parameter
84
+ """)
85
+ with gr.Row():
86
+ with gr.Column():
87
+ lang = gr.Radio(
88
+ choices=["C++", "C", "C#", "Python", "Java", "HTML", "PHP", "JavaScript", "TypeScript", "Go",
89
+ "Rust",
90
+ "SQL", "Kotlin", "R", "Fortran"], value='lang', label='Programming Language',
91
+ default="Python")
92
+ with gr.Column():
93
+ seed = gr.Slider(maximum=10000, value=8888, step=1, label='Seed')
94
+ with gr.Row():
95
+ out_seq_length = gr.Slider(maximum=1024, value=128, minimum=1, step=1, label='Output Sequence Length')
96
+ temperature = gr.Slider(maximum=1, value=0.2, minimum=0, label='Temperature')
97
+ with gr.Row():
98
+ top_k = gr.Slider(maximum=40, value=0, minimum=0, step=1, label='Top K')
99
+ top_p = gr.Slider(maximum=1, value=1.0, minimum=0, label='Top P')
100
+
101
+ inputs = [prompt, lang, seed, out_seq_length, temperature, top_k, top_p]
102
+ gen.click(fn=predict, inputs=inputs, outputs=outputs)
103
+ clr.click(fn=lambda value: gr.update(value=""), inputs=clr, outputs=prompt)
104
+
105
+ gr_examples = gr.Examples(examples=examples, inputs=[prompt, lang],
106
+ label="Example Inputs (Click to insert an examplet it into the input box)",
107
+ examples_per_page=20)
108
 
109
+ demo.launch()
 
110
 
111
+ if __name__ == '__main__':
112
+ main()