Spaces:
Runtime error
Runtime error
kokofixcomputers commited on
Commit Β·
e36063a
1
Parent(s): d88cbed
Fix errors
Browse files12345671 unmodified line9101112131415161718192021222324252 unmodified lines2829303132333435363738394041424344454647484950515253545556import gradio as grfrom transformers import AutoTokenizer, AutoModelForCausalLMimport torchimport markdownmodel_name = "deepseek-ai/deepseek-coder-1.3b-base"1 unmodified linemodel = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True)model.eval()def respond(message, chat_history, max_tokens, temperature, top_p):chat_history = chat_history or []chat_history.append(("User", message))full_prompt = ""for speaker, text in chat_history:prefix = "User: " if speaker == "User" else "Assistant: "full_prompt += prefix + text + "\n"full_prompt += "Assistant: "inputs = tokenizer(full_prompt, return_tensors="pt")outputs = model.generate(**inputs,max_new_tokens=max_tokens,2 unmodified linesdo_sample=True,pad_token_id=tokenizer.eos_token_id,)reply = tokenizer.decode(outputs[0], skip_special_tokens=True)[len(full_prompt):].strip()chat_history.append(("Assistant", reply))formatted_history = []for i in range(0, len(chat_history), 2):user_msg = chat_history[i][1] if i < len(chat_history) else ""bot_msg = chat_history[i+1][1] if i+1 < len(chat_history) else ""# Render assistant message as markdownformatted_history.append([user_msg, gr.Markdown(bot_msg)])return formatted_history, ""with gr.Blocks() as demo:gr.Markdown("# DeepSeek Coder Chatbot")chatbot = gr.Chatbot()with gr.Row():user_input = gr.Textbox(show_label=False, placeholder="Enter your prompt here and press Enter")with gr.Row():max_tokens = gr.Slider(1, 1024, value=512, step=1, label="Max Tokens")temperature = gr.Slider(0.1, 1.0, value=0.7, step=0.05, label="Temperature")top_p = gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p")def user_submit(text, history, max_tokens, temperature, top_p):if not text.strip():return history, ""1234561 unmodified line8910111213141516171819202122232425262 unmodified lines293031323334353637383940414243444546474849505152import gradio as grfrom transformers import AutoTokenizer, AutoModelForCausalLMimport torchmodel_name = "deepseek-ai/deepseek-coder-1.3b-base"1 unmodified linemodel = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True)model.eval()def respond(message, history, max_tokens, temperature, top_p):history = history or []# Append user message as dict with role and contenthistory.append({"role": "user", "content": message})# Create prompt by concatenating conversation history as textprompt = ""for msg in history:prefix = f"{msg['role'].capitalize()}: "prompt += prefix + msg["content"] + "\n"prompt += "Assistant: "inputs = tokenizer(prompt, return_tensors="pt")outputs = model.generate(**inputs,max_new_tokens=max_tokens,2 unmodified linesdo_sample=True,pad_token_id=tokenizer.eos_token_id,)reply = tokenizer.decode(outputs[0], skip_special_tokens=True)[len(prompt):].strip()# Append assistant responsehistory.append({"role": "assistant", "content": reply})return history, ""with gr.Blocks() as demo:gr.Markdown("# DeepSeek Coder Chatbot")chatbot = gr.Chatbot(type="messages")with gr.Row():user_input = gr.Textbox(show_label=False, placeholder="Enter your prompt and press Enter")with gr.Row():max_tokens = gr.Slider(1, 1024, value=512, step=1, label="Max Tokens")temperature = gr.Slider(0.1, 1.0, value=0.7, step=0.05, label="Temperature")top_p = gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p")def user_submit(text, history, max_tokens, temperature, top_p):if not text.strip():return history, ""
app.py+18-22