Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModel
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# 加载模型
|
| 6 |
+
model_path = "THUDM/chatglm2-6b"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
|
| 8 |
+
model = AutoModel.from_pretrained(model_path, trust_remote_code=True)
|
| 9 |
+
model = model.half() # 使用 CPU 而不是 GPU
|
| 10 |
+
model.eval()
|
| 11 |
+
|
| 12 |
+
def generate_code(text):
|
| 13 |
+
inputs = tokenizer(text, return_tensors="pt") # 删去 `.to('cuda')`
|
| 14 |
+
with torch.no_grad():
|
| 15 |
+
response = model.generate(**inputs, max_length=200)
|
| 16 |
+
output_text = tokenizer.decode(response[0], skip_special_tokens=True)
|
| 17 |
+
return output_text
|
| 18 |
+
|
| 19 |
+
# 创建 Gradio 接口
|
| 20 |
+
demo = gr.Interface(fn=generate_code, inputs="text", outputs="text")
|
| 21 |
+
demo.launch()
|