Spaces:
Runtime error
Runtime error
kokofixcomputers
commited on
Commit
Β·
8096cee
1
Parent(s):
148e15a
Update Space
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
|
|
|
| 4 |
|
| 5 |
model_name = "deepseek-ai/deepseek-coder-1.3b-base"
|
| 6 |
|
|
@@ -8,8 +9,17 @@ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
|
| 8 |
model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True)
|
| 9 |
model.eval()
|
| 10 |
|
| 11 |
-
def respond(
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
outputs = model.generate(
|
| 14 |
**inputs,
|
| 15 |
max_new_tokens=max_tokens,
|
|
@@ -18,17 +28,35 @@ def respond(prompt, max_tokens, temperature, top_p):
|
|
| 18 |
do_sample=True,
|
| 19 |
pad_token_id=tokenizer.eos_token_id,
|
| 20 |
)
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
with gr.Blocks() as demo:
|
| 24 |
-
gr.Markdown("# DeepSeek Coder
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
if __name__ == "__main__":
|
| 34 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
+
import markdown
|
| 5 |
|
| 6 |
model_name = "deepseek-ai/deepseek-coder-1.3b-base"
|
| 7 |
|
|
|
|
| 9 |
model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True)
|
| 10 |
model.eval()
|
| 11 |
|
| 12 |
+
def respond(message, chat_history, max_tokens, temperature, top_p):
|
| 13 |
+
chat_history = chat_history or []
|
| 14 |
+
chat_history.append(("User", message))
|
| 15 |
+
|
| 16 |
+
full_prompt = ""
|
| 17 |
+
for speaker, text in chat_history:
|
| 18 |
+
prefix = "User: " if speaker == "User" else "Assistant: "
|
| 19 |
+
full_prompt += prefix + text + "\n"
|
| 20 |
+
full_prompt += "Assistant: "
|
| 21 |
+
|
| 22 |
+
inputs = tokenizer(full_prompt, return_tensors="pt")
|
| 23 |
outputs = model.generate(
|
| 24 |
**inputs,
|
| 25 |
max_new_tokens=max_tokens,
|
|
|
|
| 28 |
do_sample=True,
|
| 29 |
pad_token_id=tokenizer.eos_token_id,
|
| 30 |
)
|
| 31 |
+
reply = tokenizer.decode(outputs[0], skip_special_tokens=True)[len(full_prompt):].strip()
|
| 32 |
+
chat_history.append(("Assistant", reply))
|
| 33 |
+
|
| 34 |
+
formatted_history = []
|
| 35 |
+
for i in range(0, len(chat_history), 2):
|
| 36 |
+
user_msg = chat_history[i][1] if i < len(chat_history) else ""
|
| 37 |
+
bot_msg = chat_history[i+1][1] if i+1 < len(chat_history) else ""
|
| 38 |
+
# Render assistant message as markdown
|
| 39 |
+
formatted_history.append([user_msg, gr.Markdown(bot_msg)])
|
| 40 |
+
|
| 41 |
+
return formatted_history, ""
|
| 42 |
|
| 43 |
with gr.Blocks() as demo:
|
| 44 |
+
gr.Markdown("# DeepSeek Coder Chatbot")
|
| 45 |
+
|
| 46 |
+
chatbot = gr.Chatbot(markdown=True)
|
| 47 |
+
with gr.Row():
|
| 48 |
+
user_input = gr.Textbox(show_label=False, placeholder="Enter your prompt here and press Enter")
|
| 49 |
+
with gr.Row():
|
| 50 |
+
max_tokens = gr.Slider(1, 1024, value=512, step=1, label="Max Tokens")
|
| 51 |
+
temperature = gr.Slider(0.1, 1.0, value=0.7, step=0.05, label="Temperature")
|
| 52 |
+
top_p = gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p")
|
| 53 |
+
|
| 54 |
+
def user_submit(text, history, max_tokens, temperature, top_p):
|
| 55 |
+
if not text.strip():
|
| 56 |
+
return history, ""
|
| 57 |
+
return respond(text, history, max_tokens, temperature, top_p)
|
| 58 |
+
|
| 59 |
+
user_input.submit(user_submit, inputs=[user_input, chatbot, max_tokens, temperature, top_p], outputs=[chatbot, user_input])
|
| 60 |
|
| 61 |
if __name__ == "__main__":
|
| 62 |
demo.launch()
|