File size: 5,849 Bytes
40ae8d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
from openai import OpenAI
import decoder_output
import cut_text
import hotel_chatbot
import traversaal
import streamlit as st
from qdrant_client import QdrantClient
from neural_searcher import NeuralSearcher


def home_page():
    # st.title("TraverGo")

    st.markdown("<h1 style='text-align: center; color: white;'>TraverGo</h1>", unsafe_allow_html=True)
    st.markdown("<h2 style='text-align: center; color: white;'>Find any type of Hotel you want !</h2>", unsafe_allow_html=True)
    st.session_state["value"] = None

    def search_hotels():
        query = st.text_input("Enter your hotel preferences:", placeholder ="clean and cheap hotel with good food and gym")

        if "load_state" not in st.session_state:
            st.session_state.load_state = False;

        # Perform semantic search when user submits query
        if query or st.session_state.load_state:
            st.session_state.load_state=True;
            neural_searcher = NeuralSearcher(collection_name="hotel_descriptions")
            results = sorted(neural_searcher.search(query), key=lambda d: d['sentiment_rate_average'])
            st.subheader("Hotels")
            for hotel in results:
                explore_hotel(hotel, query)  # Call a separate function for each hotel

    def explore_hotel(hotel, query):
        if "decoder" not in st.session_state:
            st.session_state['decoder'] = [0];

        button = st.checkbox(hotel['hotel_name'])


        if not button:
            if st.session_state.decoder == [0]:
                x = (decoder_output.decode(hotel['hotel_description'][:1000], query))
                st.session_state['value_1'] = x
                st.session_state.decoder = [st.session_state.decoder[0] + 1]
                st.write(x)

            elif (st.session_state.decoder == [1]):
                x = (decoder_output.decode(hotel['hotel_description'][:1000], query))
                st.session_state['value_2'] = x

                st.session_state.decoder = [st.session_state.decoder[0] + 1];
                st.write(x);

            elif st.session_state.decoder == [2]:
                x = (decoder_output.decode(hotel['hotel_description'][:1000], query))
                st.session_state['value_3'] = x;
                st.session_state.decoder = [st.session_state.decoder[0] + 1];
                st.write(x);


            if (st.session_state.decoder[0] >= 3):
                i = st.session_state.decoder[0] % 3
                l = ['value_1', 'value_2', 'value_3']
                st.session_state[l[i - 1]];
                st.session_state.decoder = [st.session_state.decoder[0] + 1];

        if button:
            st.session_state["value"] = hotel


        # if (st.session_state.decoder[0] < 3):
        #     st.write(decoder_output.decode(hotel['hotel_description'][:1000], query))
        #     st.session_state.decoder = [st.session_state[0] + 1];
        #

        question = st.text_input(f"Enter a question about {hotel['hotel_name']}:");
            
        if question:
            st.write(ares_api(question + "for" + hotel['hotel_name'] + "located in" + hotel['country']))
        # if "load_state" not in st.session_state:
            # st.session_state.load_state = False;
        # Perform semantic search when user submits query
        # if question:






    search_hotels()
    chat_page()


def ares_api(query):
    response_json = traversaal.getResponse(query);
    # if response_json is not json:
    #     return "Could not find information"
    return (response_json['data']['response_text'])
def chat_page():
    hotel = st.session_state["value"]
    st.session_state.value = None
    if (hotel == None):
        return;

    st.write(hotel['hotel_name']);
    st.title("Conversation")

    # Set OpenAI API key from Streamlit secrets
    client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])

    # st.session_state.pop("messages")
    # Set a default model
    if "openai_model" not in st.session_state:
        st.session_state["openai_model"] = "gpt-3.5-turbo"

    prompt = f"{hotel['hotel_description'][:2000]}\n\n you are a hotel advisor now, you should give the best response based on the above text. i will now ask you some questions get ready"
    # Initialize chat history
    if "messages" not in st.session_state:
        st.session_state.messages = [{"role": "user", "content": prompt}]

    # Display chat messages from history on app rerun
    # keys_subset = list(st.session_state.messages.keys())[1:]
    # subset_dict = {key: original_dict[key] for key in keys_subset}



    for message in st.session_state.messages[1:]:
        with st.chat_message(message["role"]):
            st.markdown(message["content"])

    # Accept user input
    if prompt := st.chat_input("What is up?"):
        x = ares_api(prompt)
        # Add user message to chat history
        st.session_state.messages[0]['content'] += "\n" + x;
        st.session_state.messages.append({"role": "assistant", "content": prompt})
        # Display user message in chat message container
        with st.chat_message("user"):
            st.markdown(prompt)



    #Display assistant response in chat message container
    with st.chat_message("assistant"):
        stream = client.chat.completions.create(
            model=st.session_state["openai_model"],
            messages=[
                {"role": m["role"], "content": m["content"]}
                for m in st.session_state.messages
            ],
            stream=True,
        )
        response = st.write_stream(stream)
    st.session_state.messages.append({"role": "assistant", "content": response})


    # hotel_chatbot.start_page();

home_page()
#
#
# page = st.sidebar.selectbox("Select a page", ["Home", "Chatbot"])
#
#
# if page == "Home":
#     home_page()
# elif page == "Chatbot":
#     chat_page(None)
#