File size: 3,326 Bytes
c411728
189a7a7
1ee8138
189a7a7
 
da6fad5
 
189a7a7
bde61e8
 
 
 
 
 
 
 
 
 
 
 
1a97f0c
 
 
 
 
4608d64
1a97f0c
da6fad5
 
 
 
 
 
f03a0c3
0b7c3d3
bde61e8
 
da6fad5
0b7c3d3
da6fad5
 
 
 
 
 
 
 
 
 
 
 
 
 
bde61e8
 
 
 
 
 
 
 
 
 
da6fad5
 
bde61e8
f03a0c3
bde61e8
 
f03a0c3
bde61e8
5f5cf3f
bde61e8
 
 
0b7c3d3
bde61e8
da6fad5
353b462
bde61e8
 
 
da6fad5
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os
import streamlit as st
import chatbot as demo_chat
from transformers import AutoModelForCausalLM, AutoTokenizer

st.title("Hi, I am Chatbot Philio :woman:")
st.write("I am your hotel booking assistant. Feel free to start chatting with me.")

scrollable_div_style = """
<style>
.scrollable-div {
    height: 200px;  /* Adjust the height as needed */
    overflow-y: auto;  /* Enable vertical scrolling */
    padding: 5px;
    border: 1px solid #ccc;  /* Optional: adds a border around the div */
    border-radius: 5px;  /* Optional: rounds the corners of the border */
}
</style>
"""

def render_chat_history(chat_history):
    #renders chat history
    for message in chat_history:
        if(message["role"]!= "system"):
            with st.chat_message(message["role"]):
                st.markdown(message["content"])

# def generate_response(chat_history):
#     tokenized_chat = tokenizer.apply_chat_template(chat_history, tokenize=True, add_generation_prompt=True, return_tensors="pt")
#     outputs = model.generate(tokenized_chat, do_sample =True, max_new_tokens=50, temperature = 0.3, top_p = 0.85)
#     answer = tokenizer.decode(outputs[0][tokenized_chat.shape[1]:],skip_special_tokens=True)
#     final_answer = answer.split("<")[0]
#     return final_answer

#Application 
#Langchain memory in session cache 
if 'memory' not in st.session_state:
    st.session_state.memory = demo_chat.demo_miny_memory()

system_content = """
You are an AI having conversation with a human. Below is an instruction that describes a task. 
Write a response that appropriately completes the request.
Reply with the most helpful and logic answer. During the conversation you need to ask the user 
the following questions to complete the hotel booking task.

1) Where would you like to stay and when?
2) How many people are staying in the room?
3) Do you prefer any ammenities like breakfast included or gym?
4) What is your name, your email address and phone number? 

Make sure you receive a logical answer from the user from every question to complete the hotel 
booking process.
"""
#Check if chat history exists in this session
if 'chat_history' not in st.session_state:
    st.session_state.chat_history = [
        {
            "role": "system",
            "content": system_content,
        },
        {"role": "assistant", "content": "Hello, how can I help you today?"},
    ] #Initialize chat history
    
# if 'model' not in st.session_state:
#     st.session_state.model = model
    
st.markdown('<div class="scrollable-div">', unsafe_allow_html=True) #add css style to container
render_chat_history(st.session_state.chat_history)

#Input field for chat interface
if input_text := st.chat_input(placeholder="Here you can chat with our hotel booking model."):
    
    with st.chat_message("user"):
        st.markdown(input_text)
    st.session_state.chat_history.append({"role" : "user", "content" : input_text}) #append message to chat history

    with st.spinner("Generating response..."):
        first_answer = demo_chat.demo_chain(input_text, st.session_state.memory)
        
        with st.chat_message("assistant"):
            st.markdown(first_answer)
        st.session_state.chat_history.append({"role": "assistant", "content": first_answer})    
st.markdown('</div>', unsafe_allow_html=True)