API / app.py
Trigger82's picture
Update app.py
dd12284 verified
raw
history blame
1.84 kB
from transformers import AutoModelForCausalLM, AutoTokenizer
import gradio as gr
import torch
model_id = "microsoft/DialoGPT-small"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id)
# System prompt sets the bot's personality once
system_prompt = "You are 𝕴 𝖆𝖒 π–π–Žπ–’ β€” a chill, witty, emotionally tuned AI friend who talks like a real person.\n"
def chat(history, message):
# history: list of tuples (user_msg, bot_reply)
# message: new user input
# Append new user message to history
history = history or []
history.append((message, ""))
# Build the conversation string from history
convo = system_prompt
for user_msg, bot_msg in history[:-1]: # all previous turns
convo += f"Human: {user_msg}\nAI: {bot_msg}\n"
convo += f"Human: {message}\nAI:"
inputs = tokenizer.encode(convo, return_tensors="pt")
outputs = model.generate(
inputs,
max_new_tokens=50,
pad_token_id=tokenizer.eos_token_id,
do_sample=True,
temperature=0.7,
top_p=0.9,
eos_token_id=tokenizer.eos_token_id,
)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
# Get only the last reply part
bot_reply = response.split("AI:")[-1].strip()
# Update history with bot reply
history[-1] = (message, bot_reply)
# Limit history length to last 5 exchanges to keep speed
if len(history) > 5:
history = history[-5:]
return history, history
iface = gr.Interface(
fn=chat,
inputs=[gr.State(), gr.Textbox(show_label=False, placeholder="Say something...")],
outputs=[gr.State(), gr.Chatbot(label="𝕴 𝖆𝖒 π–π–Žπ–’ AI Chat")],
title="𝕴 𝖆𝖒 π–π–Žπ–’ Chatbot",
allow_flagging="never",
)
iface.launch()