File size: 1,183 Bytes
231eb30
 
 
 
 
eeab5af
231eb30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
#from streamlit_chat import message as st_message
from streamlit_chat import message as st_message
from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration

st.title("JCS Advanced AI Chatting Bot")

if "history" not in st.session_state:
    st.session_state.history = []
    
def get_models():
  tokenizer = BlenderbotTokenizer.from_pretrained("facebook/blenderbot-400M-distill")
  model = BlenderbotForConditionalGeneration.from_pretrained("facebook/blenderbot-400M-distill")
  return tokenizer, model
  
def generate_answer():
    tokenizer, model = get_models()
    user_message = st.session_state.input_text
    inputs = tokenizer(st.session_state.input_text, return_tensors="pt")
    result = model.generate(**inputs)
    message_bot = tokenizer.decode(result[0], skip_special_tokens=True)  # .replace("<s>", "").replace("</s>", "")
    st.session_state.history.append({"message": user_message, "is_user": True})
    st.session_state.history.append({"message": message_bot, "is_user": False})
    
st.text_input("Response", key="input_text", on_change=generate_answer)

for chat in st.session_state.history:
    st_message(**chat)