File size: 1,378 Bytes
8bbfca2
9d8e92e
 
8bbfca2
9d8e92e
 
 
8bbfca2
41fe4ff
 
 
8bbfca2
9d8e92e
 
 
41fe4ff
 
8bbfca2
41fe4ff
9d8e92e
 
 
 
 
 
 
 
a5a0bc7
9d8e92e
 
41fe4ff
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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)