ruslanmv's picture
update
43ba7b5
raw
history blame
2.88 kB
import gradio as gr
from gradio_multimodalchatbot import MultimodalChatbot
from gradio.data_classes import FileData
from backend import *
def multimodal_results(description_df):
conversation = []
for _, row in description_df.iterrows():
hotel_name = row['hotel_name']
description = row['description']
img = row['image']
img_path = f"{hotel_name}.png"
img.save(img_path)
bot_msg = {
"text": f"Here is {hotel_name}. {description}",
"files": [{"file": FileData(path=img_path)}]
}
conversation.append([{"text": "", "files": []}, bot_msg])
return conversation
def llm_results(description_df):
result_df = grouped_description(description_df)
context_result = create_prompt_result(result_df)
recommendation_prompt = build_prompt(context_result)
result = generate_text_response(recommendation_prompt)
conversation = [[{"text": "Based on your search...", "files": []}, {"text": f"**My recommendation:** {result}", "files": []}]]
return conversation
def chatbot_response(user_input, conversation):
bot_initial_message = {
"text": f"Looking for hotels in {user_input}...",
"files": []
}
conversation.append([{"text": user_input, "files": []}, bot_initial_message])
yield conversation
description_df = search_hotel(user_input)
if description_df is None or description_df.empty:
error_message = {"text": f"Sorry, I couldn't find any hotels for {user_input}. Please try another location.", "files": []}
conversation.append([{"text": user_input, "files": []}, error_message])
yield conversation
return # Exit the function early
hotel_conversation = multimodal_results(description_df)
for message_pair in hotel_conversation:
conversation.append(message_pair)
yield conversation
final_recommendation = llm_results(description_df)
for message_pair in final_recommendation:
conversation.append(message_pair)
yield conversation
def initial_conversation():
return [[
{"text": "**Welcome to Hotel Recommendation!**", "files": []},
{"text": "Please enter the place you're interested in visiting.", "files": []}
]]
with gr.Blocks() as demo:
gr.Markdown("# 🏨 Hotel Recommendation Chatbot")
gr.Markdown("**Provide the location to discover hotels and receive personalized recommendations!**")
initial_conv = initial_conversation()
chatbot = MultimodalChatbot(value=initial_conv, height=800)
with gr.Row():
place_input = gr.Textbox(label="Enter a place", placeholder="E.g., Paris, Tokyo, New York")
send_btn = gr.Button("Search Hotels")
send_btn.click(chatbot_response, inputs=[place_input, chatbot], outputs=chatbot)
demo.launch(debug=True)