Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,20 +2,54 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoTokenizer, AutoModel
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
-
def
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
#
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from transformers import AutoTokenizer, AutoModel
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# 预定义模型的字典,用户可以选择这些模型
|
| 6 |
+
MODELS = {
|
| 7 |
+
"Code Generation (ChatGLM2-6B)": "THUDM/chatglm2-6b",
|
| 8 |
+
"Translation (Helsinki-NLP)": "Helsinki-NLP/opus-mt-en-zh",
|
| 9 |
+
"GPT-2 Text Generation": "gpt2",
|
| 10 |
+
"BERT for Question Answering": "deepset/bert-base-cased-squad2"
|
| 11 |
+
}
|
| 12 |
|
| 13 |
+
def load_model(model_name):
|
| 14 |
+
"""
|
| 15 |
+
根据模型名称加载相应的模型和tokenizer
|
| 16 |
+
"""
|
| 17 |
+
print(f"正在加载模型: {model_name}")
|
| 18 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 19 |
+
model = AutoModel.from_pretrained(model_name, trust_remote_code=True)
|
| 20 |
+
model = model.half().cuda() if torch.cuda.is_available() else model # 如果有GPU则使用GPU
|
| 21 |
+
model.eval()
|
| 22 |
+
return tokenizer, model
|
| 23 |
+
|
| 24 |
+
def generate_code(model_name, text):
|
| 25 |
+
"""
|
| 26 |
+
使用选择的模型生成代码或文本
|
| 27 |
+
"""
|
| 28 |
+
tokenizer, model = load_model(model_name)
|
| 29 |
+
inputs = tokenizer(text, return_tensors="pt").to('cuda' if torch.cuda.is_available() else 'cpu')
|
| 30 |
with torch.no_grad():
|
| 31 |
response = model.generate(**inputs, max_length=200)
|
| 32 |
output_text = tokenizer.decode(response[0], skip_special_tokens=True)
|
| 33 |
return output_text
|
| 34 |
|
| 35 |
+
# Gradio界面
|
| 36 |
+
def main():
|
| 37 |
+
def process_input(model_name, text):
|
| 38 |
+
return generate_code(model_name, text)
|
| 39 |
+
|
| 40 |
+
demo = gr.Interface(
|
| 41 |
+
fn=process_input,
|
| 42 |
+
inputs=[
|
| 43 |
+
gr.inputs.Dropdown(choices=list(MODELS.keys()), label="选择模型"),
|
| 44 |
+
gr.inputs.Textbox(lines=5, placeholder="输入文本以生成代码或其他输出...", label="输入文本")
|
| 45 |
+
],
|
| 46 |
+
outputs="text",
|
| 47 |
+
title="AI 多模型处理平台",
|
| 48 |
+
description="从多个 Hugging Face 模型中选择一个来处理输入文本。"
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
demo.launch()
|
| 52 |
+
|
| 53 |
+
if __name__ == "__main__":
|
| 54 |
+
main()
|
| 55 |
+
|