Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import random
|
3 |
+
from datetime import datetime, timedelta
|
4 |
+
|
5 |
+
import openai
|
6 |
+
import streamlit as st
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
|
9 |
+
load_dotenv()
|
10 |
+
|
11 |
+
openai.api_key = os.getenv('OPENAI_API_KEY')
|
12 |
+
|
13 |
+
# Functions to generate prompt and trip schedule based on user input
|
14 |
+
example_destinations = ['Paris', 'London', 'New York', 'Tokyo', 'Sydney', 'Hong Kong', 'Singapore', 'Warsaw', 'Mexico City', 'Palermo']
|
15 |
+
random_destination = random.choice(example_destinations)
|
16 |
+
|
17 |
+
now_date = datetime.now()
|
18 |
+
|
19 |
+
# round to nearest 15 minutes
|
20 |
+
now_date = now_date.replace(minute=now_date.minute // 15 * 15, second=0, microsecond=0)
|
21 |
+
|
22 |
+
# split into date and time objects
|
23 |
+
now_time = now_date.time()
|
24 |
+
now_date = now_date.date() + timedelta(days=1)
|
25 |
+
|
26 |
+
def generate_prompt(destination, arrival_to, arrival_date, arrival_time, departure_from, departure_date, departure_time, additional_information, **kwargs):
|
27 |
+
return f'''
|
28 |
+
Prepare trip schedule for {destination}, based on the following information:
|
29 |
+
* Arrival To: {arrival_to}
|
30 |
+
* Arrival Date: {arrival_date}
|
31 |
+
* Arrival Time: {arrival_time}
|
32 |
+
* Departure From: {departure_from}
|
33 |
+
* Departure Date: {departure_date}
|
34 |
+
* Departure Time: {departure_time}
|
35 |
+
* Additional Notes: {additional_information}
|
36 |
+
'''.strip()
|
37 |
+
|
38 |
+
|
39 |
+
def submit():
|
40 |
+
prompt = generate_prompt(**st.session_state)
|
41 |
+
|
42 |
+
# generate output
|
43 |
+
output = openai.Completion.create(
|
44 |
+
engine='text-davinci-003',
|
45 |
+
prompt=prompt,
|
46 |
+
temperature=0.45,
|
47 |
+
top_p=1,
|
48 |
+
frequency_penalty=2,
|
49 |
+
presence_penalty=0,
|
50 |
+
max_tokens=1024
|
51 |
+
)
|
52 |
+
|
53 |
+
st.session_state['output'] = output['choices'][0]['text']
|
54 |
+
|
55 |
+
# Initialization
|
56 |
+
if 'output' not in st.session_state:
|
57 |
+
st.session_state['output'] = '--'
|
58 |
+
|
59 |
+
st.title('GPT-3 Trip Scheduler')
|
60 |
+
st.subheader('Let us plan your trip!')
|
61 |
+
|
62 |
+
# Form for User Input
|
63 |
+
with st.form(key='trip_form'):
|
64 |
+
c1, c2, c3 = st.columns(3)
|
65 |
+
|
66 |
+
with c1:
|
67 |
+
st.subheader('Destination')
|
68 |
+
origin = st.text_input('Destination', value=random_destination, key='destination')
|
69 |
+
st.form_submit_button('Submit', on_click=submit)
|
70 |
+
|
71 |
+
with c2:
|
72 |
+
st.subheader('Arrival')
|
73 |
+
|
74 |
+
st.selectbox('Arrival To', ('Airport', 'Train Station', 'Bus Station', 'Ferry Terminal', 'Port', 'Other'), key='arrival_to')
|
75 |
+
st.date_input('Arrival Date', value=now_date, key='arrival_date')
|
76 |
+
st.time_input('Arrival Time', value=now_time, key='arrival_time')
|
77 |
+
|
78 |
+
with c3:
|
79 |
+
st.subheader('Departure')
|
80 |
+
|
81 |
+
st.selectbox('Departure From', ('Airport', 'Train Station', 'Bus Station', 'Ferry Terminal', 'Port', 'Other'), key='departure_from')
|
82 |
+
st.date_input('Departure Date', value=now_date + timedelta(days=1), key='departure_date')
|
83 |
+
st.time_input('Departure Time', value=now_time, key='departure_time')
|
84 |
+
|
85 |
+
st.text_area('Additional Information', height=200, value='I want to visit as many places as possible! (respect time)', key='additional_information')
|
86 |
+
|
87 |
+
# Generate Trip Schedule
|
88 |
+
st.subheader('Trip Schedule')
|
89 |
+
st.write(st.session_state.output)
|