Spaces:
Runtime error
Runtime error
File size: 1,310 Bytes
8da0b8d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
# Load the model and tokenizer
model_name = "deepseek-ai/deepseek-llm-7b-chat"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
# Define chat function
def chat(message, history):
inputs = tokenizer.apply_chat_template(
history + [{"role": "user", "content": message}],
return_tensors="pt"
).to(model.device)
outputs = model.generate(inputs, max_new_tokens=256, do_sample=True)
reply = tokenizer.decode(outputs[0], skip_special_tokens=True).split("<|assistant|>")[-1]
history.append({"role": "user", "content": message})
history.append({"role": "assistant", "content": reply})
return reply, history
# UI with Gradio
with gr.Blocks() as demo:
chatbot = gr.Chatbot()
state = gr.State([])
with gr.Row():
msg = gr.Textbox(placeholder="Type your message here...", label="Your Message")
send = gr.Button("Send")
def respond(message, history):
reply, updated_history = chat(message, history)
return updated_history, updated_history
send.click(respond, [msg, state], [chatbot, state])
msg.submit(respond, [msg, state], [chatbot, state])
demo.launch()
|