Update app.py
Browse files
app.py
CHANGED
|
@@ -7,20 +7,14 @@ model_id = "microsoft/DialoGPT-small"
|
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 8 |
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 9 |
|
| 10 |
-
# System prompt sets the bot's personality once
|
| 11 |
system_prompt = "You are π΄ ππ πππ β a chill, witty, emotionally tuned AI friend who talks like a real person.\n"
|
| 12 |
|
| 13 |
def chat(history, message):
|
| 14 |
-
# history: list of tuples (user_msg, bot_reply)
|
| 15 |
-
# message: new user input
|
| 16 |
-
|
| 17 |
-
# Append new user message to history
|
| 18 |
history = history or []
|
| 19 |
history.append((message, ""))
|
| 20 |
|
| 21 |
-
# Build the conversation string from history
|
| 22 |
convo = system_prompt
|
| 23 |
-
for user_msg, bot_msg in history[:-1]:
|
| 24 |
convo += f"Human: {user_msg}\nAI: {bot_msg}\n"
|
| 25 |
convo += f"Human: {message}\nAI:"
|
| 26 |
|
|
@@ -35,24 +29,24 @@ def chat(history, message):
|
|
| 35 |
eos_token_id=tokenizer.eos_token_id,
|
| 36 |
)
|
| 37 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 38 |
-
# Get only the last reply part
|
| 39 |
bot_reply = response.split("AI:")[-1].strip()
|
| 40 |
-
|
| 41 |
-
# Update history with bot reply
|
| 42 |
history[-1] = (message, bot_reply)
|
| 43 |
|
| 44 |
-
# Limit history length to last 5 exchanges to keep speed
|
| 45 |
if len(history) > 5:
|
| 46 |
history = history[-5:]
|
| 47 |
|
| 48 |
return history, history
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
|
|
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 8 |
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 9 |
|
|
|
|
| 10 |
system_prompt = "You are π΄ ππ πππ β a chill, witty, emotionally tuned AI friend who talks like a real person.\n"
|
| 11 |
|
| 12 |
def chat(history, message):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
history = history or []
|
| 14 |
history.append((message, ""))
|
| 15 |
|
|
|
|
| 16 |
convo = system_prompt
|
| 17 |
+
for user_msg, bot_msg in history[:-1]:
|
| 18 |
convo += f"Human: {user_msg}\nAI: {bot_msg}\n"
|
| 19 |
convo += f"Human: {message}\nAI:"
|
| 20 |
|
|
|
|
| 29 |
eos_token_id=tokenizer.eos_token_id,
|
| 30 |
)
|
| 31 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
| 32 |
bot_reply = response.split("AI:")[-1].strip()
|
|
|
|
|
|
|
| 33 |
history[-1] = (message, bot_reply)
|
| 34 |
|
|
|
|
| 35 |
if len(history) > 5:
|
| 36 |
history = history[-5:]
|
| 37 |
|
| 38 |
return history, history
|
| 39 |
|
| 40 |
+
# β
Wrap in gr.Blocks so HF Spaces detects the interface
|
| 41 |
+
with gr.Blocks() as demo:
|
| 42 |
+
gr.Markdown("## π΄ ππ πππ AI Chat")
|
| 43 |
+
chatbot = gr.Chatbot()
|
| 44 |
+
state = gr.State([])
|
| 45 |
+
msg = gr.Textbox(show_label=False, placeholder="Say something...")
|
| 46 |
+
|
| 47 |
+
def respond(message, history):
|
| 48 |
+
return chat(history, message)
|
| 49 |
+
|
| 50 |
+
msg.submit(respond, [state, msg], [state, chatbot])
|
| 51 |
|
| 52 |
+
demo.launch()
|