Spaces:
Runtime error
Runtime error
whatssubbuupto
commited on
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# test file for gradio
|
2 |
+
|
3 |
+
# import openai
|
4 |
+
import os
|
5 |
+
from openai import OpenAI
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
client = OpenAI()
|
9 |
+
|
10 |
+
def travel_agent(input_str):
|
11 |
+
completion = client.chat.completions.create(
|
12 |
+
model="gpt-4",
|
13 |
+
messages=[
|
14 |
+
{"role": "system",
|
15 |
+
"content": "You will be provided with constraints, and your task is to provide a travel itinerary which satisfies these constraints. Suggest restaurants and hotels as appropriate."},
|
16 |
+
{"role": "user", "content": input_str}
|
17 |
+
]
|
18 |
+
)
|
19 |
+
return (completion.choices[0].message.content)
|
20 |
+
|
21 |
+
|
22 |
+
def get_itinerary(destination, duration, events, cost, individuals, constraints, food, mobility, transport):
|
23 |
+
# create prompt
|
24 |
+
input_str = ('I want to go to ' + destination + ' for ' + str(duration) + ' days.' +
|
25 |
+
'. I want the itinerary to include the following activities:' + ','.join(events) +
|
26 |
+
'. On average, I want to spend ' + str(cost) + ' dollars per day for a group of ' + str(individuals) +
|
27 |
+
'. Some additional constraints are: ' + constraints + '.')
|
28 |
+
|
29 |
+
personalize = {
|
30 |
+
"Preferred food": food,
|
31 |
+
"Mobility constraint": mobility,
|
32 |
+
"Preferred transport": transport
|
33 |
+
}
|
34 |
+
if any(s != '' for s in personalize.values()):
|
35 |
+
for key, value in personalize.items():
|
36 |
+
if value != '':
|
37 |
+
input_str = input_str + key + ': ' + value + '. '
|
38 |
+
|
39 |
+
# print(input_str)
|
40 |
+
# call openAI with that string
|
41 |
+
itin = travel_agent(input_str)
|
42 |
+
|
43 |
+
# create a fun image
|
44 |
+
image_str = "Create a 2 by 2 collage of hyper-realistic images from " + destination + " based on places visited in this itinerary: " + itin
|
45 |
+
response = client.images.generate(
|
46 |
+
model="dall-e-3",
|
47 |
+
prompt=image_str,
|
48 |
+
size="1024x1024",
|
49 |
+
quality="standard",
|
50 |
+
n=1,
|
51 |
+
)
|
52 |
+
|
53 |
+
image_url = response.data[0].url
|
54 |
+
return itin, image_url
|
55 |
+
|
56 |
+
|
57 |
+
with gr.Blocks(theme=gr.themes.Default()) as demo:
|
58 |
+
gr.Markdown(
|
59 |
+
"""
|
60 |
+
# The NS Travel Agency
|
61 |
+
We are the best AI travel agency in the world! Tell us about your travel plans and we will create an itinerary for you!
|
62 |
+
""")
|
63 |
+
with gr.Row():
|
64 |
+
with gr.Column():
|
65 |
+
destination = gr.Textbox(label="Where would you like to go?")
|
66 |
+
with gr.Column():
|
67 |
+
with gr.Row():
|
68 |
+
duration = gr.Slider(label="How many days do you plan to stay?", value=5, minimum=1, maximum=30, step=1)
|
69 |
+
individuals = gr.Slider(label="How many individuals will be traveling?", value=1, minimum=1, maximum=10,
|
70 |
+
step=1)
|
71 |
+
cost = gr.Slider(label="What is your daily budget in dollars?", value=100, minimum=50, maximum=1000,
|
72 |
+
step=50)
|
73 |
+
with gr.Row():
|
74 |
+
with gr.Column():
|
75 |
+
constraints = gr.Textbox(label="Are there any constraints or events you wish to add?")
|
76 |
+
with gr.Column():
|
77 |
+
events = gr.CheckboxGroup(["Hiking", "Nature", "Sports", "Museums", "Shopping"],
|
78 |
+
label="What activities do you enjoy (select all)")
|
79 |
+
with gr.Accordion("Personalize further!", open=False):
|
80 |
+
with gr.Row():
|
81 |
+
food = gr.Textbox(label="What kinda food do you like?")
|
82 |
+
with gr.Row():
|
83 |
+
mobility = gr.Textbox(label="Do you have any mobility or accessibility needs?")
|
84 |
+
with gr.Row():
|
85 |
+
transport = gr.Textbox(label="What is your preferred mode of transportation?")
|
86 |
+
|
87 |
+
with gr.Row():
|
88 |
+
btn = gr.Button("Create Itinerary", variant='primary')
|
89 |
+
with gr.Row():
|
90 |
+
with gr.Column():
|
91 |
+
itin = gr.Text(label="Itinerary", autoscroll=False)
|
92 |
+
with gr.Column():
|
93 |
+
collage = gr.Image()
|
94 |
+
|
95 |
+
btn.click(get_itinerary, inputs=[destination, duration, events, cost, individuals, constraints, food, mobility, transport],
|
96 |
+
outputs=[itin, collage])
|
97 |
+
|
98 |
+
if __name__ == "__main__":
|
99 |
+
demo.launch(auth=("AI", os.getenv('PASSWD')))
|