Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,22 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# 导入所需库
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
import gradio as gr
|
4 |
|
5 |
+
# 加载模型和分词器
|
6 |
+
checkpoint = "bigcode/santacoder"
|
7 |
+
device = "cuda" # 若要使用GPU,请将其设置为"cuda",若使用CPU,请设置为"cpu"
|
8 |
+
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
|
10 |
+
model = AutoModelForCausalLM.from_pretrained(checkpoint, revision="main", trust_remote_code=True).to(device)
|
11 |
+
|
12 |
+
# 创建代码生成函数
|
13 |
+
def generate_code(nl_description):
|
14 |
+
inputs = tokenizer.encode(nl_description, return_tensors="pt").to(device)
|
15 |
+
outputs = model.generate(inputs)
|
16 |
+
return tokenizer.decode(outputs[0])
|
17 |
+
|
18 |
+
# 使用Gradio创建界面
|
19 |
+
input_interface = gr.inputs.Textbox(lines=5, placeholder="请输入自然语言描述...")
|
20 |
+
output_interface = gr.outputs.Code()
|
21 |
+
|
22 |
+
gr.Interface(fn=generate_code, inputs=input_interface, outputs=output_interface, title="SantaCoder代码生成器").launch()
|