Spaces:
Sleeping
Sleeping
import streamlit as st | |
from transformers import AutoModelForCausalLM, AutoTokenizer | |
import torch | |
# Load DialoGPT model and tokenizer | |
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium") | |
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium") | |
# Streamlit app header | |
st.set_page_config(page_title="Conversational Model Demo", page_icon="🤖") | |
st.header("Conversational Model Demo") | |
# Initialize chat history | |
chat_history_ids = None | |
# Input for user message | |
user_message = st.text_input("You:", "") | |
if st.button("Send"): | |
# Encode the new user input, add the eos_token and return a tensor in PyTorch | |
new_user_input_ids = tokenizer.encode(user_message + tokenizer.eos_token, return_tensors='pt') | |
# Append the new user input tokens to the chat history | |
bot_input_ids = torch.cat([chat_history_ids, new_user_input_ids], dim=-1) if chat_history_ids is not None else new_user_input_ids | |
# Generate a response while limiting the total chat history to 1000 tokens | |
chat_history_ids = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id) | |
# Pretty print last output tokens from the bot | |
model_response = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True) | |
# Display the model's response | |
st.text_area("Model:", model_response, height=100) |