Code_generation / app.py
letao670982's picture
Update app.py
9e8f70b verified
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer
from threading import Thread
tokenizer = AutoTokenizer.from_pretrained("deepseek-ai/deepseek-coder-1.3b-instruct", trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained("deepseek-ai/deepseek-coder-1.3b-instruct", trust_remote_code=True, device_map="auto")
streamer = TextStreamer(tokenizer, skip_prompt=True)
#streaming output
def respond(message, history):
messages=[
{ 'role': 'user', 'content': message}]
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
# 32021 is the id of <|EOT|> token
outputs = model.generate(inputs, streamer=streamer, max_new_tokens=1024, do_sample=False, top_k=50, top_p=0.95, num_return_sequences=1, eos_token_id=32021)
out_answer = tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True)
generated_text = ""
for new_text in out_answer:
generated_text += new_text
yield generated_text
demo_chatbot = gr.ChatInterface(respond, title="Deepseek-Coder", description="Enter text to start chatting.")
demo_chatbot.launch()
#https://huggingface.co/docs/transformers/internal/generation_utils