File size: 2,879 Bytes
2a8c9b4 b68fd83 c6bf829 b68fd83 |
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 |
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)
|