Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,51 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 4 |
+
|
| 5 |
+
model_name = "facebook/blenderbot-400M-distill"
|
| 6 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name).to(device)
|
| 9 |
+
|
| 10 |
+
system_preamble = (
|
| 11 |
+
"You are a friendly, energetic motivational coach. "
|
| 12 |
+
"Keep answers concise, positive, and actionable. "
|
| 13 |
+
"When the user asks for exercises or steps, provide a short numbered list. "
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
def generate_response(history, user_message):
|
| 17 |
+
conversation = ""
|
| 18 |
+
for u, r in history:
|
| 19 |
+
conversation += "User: " + u + "\nCoach: " + r + "\n"
|
| 20 |
+
conversation += "User: " + user_message + "\nCoach:"
|
| 21 |
+
prompt = system_preamble + "\n" + conversation
|
| 22 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1024).to(device)
|
| 23 |
+
out = model.generate(**inputs, max_new_tokens=256, do_sample=True, top_p=0.9, temperature=0.8)
|
| 24 |
+
reply = tokenizer.decode(out[0], skip_special_tokens=True).strip()
|
| 25 |
+
return reply
|
| 26 |
+
|
| 27 |
+
def chat(user_message, chat_history):
|
| 28 |
+
if chat_history is None:
|
| 29 |
+
chat_history = []
|
| 30 |
+
reply = generate_response(chat_history, user_message)
|
| 31 |
+
chat_history.append((user_message, reply))
|
| 32 |
+
return chat_history, chat_history
|
| 33 |
+
|
| 34 |
+
with gr.Blocks(title="Motivational Coach") as demo:
|
| 35 |
+
gr.Markdown("<h2 style='text-align:center'>Motivational Coach — powered by BlenderBot</h2>")
|
| 36 |
+
with gr.Row():
|
| 37 |
+
chatbot = gr.Chatbot(elem_id="chatbot", label="Coach")
|
| 38 |
+
with gr.Column(scale=0.3):
|
| 39 |
+
clear_btn = gr.Button("Clear")
|
| 40 |
+
msg = gr.Textbox(placeholder="Write your message here...", show_label=False)
|
| 41 |
+
state = gr.State([])
|
| 42 |
+
|
| 43 |
+
def submit_message(message, state):
|
| 44 |
+
new_history, state_out = chat(message, state)
|
| 45 |
+
return "", new_history, state_out
|
| 46 |
+
|
| 47 |
+
msg.submit(submit_message, inputs=[msg, state], outputs=[msg, chatbot, state])
|
| 48 |
+
clear_btn.click(lambda: ([], []), None, [chatbot, state])
|
| 49 |
+
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|