cnmksjs commited on
Commit
3f403d6
Β·
verified Β·
1 Parent(s): 5e5244f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -17
app.py CHANGED
@@ -11,28 +11,29 @@ model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True)
11
  tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
12
 
13
  # η”Ÿζˆε›žε€ηš„ε‡½ζ•°
14
- def chat(input_text, history=[]):
15
- inputs = tokenizer(input_text, return_tensors="pt").to(device)
16
  with torch.no_grad():
17
- outputs = model.generate(**inputs, max_length=512, temperature=0.7)
18
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
19
- return response
 
20
 
21
  # εˆ›ε»Ί Gradio η•Œι’
22
  with gr.Blocks() as demo:
23
- gr.Markdown("## DeepSeek-V3 Chatbot")
24
-
25
  chatbot = gr.Chatbot(label="Chatbot")
26
- msg = gr.Textbox(label="Type a message...", placeholder="Enter your message here...", lines=2)
27
- submit = gr.Button("Submit")
 
 
 
 
28
 
29
- def respond(user_input, chat_history):
30
- response = chat(user_input, chat_history)
31
- chat_history.append((user_input, response))
32
- return "", chat_history
33
 
34
- submit.click(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
35
-
36
- # 启动 Gradio 应用
37
- if __name__ == "__main__":
38
- demo.launch()
 
11
  tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
12
 
13
  # η”Ÿζˆε›žε€ηš„ε‡½ζ•°
14
+ def chat(input_text, chat_history=[]):
15
+ inputs = tokenizer(input_text, return_tensors="pt").to("cuda")
16
  with torch.no_grad():
17
+ outputs = model.generate(**inputs, max_length=512)
18
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
19
+ chat_history.append((input_text, response))
20
+ return chat_history, ""
21
 
22
  # εˆ›ε»Ί Gradio η•Œι’
23
  with gr.Blocks() as demo:
24
+ gr.Markdown("# πŸ€– Chatbot")
 
25
  chatbot = gr.Chatbot(label="Chatbot")
26
+ msg = gr.Textbox(placeholder="Type a message...")
27
+ with gr.Row():
28
+ submit_btn = gr.Button("Submit")
29
+ retry_btn = gr.Button("πŸ”„ Retry")
30
+ undo_btn = gr.Button("↩️ Undo")
31
+ clear_btn = gr.Button("πŸ—‘οΈ Clear")
32
 
33
+ submit_btn.click(chat, inputs=[msg, chatbot], outputs=[chatbot, msg])
34
+ retry_btn.click(chat, inputs=[msg, chatbot], outputs=[chatbot, msg])
35
+ undo_btn.click(lambda history: history[:-1] if history else [], inputs=[chatbot], outputs=[chatbot])
36
+ clear_btn.click(lambda: [], inputs=[], outputs=[chatbot])
37
 
38
+ # 运葌 Gradio η•Œι’
39
+ demo.launch()