Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,101 +1,45 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
| 3 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
-
MODEL_NAME,
|
| 11 |
-
torch_dtype=torch.float32,
|
| 12 |
-
device_map="cpu"
|
| 13 |
-
)
|
| 14 |
-
model.eval()
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
Do NOT assume intent or emotions.
|
| 19 |
-
If the message is unclear, ask for clarification.
|
| 20 |
-
Keep responses natural and concise.
|
| 21 |
-
"""
|
| 22 |
-
|
| 23 |
-
# ------------------ CHAT FUNCTIONS ------------------
|
| 24 |
-
|
| 25 |
-
def build_prompt(history):
|
| 26 |
-
prompt = SYSTEM_PROMPT + "\n\n"
|
| 27 |
-
for msg in history:
|
| 28 |
-
role = "User" if msg["role"] == "user" else "Assistant"
|
| 29 |
-
prompt += f"{role}: {msg['content']}\n"
|
| 30 |
-
prompt += "Assistant:"
|
| 31 |
-
return prompt
|
| 32 |
-
|
| 33 |
-
def respond(message, history):
|
| 34 |
-
# Add user message
|
| 35 |
history.append({"role": "user", "content": message})
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
inputs = tokenizer(
|
| 45 |
-
prompt,
|
| 46 |
-
return_tensors="pt",
|
| 47 |
-
truncation=True,
|
| 48 |
-
max_length=2048
|
| 49 |
-
)
|
| 50 |
-
|
| 51 |
-
# Generate response
|
| 52 |
-
with torch.no_grad():
|
| 53 |
-
output = model.generate(
|
| 54 |
-
**inputs,
|
| 55 |
-
max_new_tokens=120,
|
| 56 |
-
do_sample=False,
|
| 57 |
-
temperature=0.0,
|
| 58 |
-
top_p=1.0,
|
| 59 |
-
repetition_penalty=1.1
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
text = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 63 |
-
# Extract model response
|
| 64 |
-
response = text.split("Assistant:")[-1].strip()
|
| 65 |
-
|
| 66 |
-
# Replace loading with actual response
|
| 67 |
-
history[-1]["content"] = response
|
| 68 |
-
|
| 69 |
-
# Trim history if too long
|
| 70 |
-
if len(history) > 10:
|
| 71 |
-
history = history[-8:]
|
| 72 |
-
|
| 73 |
-
yield history, history
|
| 74 |
|
| 75 |
def reset_chat():
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
body {
|
| 82 |
-
.gr-chatbot {
|
| 83 |
-
.gr-chatbot .message.user {
|
| 84 |
-
.gr-chatbot .message.bot {
|
| 85 |
-
.gr-textbox textarea {
|
| 86 |
-
.gr-textbox textarea::selection {
|
| 87 |
-
.gr-button {
|
| 88 |
-
footer {
|
| 89 |
-
"""
|
| 90 |
|
| 91 |
-
|
| 92 |
chatbot = gr.Chatbot(label="DevMegaBlack")
|
| 93 |
msg = gr.Textbox(placeholder="Say something...")
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
msg.submit(respond, [msg, state], [chatbot, state]).then(lambda: "", None, msg)
|
| 98 |
-
reset.click(reset_chat, [], [chatbot, state])
|
| 99 |
|
| 100 |
-
demo.queue()
|
| 101 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 3 |
import torch
|
|
|
|
| 4 |
|
| 5 |
+
model_name = "facebook/blenderbot-400M-distill"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 8 |
|
| 9 |
+
history = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
def respond(message):
|
| 12 |
+
global history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
history.append({"role": "user", "content": message})
|
| 14 |
+
last_msgs = history[-3:]
|
| 15 |
+
input_text = " ".join([m["content"] for m in last_msgs])
|
| 16 |
+
inputs = tokenizer(input_text, return_tensors="pt")
|
| 17 |
+
outputs = model.generate(**inputs, max_new_tokens=100)
|
| 18 |
+
response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 19 |
+
history.append({"role": "assistant", "content": response_text})
|
| 20 |
+
return history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def reset_chat():
|
| 23 |
+
global history
|
| 24 |
+
history = []
|
| 25 |
+
return history
|
| 26 |
+
|
| 27 |
+
css = """
|
| 28 |
+
body {background-color: #000 !important; color: #fff !important;}
|
| 29 |
+
.gr-chatbot {background-color: #111 !important; border-radius: 12px; height: 100% !important;}
|
| 30 |
+
.gr-chatbot .message.user {border-color: #0ff; background-color: transparent !important;}
|
| 31 |
+
.gr-chatbot .message.bot {border-color: #aaa; background-color: transparent !important;}
|
| 32 |
+
.gr-textbox textarea {background-color: transparent !important; color: #fff !important; border: 1px solid #555 !important;}
|
| 33 |
+
.gr-textbox textarea::selection {background-color: #0ff !important; color: #000 !important;}
|
| 34 |
+
.gr-button {background-color: #0ff !important; color: #000 !important; border-radius: 8px;}
|
| 35 |
+
footer {display: none !important;}
|
| 36 |
+
"""
|
| 37 |
|
| 38 |
+
with gr.Blocks(css=css) as demo:
|
| 39 |
chatbot = gr.Chatbot(label="DevMegaBlack")
|
| 40 |
msg = gr.Textbox(placeholder="Say something...")
|
| 41 |
+
reset_btn = gr.Button("Reset Chat")
|
| 42 |
+
msg.submit(respond, msg, chatbot)
|
| 43 |
+
reset_btn.click(reset_chat, [], chatbot)
|
|
|
|
|
|
|
| 44 |
|
|
|
|
| 45 |
demo.launch()
|