Th3Nic3Guy's picture
initial
5ac7343
import json
import urllib.parse
import gradio as gr
import numpy as np
import pandas as pd
from tqdm import tqdm
import google.generativeai as genai
CONTEXT_TEMPLATE = """
I want to travel to `{place}`.
I am `{age_segment} {traveler}`.
I like `{preference}`.
My trip spans `{ndays}` days.
My budget is `{budget}`.
"""
RECOMENDATION_TEMPLATE = """
Write a 1500 word article on {place} keeping in mind the following:
Recommend best time to visit {place} for {age_segment} {traveler}.
Recommend the local cuisine, attractions, activities in {place}.
Recommend an itinerary for {place}.
Elaborate on the things to do in {place}, specially events like concerts, museums, arcades, etc.
"""
# config = json.load(open(file="config.json", mode="r"))
personas = {
"places": [
"greece",
"japan",
"thailand",
"bali",
"paris",
"london",
"new york",
"rome",
"amsterdam",
"barcelona",
"berlin",
"budapest",
"dubai",
"istanbul",
"lisbon",
"prague",
"rio de janeiro",
"san francisco",
"seoul",
"singapore",
"sydney",
"tokyo",
"vancouver",
"venice",
"vienna",
"washington dc"
],
"traveler": [
"solo backpacker",
"travelling with friends",
"travelling with family",
"travelling with kids",
"travelling with pets",
"travelling with a senior"
],
"budgets": ["economical", "balanced", "luxury"],
"preferences": [
"beach",
"city",
"food",
"history",
"sightseeing",
"shopping",
"adventure",
"nature",
"nightlife",
"culture",
"relaxation",
"romance",
"sports",
"wildlife"
]
}
age_map = {
(13, 18): "a Teenager",
(19, 30): "a Young Adult",
(31, 50): "an Adult",
(51, 65): "a Senior",
(66, 90): "an Elderly",
}
def generate_itinerary(
model_name,
api_key,
dest,
age,
traveler,
days,
budget,
preferences
):
age_segment = age_map[
list(filter(lambda x: x[0] <= age <= x[1], age_map.keys()))[0]
]
context = CONTEXT_TEMPLATE.format(
place=", ".join(dest) if isinstance(dest, list) else dest,
age_segment=age_segment,
traveler=traveler,
preference=", ".join(preferences),
ndays=days,
budget=budget,
)
def _msg(prompt):
_prompt = prompt.format(
place=", ".join(dest) if isinstance(dest, list) else dest,
age_segment=age_segment,
traveler=traveler,
preference=", ".join(preferences),
ndays=days,
budget=budget,
)
genai.configure(api_key=api_key)
model = genai.GenerativeModel(model_name)
# remove_txt = ". Dont tell me the itinerary. Ill ask that later"
return (
"\n\n### "
+ _prompt
+ "\n\n"
+ model.generate_content(_prompt).text
)
return _msg(RECOMENDATION_TEMPLATE)
def ui(api_key, model_name, ):
with gr.Blocks(title="Tr-AI-vel!!",) as questionaire:
hidden_api_key = gr.Text(value=api_key.value, visible=False)
hidden_model_name = gr.Text(value=model_name.value, visible=False)
with gr.Row():
with gr.Column(variant="compact"):
gr.Markdown(value="## Define your persona")
with gr.Row():
country = gr.components.Dropdown(
choices=sorted(personas["places"]),
value=personas["places"][0],
label="I want to visit",
)
with gr.Row():
traveler = gr.components.Dropdown(
label="I am a traveler",
choices=sorted(personas["traveler"]),
value="solo backpacker",
)
age = gr.components.Slider(
minimum=1, maximum=90, value=30, step=1, label="My age is"
)
with gr.Row():
days = gr.components.Slider(
minimum=3, maximum=21, value=5, step=1, label="I want to stay for"
)
budget = gr.components.Dropdown(
label="My budget is",
choices=sorted(personas["budgets"]),
value=personas["budgets"][0],
)
with gr.Row():
preferences = gr.components.CheckboxGroup(
label="I prefer",
choices=personas["preferences"],
value=personas["preferences"][:2],
)
btn = gr.Button(
value="Generate Itinerary",
)
with gr.Column(variant="compact"):
gr.Markdown(value="## Your Itinerary would show up here")
with gr.Row():
ta = gr.Markdown(
label="Itinerary",
)
btn.click(
generate_itinerary,
[
model_name,
api_key,
country,
age,
traveler,
days,
budget,
preferences,
],
ta,
)
return questionaire