Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,50 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
max_length=100,
|
| 9 |
-
truncation=True # important
|
| 10 |
-
)
|
| 11 |
|
| 12 |
-
def
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
# Load the DeepSeek R1 model and tokenizer
|
| 6 |
+
model_name = "deepseek-ai/deepseek-r1"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
def chat_with_deepseek(user_input, history):
|
| 11 |
+
# Combine the history with the new user input
|
| 12 |
+
full_input = "\n".join(history + [user_input])
|
| 13 |
+
|
| 14 |
+
# Tokenize the input
|
| 15 |
+
inputs = tokenizer(full_input, return_tensors="pt")
|
| 16 |
+
|
| 17 |
+
# Generate a response
|
| 18 |
+
outputs = model.generate(inputs.input_ids, max_length=150, num_return_sequences=1)
|
| 19 |
+
|
| 20 |
+
# Decode the response
|
| 21 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 22 |
+
|
| 23 |
+
# Update the history with the new interaction
|
| 24 |
+
history.append(user_input)
|
| 25 |
+
history.append(response)
|
| 26 |
+
|
| 27 |
+
return response, history
|
| 28 |
|
| 29 |
+
# Create the Gradio interface
|
| 30 |
+
with gr.Blocks() as demo:
|
| 31 |
+
chatbot = gr.Chatbot()
|
| 32 |
+
msg = gr.Textbox()
|
| 33 |
+
clear = gr.Button("Clear")
|
| 34 |
+
|
| 35 |
+
def user(user_message, history):
|
| 36 |
+
return "", history + [[user_message, None]]
|
| 37 |
+
|
| 38 |
+
def bot(history):
|
| 39 |
+
user_message = history[-1][0]
|
| 40 |
+
response, _ = chat_with_deepseek(user_message, [h[0] for h in history])
|
| 41 |
+
history[-1][1] = response
|
| 42 |
+
return history
|
| 43 |
+
|
| 44 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 45 |
+
bot, chatbot, chatbot
|
| 46 |
+
)
|
| 47 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 48 |
|
| 49 |
+
# Launch the Gradio app
|
| 50 |
+
demo.launch()
|