zxl2025 commited on
Commit
bfaa716
·
verified ·
1 Parent(s): 8465dd1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -11
app.py CHANGED
@@ -2,20 +2,54 @@ 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()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+