Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
|
4 |
+
# Load model and tokenizer
|
5 |
+
model_name = "deepseek-ai/DeepSeek-R1"
|
6 |
+
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True)
|
9 |
+
|
10 |
+
# Chat function
|
11 |
+
def chatbot(user_message, history):
|
12 |
+
messages = history + [{"role": "user", "content": user_message}]
|
13 |
+
|
14 |
+
inputs = tokenizer.apply_chat_template(
|
15 |
+
messages,
|
16 |
+
add_generation_prompt=True,
|
17 |
+
tokenize=True,
|
18 |
+
return_tensors="pt",
|
19 |
+
return_dict=True
|
20 |
+
).to(model.device)
|
21 |
+
|
22 |
+
outputs = model.generate(**inputs, max_new_tokens=200)
|
23 |
+
response = tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:], skip_special_tokens=True)
|
24 |
+
|
25 |
+
history.append({"role": "user", "content": user_message})
|
26 |
+
history.append({"role": "assistant", "content": response})
|
27 |
+
|
28 |
+
return response, history
|
29 |
+
|
30 |
+
# Gradio UI
|
31 |
+
with gr.Blocks() as demo:
|
32 |
+
gr.Markdown("# 🤖 DeepSeek-R1 Chatbot")
|
33 |
+
|
34 |
+
chatbot_ui = gr.Chatbot()
|
35 |
+
msg = gr.Textbox(placeholder="Type your message here...")
|
36 |
+
clear = gr.Button("Clear")
|
37 |
+
|
38 |
+
state = gr.State([])
|
39 |
+
|
40 |
+
def user_input(message, history):
|
41 |
+
response, history = chatbot(message, history)
|
42 |
+
return history, history
|
43 |
+
|
44 |
+
msg.submit(user_input, [msg, state], [chatbot_ui, state])
|
45 |
+
clear.click(lambda: ([], []), None, [chatbot_ui, state])
|
46 |
+
|
47 |
+
demo.launch()
|