File size: 2,022 Bytes
189a7a7
 
 
 
 
 
 
 
 
 
 
 
0b7c3d3
1b9d85c
0b7c3d3
3daead0
 
0b7c3d3
 
 
 
 
 
 
 
 
 
 
eff3674
374d419
eff3674
 
0b7c3d3
 
 
 
 
 
 
 
 
 
 
 
 
3daead0
0b7c3d3
 
 
 
 
 
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import streamlit as st
import chatbot_bedrock as demo_chat
from transformers import AutoModelForCausalLM, AutoTokenizer

st.title("Hi, I am Chatbot Philio :mermaid:")
st.write("I am your hotel booking assistant for today.")

# tokenizer = AutoTokenizer.from_pretrained("KvrParaskevi/Hotel-Assistant-Attempt4-Llama-2-7b")

# [theme]
# base="light"
# primaryColor="#6b4bff"

tokenizer, model = demo_chat.load_model()

model_wrapper = demo_chat.HuggingFaceModelWrapper(model, tokenizer)

#Application 
with st.container():
    st.markdown('<div class="scrollable-div">', unsafe_allow_html=True)
    #Langchain memory in session cache 
    if 'memory' not in st.session_state:
        st.session_state.memory = demo_chat.demo_miny_memory(model)

    #Check if chat history exists in this session
    if 'chat_history' not in st.session_state:
        st.session_state.chat_history = [ ] #Initialize chat history

    if 'model' not in st.session_state:
        st.write("Model added in state.")
        st.session_state.model = model

    #renders chat history
    for message in st.session_state.chat_history: 
        with st.chat_message(message["role"]):
            st.write(message["content"])

    #Set up input text field
    input_text = st.chat_input(placeholder="Here you can chat with Llamma 2 model.")

    if input_text:
        with st.chat_message("user"):
            st.write(input_text)
            st.session_state.chat_history.append({"role" : "user", "content" : input_text}) #append message to chat history

        chat_response = demo_chat.demo_chain(input_text=input_text, memory=st.session_state.memory, model= model_wrapper)
        first_answer = chat_response.split("Human")[0] #Because of Predict it prints the whole conversation.Here we seperate the first answer only.

        with st.chat_message("assistant"):
            st.write(first_answer)
            st.session_state.chat_history.append({"role": "assistant", "content": first_answer})
    st.markdown('</div>', unsafe_allow_html=True)