Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,9 +21,20 @@ def chat(user_input):
|
|
| 21 |
prompt += f"{speaker}: {msg}\n"
|
| 22 |
prompt += f"User: {user_input}\nBot:"
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
with torch.no_grad():
|
| 26 |
-
output = model.generate(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
full_reply = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 29 |
reply = full_reply.split("Bot:")[-1].strip()
|
|
@@ -48,6 +59,7 @@ with gr.Blocks() as demo:
|
|
| 48 |
gr.Markdown("""# 🤖 Chat with Phi-2
|
| 49 |
Small CPU-friendly chatbot using Hugging Face Transformers
|
| 50 |
""")
|
|
|
|
| 51 |
chatbot = gr.Markdown()
|
| 52 |
with gr.Row():
|
| 53 |
user_input = gr.Textbox(placeholder="Type your message here...", show_label=False)
|
|
@@ -62,6 +74,7 @@ Small CPU-friendly chatbot using Hugging Face Transformers
|
|
| 62 |
clear_btn.click(clear, outputs=chatbot)
|
| 63 |
export_btn.click(export_chat, outputs=file_out)
|
| 64 |
|
|
|
|
| 65 |
chatbot.value = format_chat(chat_history)
|
| 66 |
|
| 67 |
-
demo.launch()
|
|
|
|
| 21 |
prompt += f"{speaker}: {msg}\n"
|
| 22 |
prompt += f"User: {user_input}\nBot:"
|
| 23 |
|
| 24 |
+
# Tokenize input with attention mask
|
| 25 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 26 |
+
input_ids = inputs.input_ids
|
| 27 |
+
attention_mask = inputs.attention_mask
|
| 28 |
+
|
| 29 |
with torch.no_grad():
|
| 30 |
+
output = model.generate(
|
| 31 |
+
input_ids,
|
| 32 |
+
attention_mask=attention_mask,
|
| 33 |
+
max_new_tokens=100,
|
| 34 |
+
do_sample=True,
|
| 35 |
+
temperature=0.7,
|
| 36 |
+
pad_token_id=tokenizer.eos_token_id
|
| 37 |
+
)
|
| 38 |
|
| 39 |
full_reply = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 40 |
reply = full_reply.split("Bot:")[-1].strip()
|
|
|
|
| 59 |
gr.Markdown("""# 🤖 Chat with Phi-2
|
| 60 |
Small CPU-friendly chatbot using Hugging Face Transformers
|
| 61 |
""")
|
| 62 |
+
|
| 63 |
chatbot = gr.Markdown()
|
| 64 |
with gr.Row():
|
| 65 |
user_input = gr.Textbox(placeholder="Type your message here...", show_label=False)
|
|
|
|
| 74 |
clear_btn.click(clear, outputs=chatbot)
|
| 75 |
export_btn.click(export_chat, outputs=file_out)
|
| 76 |
|
| 77 |
+
# Initialize chatbot display
|
| 78 |
chatbot.value = format_chat(chat_history)
|
| 79 |
|
| 80 |
+
demo.launch(share=True)
|