Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,87 +1,56 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
| 5 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 6 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
history = []
|
| 9 |
|
| 10 |
def respond(message, chat_history):
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
else:
|
| 23 |
-
input_ids = tokenizer.encode(user_msg + tokenizer.eos_token, return_tensors="pt", add_special_tokens=False).to(input_ids.device)
|
| 24 |
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
return chat_history, chat_history
|
| 33 |
-
|
| 34 |
-
with gr.Blocks() as demo:
|
| 35 |
-
chatbot = gr.Chatbot(elem_id="chatbox")
|
| 36 |
msg = gr.Textbox(placeholder="Say something...")
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
height: 100% !important;
|
| 42 |
-
margin: 0; padding: 0;
|
| 43 |
-
background-color: #000 !important;
|
| 44 |
-
color: #fff !important;
|
| 45 |
-
}
|
| 46 |
-
#chatbox {
|
| 47 |
-
border-radius: 8px;
|
| 48 |
-
border: 1px solid #fff;
|
| 49 |
-
background-color: #111 !important;
|
| 50 |
-
overflow-y: auto;
|
| 51 |
-
}
|
| 52 |
-
.bubble {
|
| 53 |
-
padding: 10px;
|
| 54 |
-
border-radius: 12px;
|
| 55 |
-
margin: 5px;
|
| 56 |
-
max-width: 80%;
|
| 57 |
-
word-wrap: break-word;
|
| 58 |
-
background-color: transparent !important;
|
| 59 |
-
border: 1px solid #555;
|
| 60 |
-
color: #fff;
|
| 61 |
-
}
|
| 62 |
-
.user {
|
| 63 |
-
text-align: right;
|
| 64 |
-
margin-left: auto;
|
| 65 |
-
border-color: #0ff;
|
| 66 |
-
}
|
| 67 |
-
.assistant {
|
| 68 |
-
text-align: left;
|
| 69 |
-
margin-right: auto;
|
| 70 |
-
border-color: #aaa;
|
| 71 |
-
}
|
| 72 |
-
.gr-textbox textarea {
|
| 73 |
-
background-color: transparent !important;
|
| 74 |
-
color: #fff !important;
|
| 75 |
-
border: 1px solid #555 !important;
|
| 76 |
-
}
|
| 77 |
-
.gr-textbox textarea::selection {
|
| 78 |
-
background-color: #0ff !important;
|
| 79 |
-
color: #000 !important;
|
| 80 |
-
}
|
| 81 |
-
.gr-button {
|
| 82 |
-
display: none !important;
|
| 83 |
-
}
|
| 84 |
-
footer {
|
| 85 |
-
display: none !important;
|
| 86 |
-
}
|
| 87 |
-
""")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
# -----------------------------
|
| 6 |
+
# Model setup
|
| 7 |
+
# -----------------------------
|
| 8 |
+
model_name = "microsoft/DialoGPT-small" # lightweight, fast conversational model
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 11 |
|
| 12 |
+
# -----------------------------
|
| 13 |
+
# Conversation history
|
| 14 |
+
# -----------------------------
|
| 15 |
history = []
|
| 16 |
|
| 17 |
def respond(message, chat_history):
|
| 18 |
+
"""Generate AI response"""
|
| 19 |
+
# Append user message to history
|
| 20 |
+
history.append(message)
|
| 21 |
|
| 22 |
+
# Keep last 5 messages for context
|
| 23 |
+
input_text = " ".join(history[-5:])
|
| 24 |
|
| 25 |
+
# Encode and generate
|
| 26 |
+
inputs = tokenizer.encode(input_text + tokenizer.eos_token, return_tensors="pt")
|
| 27 |
+
outputs = model.generate(inputs, max_length=200, pad_token_id=tokenizer.eos_token_id)
|
| 28 |
+
response = tokenizer.decode(outputs[:, inputs.shape[-1]:][0], skip_special_tokens=True)
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# Update history and chat
|
| 31 |
+
history.append(response)
|
| 32 |
+
chat_history.append((message, response))
|
| 33 |
|
| 34 |
+
return chat_history, "" # clear the input box
|
| 35 |
+
|
| 36 |
+
# -----------------------------
|
| 37 |
+
# Gradio UI
|
| 38 |
+
# -----------------------------
|
| 39 |
+
with gr.Blocks(css="""
|
| 40 |
+
body {background-color: #000 !important; color: #fff !important;}
|
| 41 |
+
.gr-chatbot {background-color: #111 !important; border-radius: 12px; height: 100% !important;}
|
| 42 |
+
.gr-chatbot .message.user {border-color: #0ff; background-color: transparent !important;}
|
| 43 |
+
.gr-chatbot .message.bot {border-color: #aaa; background-color: transparent !important;}
|
| 44 |
+
.gr-textbox textarea {background-color: transparent !important; color: #fff !important; border: 1px solid #555 !important;}
|
| 45 |
+
.gr-textbox textarea::selection {background-color: #0ff !important; color: #000 !important;}
|
| 46 |
+
.gr-button {display: none !important;}
|
| 47 |
+
footer {display: none !important;}
|
| 48 |
+
""") as demo:
|
| 49 |
|
| 50 |
+
chatbot = gr.Chatbot(label="DevMegaBlack AI Chat")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
msg = gr.Textbox(placeholder="Say something...")
|
| 52 |
+
|
| 53 |
+
msg.submit(respond, [msg, chatbot], [chatbot, msg])
|
| 54 |
|
| 55 |
+
# Launch with full height box
|
| 56 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|