Zoe911 commited on
Commit
a57d41c
Β·
verified Β·
1 Parent(s): 154623f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
+
5
+ # 加载Qwen1.5-7B-Chatζ¨‘εž‹
6
+ model_name = "Qwen/Qwen1.5-7B-Chat"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
8
+ model = AutoModelForCausalLM.from_pretrained(
9
+ model_name,
10
+ trust_remote_code=True,
11
+ torch_dtype=torch.bfloat16,
12
+ device_map="auto"
13
+ )
14
+
15
+
16
+ def chat_with_qwen(message):
17
+
18
+ messages = [{"role": "user", "content": message}]
19
+ inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
20
+
21
+
22
+ outputs = model.generate(
23
+ inputs,
24
+ max_new_tokens=512,
25
+ do_sample=True,
26
+ temperature=0.7,
27
+ top_p=0.95
28
+ )
29
+ response = tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True)
30
+ return response
31
+
32
+
33
+ iface = gr.Interface(
34
+ fn=chat_with_qwen,
35
+ inputs=gr.Textbox(lines=2, placeholder="θΎ“ε…₯δ½ ηš„ζΆˆζ―..."),
36
+ outputs="text",
37
+ title="Qwen Chatbot API",
38
+ description="基于Qwen1.5-7B-Chatηš„δΈ­ζ–‡δΌ˜εŒ–θŠε€©ζœΊε™¨δΊΊ"
39
+ )
40
+
41
+ iface.launch(server_name="0.0.0.0", server_port=7860)