Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,6 +8,9 @@ import nltk
|
|
8 |
from openai.embeddings_utils import cosine_similarity
|
9 |
import spacy
|
10 |
from spacy.cli import download
|
|
|
|
|
|
|
11 |
nltk.download('punkt')
|
12 |
nltk.download('punkt_tab')
|
13 |
|
@@ -25,7 +28,7 @@ def bm25_rank(query, df, n=15):
|
|
25 |
return top_results
|
26 |
|
27 |
def search(query, df):
|
28 |
-
n =
|
29 |
query_embedding = model.encode(query)
|
30 |
df = bm25_rank(query, df)
|
31 |
df["similarity"] = df.embeddings.apply(lambda x: cosine_similarity(x, query_embedding.reshape(768,-1)))
|
@@ -50,7 +53,7 @@ def search(query, df):
|
|
50 |
"image_url": smalldf.hotel_image[r],
|
51 |
"score": smalldf.rate[r],
|
52 |
"description": smalldf.hotel_description[r]
|
53 |
-
|
54 |
})
|
55 |
hlist.append(results.hotel_name[r])
|
56 |
return resultlist
|
@@ -78,6 +81,7 @@ def get_hotel_info(query):
|
|
78 |
'hotel_name': result['hotel_name'],
|
79 |
'score': result['score'],
|
80 |
'description': result['description']
|
|
|
81 |
})
|
82 |
return response
|
83 |
|
@@ -88,30 +92,66 @@ def get_hotel_info(query):
|
|
88 |
# response += f"\n"
|
89 |
# return response
|
90 |
|
91 |
-
def
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
with gr.Blocks() as interface:
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
submit_button = gr.Button("Submit")
|
104 |
-
|
105 |
-
with gr.Column(scale=2):
|
106 |
-
image_output = gr.Image(label="Hotel Image")
|
107 |
-
name_output = gr.Textbox(label="Hotel Name")
|
108 |
-
score_output = gr.Textbox(label="Score")
|
109 |
-
description_output = gr.Textbox(label="Description")
|
110 |
|
111 |
submit_button.click(
|
112 |
-
fn=chatbot_response,
|
113 |
-
inputs=query_input,
|
114 |
-
outputs=[
|
115 |
)
|
116 |
|
117 |
interface.launch()
|
|
|
8 |
from openai.embeddings_utils import cosine_similarity
|
9 |
import spacy
|
10 |
from spacy.cli import download
|
11 |
+
import os
|
12 |
+
|
13 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
14 |
nltk.download('punkt')
|
15 |
nltk.download('punkt_tab')
|
16 |
|
|
|
28 |
return top_results
|
29 |
|
30 |
def search(query, df):
|
31 |
+
n = 5
|
32 |
query_embedding = model.encode(query)
|
33 |
df = bm25_rank(query, df)
|
34 |
df["similarity"] = df.embeddings.apply(lambda x: cosine_similarity(x, query_embedding.reshape(768,-1)))
|
|
|
53 |
"image_url": smalldf.hotel_image[r],
|
54 |
"score": smalldf.rate[r],
|
55 |
"description": smalldf.hotel_description[r]
|
56 |
+
"relevant_reviews": [ smalldf.review_text[s] for s in smalldf.index]
|
57 |
})
|
58 |
hlist.append(results.hotel_name[r])
|
59 |
return resultlist
|
|
|
81 |
'hotel_name': result['hotel_name'],
|
82 |
'score': result['score'],
|
83 |
'description': result['description']
|
84 |
+
'relevent_reviews': result['relevant_reviews']
|
85 |
})
|
86 |
return response
|
87 |
|
|
|
92 |
# response += f"\n"
|
93 |
# return response
|
94 |
|
95 |
+
def generate_answer(query, context):
|
96 |
+
prompt = f"""
|
97 |
+
Based on the following query from a user, please generate a detailed answer based on the context
|
98 |
+
focusing on which is the top hotel based on the query. You should respond as if you are a travel agent and are conversing with the
|
99 |
+
user in a nice cordial way. Remove any special characters and (\\n), make the output clean and concise.
|
100 |
+
|
101 |
+
###########
|
102 |
+
query:
|
103 |
+
"{query}"
|
104 |
+
|
105 |
+
########
|
106 |
+
|
107 |
+
context:"
|
108 |
+
"{context}"
|
109 |
+
#####
|
110 |
+
|
111 |
+
Return in Markdown format with each hotel highlighted.
|
112 |
+
"""
|
113 |
+
|
114 |
+
messages = [
|
115 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
116 |
+
{"role": "user", "content": prompt}
|
117 |
+
]
|
118 |
+
response = openai.ChatCompletion.create(
|
119 |
+
model="gpt-4o-mini",
|
120 |
+
max_tokens=1500,
|
121 |
+
n=1,
|
122 |
+
stop=None,
|
123 |
+
temperature=0.2, # Higher temperature means more creative or more hallucination
|
124 |
+
messages=messages
|
125 |
+
)
|
126 |
+
|
127 |
+
# Extract the generated response from the API response
|
128 |
+
generated_text = response.choices[0].message['content'].strip()
|
129 |
+
|
130 |
+
return generated_text
|
131 |
+
|
132 |
+
def chatbot_response(message, history):
|
133 |
+
hotel_infos = get_hotel_info(message)
|
134 |
+
if hotel_infos:
|
135 |
+
context = "\n".join([
|
136 |
+
f"Hotel Name: {info['hotel_name']}, Score: {info['score']}, Description: {info['description']}, Reviews: {info['relevant_reviews']}"
|
137 |
+
for info in hotel_infos
|
138 |
+
])
|
139 |
+
response = generate_answer(message, context)
|
140 |
+
else:
|
141 |
+
response = "No results found."
|
142 |
+
|
143 |
+
history.append((message, response))
|
144 |
+
return history, history
|
145 |
|
146 |
with gr.Blocks() as interface:
|
147 |
+
chatbot = gr.Chatbot(label="Hotel Search Chatbot")
|
148 |
+
query_input = gr.Textbox(label="Ask me about hotels!")
|
149 |
+
submit_button = gr.Button("Send")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
|
151 |
submit_button.click(
|
152 |
+
fn=chatbot_response,
|
153 |
+
inputs=[query_input, chatbot],
|
154 |
+
outputs=[chatbot, chatbot]
|
155 |
)
|
156 |
|
157 |
interface.launch()
|