|
import os |
|
import streamlit as st |
|
import chatbot as demo_chat |
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
from langchain.schema import ( |
|
HumanMessage, |
|
SystemMessage, |
|
) |
|
from langchain_community.chat_models.huggingface import ChatHuggingFace |
|
from transformers import pipeline |
|
|
|
st.title("Hi, I am Chatbot Philio :mermaid:") |
|
st.write("I am your hotel booking assistant for today.") |
|
|
|
|
|
|
|
tokenizer, model = demo_chat.load_model() |
|
|
|
model_identifier = "KvrParaskevi/Hotel-Assistant-Attempt4-Llama-2-7b" |
|
task = "text-generation" |
|
|
|
|
|
model_pipeline = pipeline(task, model=model,tokenizer=tokenizer) |
|
|
|
|
|
|
|
with st.container(): |
|
st.markdown('<div class="scrollable-div">', unsafe_allow_html=True) |
|
|
|
if 'memory' not in st.session_state: |
|
st.session_state.memory = demo_chat.demo_miny_memory(model) |
|
|
|
|
|
if 'chat_history' not in st.session_state: |
|
st.session_state.chat_history = [ |
|
{ |
|
"role": "system", |
|
"content": "You are a friendly chatbot who always helps the user book a hotel room based on his/her needs." |
|
+ "Based on the current social norms you wait for the user's response to your proposals.", |
|
}, |
|
{"role": "assistant", "content": "Hello, how can I help you today?"}, |
|
] |
|
|
|
if 'model' not in st.session_state: |
|
st.session_state.model = model |
|
|
|
|
|
for message in st.session_state.chat_history: |
|
if(message["role"]!= "system"): |
|
with st.chat_message(message["role"]): |
|
st.write(message["content"]) |
|
|
|
|
|
|
|
input_text = st.chat_input(placeholder="Here you can chat with our hotel booking model.") |
|
|
|
if input_text: |
|
with st.chat_message("user"): |
|
|
|
st.session_state.chat_history.append({"role" : "user", "content" : input_text}) |
|
|
|
|
|
|
|
tokenized_chat = tokenizer.apply_chat_template(st.session_state.chat_history, tokenize=True, add_generation_prompt=True, return_tensors="pt") |
|
|
|
outputs = model.generate(tokenized_chat, max_new_tokens=128) |
|
first_answer = tokenizer.decode(outputs[0][tokenized_chat.shape[1]:],skip_special_tokens=True) |
|
|
|
with st.chat_message("assistant"): |
|
|
|
st.session_state.chat_history.append({"role": "assistant", "content": first_answer}) |
|
st.markdown('</div>', unsafe_allow_html=True) |