Spaces:
Sleeping
Sleeping
import gradio as gr | |
import os | |
from gradio.themes import Size, GoogleFont | |
from agents.gather_agent import Gather_Agent | |
from agents.check_agent import Check_Agent | |
from agents.response_agent import Response_Agent | |
from agents.planner_agent import Planner_Agent | |
import get_map | |
# Create custom Color objects for our primary, secondary, and neutral colors | |
primary_color = gr.themes.colors.green | |
secondary_color = gr.themes.colors.amber | |
neutral_color = gr.themes.colors.stone # Assuming black for text | |
# Set the sizes | |
spacing_size = gr.themes.sizes.spacing_md | |
radius_size = gr.themes.sizes.radius_md | |
text_size = gr.themes.sizes.text_md | |
# Set the fonts | |
font = GoogleFont("Source Sans Pro") | |
font_mono = GoogleFont("IBM Plex Mono") | |
# Create the theme | |
theme = gr.themes.Base( | |
primary_hue=primary_color, | |
secondary_hue=secondary_color, | |
neutral_hue=neutral_color, | |
spacing_size=spacing_size, | |
radius_size=radius_size, | |
text_size=text_size, | |
font=font, | |
font_mono=font_mono | |
) | |
gather_agent = Gather_Agent() | |
check_agent = Check_Agent() | |
response_agent = Response_Agent() | |
planner_agent = Planner_Agent() | |
def send_message(user_input, chat_history): | |
isComplete = False | |
helper_anwser = "" | |
_input_gather = gather_agent.format_prompt(history=chat_history, input=user_input) | |
parsed_result_gather = gather_agent.get_parsed_result(_input_gather) | |
_input_check = check_agent.format_prompt(input=parsed_result_gather) | |
isComplete = check_agent.get_parsed_result(_input_check) | |
if isComplete == False: | |
_input_response = response_agent.format_prompt(input=parsed_result_gather) | |
helper_anwser = response_agent.get_parsed_result(_input_response) | |
# _input_check = check_agent.format_prompt(input=parsed_result_gather) | |
# isComplete, helper_anwser = check_agent.get_parsed_result(_input_check) | |
return isComplete, helper_anwser, parsed_result_gather | |
def get_itenerary(parsed_result_gather): | |
_input_planner = planner_agent.format_prompt(parsed_result_gather) | |
return planner_agent.get_itenerary(_input_planner) | |
def get_itenerary_places(itenerary): | |
_input_places = planner_agent.format_prompt_to_get_places(itenerary) | |
return planner_agent.get_places_from_itenerary(_input_places) | |
# isComplete = False | |
# chat_history = "" | |
# helper_anwser = "Hello, can you tell me your trip details and constraints so I can give you great recomendations?" | |
# user_input = input("Helper: " + helper_anwser + "\nUser: ") | |
with gr.Blocks(theme=theme, title="TrainLine") as demo: | |
gr.Markdown( | |
""" | |
<div style="vertical-align: middle"> | |
<div style="float: left"> | |
<img src="https://static.trainlinecontent.com/content/vul/logos/trainline-mint.svg" alt="" | |
width="120" height="120"> | |
</div> | |
</div> | |
""") | |
helper_anwser = "Hello, can you tell me your trip details and constraints so I can give you great recomendations?" | |
with gr.TabItem("Travel Companion"): | |
chatbot = gr.Chatbot(value=[[None, helper_anwser]]) | |
user_input = gr.Textbox() | |
gr.Examples([ | |
"I want to go to Rome. can you recommend a site seeing tour for one day?", | |
"I like to walk a lot and i prefer to visit fine arts museums", | |
"Porto for 3 days. i will arrive on monday and leave on thursday. i can only visit places after 5pm so be " | |
"sure i can visit those places", | |
"I would like to plan a trip to Europe with my family of four. We want to visit Paris, Rome, and Madrid in " | |
"10 days. Can you suggest an itinerary that includes transportation and accommodations? " | |
"Also, please provide information on the best restaurants in each city for a budget of $50 per person per meal." | |
], user_input) | |
with gr.TabItem("Map"): | |
map = gr.Plot(visible=True).style() | |
result_df = gr.Dataframe(type="pandas", visible=True) | |
isComplete = False | |
history = "" | |
locations = [] | |
def user(user_message, history): | |
print(user_message, history) | |
return gr.update(value="", interactive=False), history + [[user_message, None]] | |
# def bot(chat_history): | |
# print(chat_history) | |
# # Create history | |
# history = "" | |
# for i in range(len(chat_history)-1): | |
# history += "User: " + chat_history[i][0] + "\nHelper: " + chat_history[i][1] + "\n" | |
# history += "User: " + chat_history[-1][0] | |
# # isComplete, helper_anwser, data_collected = send_message(message, history) | |
# # if isComplete == True: | |
# # helper_anwser = get_itenerary(data_collected) | |
# # chat_history.append((message, helper_anwser)) | |
# return "", chat_history | |
def respond(chat_history): | |
print(chat_history) | |
# Create history | |
history = "" | |
for i in range(1, len(chat_history) - 1): | |
history += "User: " + chat_history[i][0] + "\nHelper: " + chat_history[i][1] + "\n" | |
message = chat_history[-1][0] | |
print(history) | |
print(message) | |
isComplete, helper_anwser, data_collected = send_message(message, history) | |
chat_history.pop(-1) | |
if isComplete == True: | |
itenerary = get_itenerary(data_collected) | |
locations = get_itenerary_places(itenerary) | |
helper_anwser = itenerary + "\nList of places with adresses: " + str(locations) | |
map, result_df = get_map.filter_map(locations) | |
chat_history.append((message, helper_anwser)) | |
return chat_history, map, result_df | |
chat_history.append((message, helper_anwser)) | |
return chat_history, None, None | |
# user_input.submit(respond, [user_input, chatbot], [user_input, chatbot]) | |
response = user_input.submit(user, [user_input, chatbot], [user_input, chatbot], queue=False).then( | |
respond, chatbot, [chatbot, map, result_df] | |
) | |
response.then(lambda: gr.update(interactive=True), None, [user_input], queue=False) | |
# if map != None: | |
# map.update(visible=True) | |
# result_df.update(visible=True) | |
demo.launch(auth=( os.environ["USER"],os.environ["PASSWORD"])) | |
# while isComplete == False: | |
# isComplete, helper_anwser, data_collected = main.send_message(user_input, chat_history) | |
# if isComplete == False: | |
# chat_history += "User: " + user_input + "\nHelper: " + helper_anwser + "\n" | |
# user_input = input("Helper: " + helper_anwser + "\nUser: ") | |
# itenerary_response = main.get_itenerary(data_collected) | |
# I would like to go to paris, from 12th of july to 15th of July, I want to visit museums, eat at local restaurants and visit the louvre on my first day. My son is allergic to peanuts, and I like to sleep in, so please don't book anything before 11am. I would also like to not get further then 2km from the city's center. |