import sys import os sys.path.append(os.path.abspath(os.path.join(os.getcwd(), ".."))) os.chdir(os.path.dirname(os.path.abspath(__file__))) import gradio as gr import json from tools.flights.apis import Flights from tools.accommodations.apis import Accommodations from tools.restaurants.apis import Restaurants from tools.googleDistanceMatrix.apis import GoogleDistanceMatrix from tools.attractions.apis import Attractions import pandas as pd from gradio.events import Changeable, Streamable, Submittable, on from src.utils import * from gradio.events import EventListenerMethod import math data = load_line_json_data('query.jsonl') query_dropdown_text = [f"Query {index}" for index in range(1, len(data)+1)] with open('assets/styles.css', 'r') as css_file: css_content = css_file.read() flight = Flights() accommodations = Accommodations() restaurants = Restaurants() googleDistanceMatrix = GoogleDistanceMatrix() attractions = Attractions() annotation_idx = 4 cost = 0 global_user_name = "" transportaion_val = "" accommodation_val = "" breakfast_val = "" lunch_val = "" dinner_val = "" attraction_val = "" def auto_update_current_cost(*components): global cost cost = 0 for i in range(7): # flight if judge_valid_info(components[7+i]): if components[7+i] != '-': if 'Flight Number' in components[7+i]: cost += flight.data[flight.data['Flight Number'] == components[7+i].split('Flight Number: ')[1].split(',')[0]]['Price'].values[0] * data[annotation_idx-1]['people_number'] elif 'Self-driving' in components[7+i] or 'Taxi' in components[7+i]: if 'Self-driving' in components[7+i]: cost += int(components[7+i].split('cost: ')[1]) * math.ceil(data[annotation_idx-1]['people_number'] * 1.0 / 5) else: cost += int(components[7+i].split('cost: ')[1]) * math.ceil(data[annotation_idx-1]['people_number'] * 1.0 / 4) # breakfast if judge_valid_info(components[7*2+i]): if components[7*2+i] != '-': name, city = get_valid_name_city(components[7*2+i]) cost += restaurants.data[(restaurants.data['Name'] == name) & (restaurants.data['City'] == city)]['Average Cost'].values[0] * data[annotation_idx-1]['people_number'] # attraction # lunch if judge_valid_info(components[7*4+i]): if components[7*4+i] != '-': name, city = get_valid_name_city(components[7*4+i]) cost += restaurants.data[(restaurants.data['Name'] == name) & (restaurants.data['City'] == city)]['Average Cost'].values[0] * data[annotation_idx-1]['people_number'] # dinner if judge_valid_info(components[7*5+i]): if components[7*5+i] != '-': name, city = get_valid_name_city(components[7*5+i]) cost += restaurants.data[(restaurants.data['Name'] == name) & (restaurants.data['City'] == city)]['Average Cost'].values[0] * data[annotation_idx-1]['people_number'] # accommodation if judge_valid_info(components[7*6+i]): if components[7*6+i] != '-': name, city = get_valid_name_city(components[7*6+i]) res = accommodations.data[(accommodations.data['NAME'] == name) & (accommodations.data['city'] == city)] cost += res['price'].values[0] * math.ceil(data[annotation_idx-1]['people_number'] * 1.0 / res['maximum occupancy'].values[0]) return gr.Number(label="Current Cost", value=cost, interactive=False,elem_classes="current_cost") def auto_update_current_cost_number(fn, input_components, output_components): events: list[EventListenerMethod] = [] for component in input_components: if isinstance(component, Streamable) and component.streaming: events.append(component.stream) elif isinstance(component, Changeable): events.append(component.change) on( events, fn, input_components, output_components ) def get_google_distance_matrix(mode,departure_city, destination_city): if mode == "Self-driving": return pd.DataFrame(googleDistanceMatrix.run_for_annotation(departure_city, destination_city,'driving'),index=[0]) elif mode == "Taxi": return pd.DataFrame(googleDistanceMatrix.run_for_annotation(departure_city, destination_city,'taxi'),index=[0]) def return_list_items(items): return items[0],items[1],items[2],items[3],items[4],items[5],items[6] def submit_update_options(query_candidate, *components): global query_dropdown_text ,cost global transportaion_val, accommodation_val, breakfast_val, lunch_val, dinner_val, attraction_val json_data = [{} for i in range(7)] new_components = [ None for i in range(len(components))] # write the annotation into the json file cuisine_set = set() for i in range(data[annotation_idx-1]['days']): json_data[i]['days'] = i+1 if judge_submit_info(components[i],i+1,'current city',data[annotation_idx-1]): json_data[i]['current_city'] = components[i] if judge_submit_info(components[7+i],i+1,'transportation',data[annotation_idx-1]): json_data[i]['transportation'] = components[7+i] if judge_submit_info(components[7*2+i],i+1,'breakfast',data[annotation_idx-1]): cuisine_set = judge_valid_cuisine(components[7*2+i],data[annotation_idx-1],restaurants.data, cuisine_set) json_data[i]['breakfast'] = components[7*2+i] if judge_submit_info(components[7*3+i],i+1,'attraction',data[annotation_idx-1]): json_data[i]['attraction'] = components[7*3+i] if judge_submit_info(components[7*4+i],i+1,'lunch',data[annotation_idx-1]): cuisine_set = judge_valid_cuisine(components[7*4+i],data[annotation_idx-1],restaurants.data, cuisine_set) json_data[i]['lunch'] = components[7*4+i] if judge_submit_info(components[7*5+i],i+1,'dinner',data[annotation_idx-1]): cuisine_set = judge_valid_cuisine(components[7*5+i],data[annotation_idx-1],restaurants.data, cuisine_set) json_data[i]['dinner'] = components[7*5+i] if judge_submit_info(components[7*6+i],i+1,'accommodation',data[annotation_idx-1],accommodations.data): json_data[i]['accommodation'] = components[7*6+i] if data[annotation_idx-1]['local_constraint']['cuisine'] and len(list(cuisine_set)) != len(data[annotation_idx-1]['local_constraint']['cuisine']): raise gr.Error("Please note the cuisine constraints.") consectutive_accommodation = count_consecutive_values(components[7*6:7*6+data[annotation_idx-1]['days']]) # print(consectutive_accommodation) # print("") for unit in consectutive_accommodation: # print(unit) if unit and unit[0] not in ['-',''] and unit!="You don't need to fill in the information for this or later days.": name, city = get_valid_name_city(unit[0]) if unit[1] < accommodations.data[(accommodations.data['NAME'] == name) & (accommodations.data['city'] == city)].iloc[0]['minimum nights']: raise gr.Error(f"Please note the minimum nights of {unit[0]}.") if cost > data[annotation_idx-1]['budget']: raise gr.Error("The current cost is higher than the budget, please modify the information.") # return [gr.Textbox(value="The current cost is higher than the budget, please modify the information.",interactive=False)]*6 + [gr.Dropdown(choices=query_dropdown_text)] # ['is_reasonalbe_visiting_city', 'is_valid_restaurants', 'is_valid_attractions', 'is_valid_transportation', 'is_valid_information_in_current_city', 'is_valid_information_in_sandbox'] for i in range(data[annotation_idx-1]['days']): new_components[i] = gr.Textbox(label="Current City", value=None,interactive=False) new_components[7+i] = gr.Textbox(label="Transportation", value="-",interactive=False) new_components[7*2+i] = gr.Textbox(label="Breakfast", value=None, interactive=False) new_components[7*3+i] = gr.Textbox(label="Attractions", value=None, interactive=False) new_components[7*4+i] = gr.Textbox(label="Lunch", value=None,interactive=False) new_components[7*5+i] = gr.Textbox(label="Dinner", value=None,interactive=False) new_components[7*6+i] = gr.Textbox(label="Accommodation", value=None,interactive=False) with open('../data/annotation/{}/annotation_{}.json'.format(global_user_name,annotation_idx), 'w') as f: json.dump(json_data, f) transportaion_val = "" accommodation_val = "" breakfast_val = "" lunch_val = "" dinner_val = "" attraction_val = "" new_options = query_dropdown_text new_options[annotation_idx-1] = f"Query {annotation_idx} --- Done" query_dropdown_text = new_options return [gr.Dropdown(choices=new_options)] + new_components + [gr.Textbox(value=None) for _ in range(14)] def update_gloabal_idx(selected_option): global annotation_idx,cost global transportaion_val, accommodation_val, breakfast_val, lunch_val, dinner_val, attraction_val transportaion_val = "" accommodation_val = "" breakfast_val = "" lunch_val = "" dinner_val = "" attraction_val = "" annotation_idx = extract_query_number(selected_option) new_components = [ gr.Textbox() for i in range(49)] city_list = get_city_list(data[annotation_idx-1]['days'], data[annotation_idx-1]['org'], data[annotation_idx-1]['dest']) city_drop_down = [None for i in range(63)] date_component_list = [gr.Textbox(value="", interactive=False) for i in range(7)] for i in range(data[annotation_idx-1]['days']): new_components[i] = gr.Textbox(label="Current City",value="",interactive=False) new_components[7+i] = gr.Textbox(label="Transportation",value="",interactive=False) new_components[7*2+i] = gr.Textbox(label="Breakfast", value="",interactive=False) new_components[7*3+i] = gr.Textbox(label="Attractions", value="",interactive=False) new_components[7*4+i] = gr.Textbox(label="Lunch", value="",interactive=False) new_components[7*5+i] = gr.Textbox(label="Dinner", value="",interactive=False) new_components[7*6+i] = gr.Textbox(label="Accommodation", value="",interactive=False) for i in range(63): if i % 9 in [0,2,4]: city_drop_down[i] = gr.Radio(label="Departure City",choices=city_list) elif i % 9 in [1,3,5]: city_drop_down[i] = gr.Radio(label="Destination City",choices=city_list) else: city_drop_down[i] = gr.Radio(label="City",choices=city_list) for i in range(data[annotation_idx-1]['days']): date_component_list[i] = gr.Textbox(label="Date", value=data[annotation_idx-1]['date'][i], interactive=False) return [create_data_display(css_content,data,annotation_idx),gr.update(visible=True)]+new_components+city_drop_down+date_component_list+[gr.Number(value=cost, interactive=False)] def Recommend_new(x): return x + ": Highest Cosine Similarity" def current_city_selection(departure_city, dest_city): final_city = "" if departure_city == dest_city: final_city = departure_city else: final_city = "from " + departure_city + " to " + dest_city return gr.Textbox(label="Current City", value=final_city, interactive=False) def tab_hint(current_city,transportaion,breakfast,attraction,lunch,dinner,accommodation,evt:gr.SelectData): global transportaion_val, accommodation_val, breakfast_val, lunch_val, dinner_val, attraction_val transportaion_val = "" accommodation_val = "" breakfast_val = breakfast lunch_val = lunch dinner_val = dinner attraction_val = attraction if data[annotation_idx-1]['days'] <= evt.index: return [gr.Textbox(value="You don't need to fill in the information for this or later days.",interactive=False)]*7 + [gr.Dropdown(choices=query_dropdown_text)] + [gr.Textbox(value="") for _ in range(2)] else: return current_city, transportaion,breakfast,attraction,lunch,dinner,accommodation, gr.Dropdown(choices=query_dropdown_text),gr.Textbox(value=""), gr.Textbox(value="") css = """ #submit_button {background: #00a1b8;} #clear_button {background: #ff8069;} """ with gr.Blocks(css=css) as demo: gr.HTML(f"""