Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,29 +1,46 @@
|
|
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 |
-
|
|
|
12 |
|
13 |
-
#
|
14 |
-
example_destinations = [
|
15 |
-
|
|
|
|
|
16 |
|
17 |
-
|
|
|
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 |
-
#
|
23 |
-
now_time = now_date.time()
|
24 |
-
now_date = now_date.date() + timedelta(days=1)
|
25 |
-
|
26 |
-
def generate_prompt(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
return f'''
|
28 |
Prepare trip schedule for {destination}, based on the following information:
|
29 |
* Arrival To: {arrival_to}
|
@@ -35,9 +52,9 @@ Prepare trip schedule for {destination}, based on the following information:
|
|
35 |
* Additional Notes: {additional_information}
|
36 |
'''.strip()
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
prompt = generate_prompt(**st.session_state)
|
41 |
|
42 |
# generate output
|
43 |
output = openai.Completion.create(
|
@@ -56,6 +73,7 @@ def submit():
|
|
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 |
|
@@ -65,25 +83,9 @@ with st.form(key='trip_form'):
|
|
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)
|
|
|
1 |
import os
|
2 |
import random
|
3 |
+
from datetime import datetime, timedelta, date, time
|
|
|
4 |
import openai
|
5 |
import streamlit as st
|
6 |
from dotenv import load_dotenv
|
7 |
+
from typing import Dict
|
8 |
|
9 |
+
# Load environment variables from a .env file
|
10 |
load_dotenv()
|
11 |
|
12 |
+
# Set the OpenAI API key from the .env file
|
13 |
+
openai.api_key = os.getenv('OPENAI_API')
|
14 |
|
15 |
+
# List of example destinations
|
16 |
+
example_destinations: list[str] = [
|
17 |
+
'Paris', 'London', 'New York', 'Tokyo', 'Sydney',
|
18 |
+
'Hong Kong', 'Singapore', 'Warsaw', 'Mexico City', 'Palermo'
|
19 |
+
]
|
20 |
|
21 |
+
# Randomly choose a destination
|
22 |
+
random_destination: str = random.choice(example_destinations)
|
23 |
|
24 |
+
# Get the current time and round to the nearest 15 minutes
|
25 |
+
now_date: datetime = datetime.now()
|
26 |
now_date = now_date.replace(minute=now_date.minute // 15 * 15, second=0, microsecond=0)
|
27 |
|
28 |
+
# Split into date and time objects
|
29 |
+
now_time: time = now_date.time()
|
30 |
+
now_date: date = now_date.date() + timedelta(days=1)
|
31 |
+
|
32 |
+
def generate_prompt(
|
33 |
+
destination: str,
|
34 |
+
arrival_to: str,
|
35 |
+
arrival_date: str,
|
36 |
+
arrival_time: str,
|
37 |
+
departure_from: str,
|
38 |
+
departure_date: str,
|
39 |
+
departure_time: str,
|
40 |
+
additional_information: str,
|
41 |
+
**kwargs: Dict[str, str]
|
42 |
+
) -> str:
|
43 |
+
"""Generate a trip schedule prompt based on the provided information."""
|
44 |
return f'''
|
45 |
Prepare trip schedule for {destination}, based on the following information:
|
46 |
* Arrival To: {arrival_to}
|
|
|
52 |
* Additional Notes: {additional_information}
|
53 |
'''.strip()
|
54 |
|
55 |
+
def submit() -> None:
|
56 |
+
"""Generate trip schedule based on user input and display the output."""
|
57 |
+
prompt: str = generate_prompt(**st.session_state)
|
58 |
|
59 |
# generate output
|
60 |
output = openai.Completion.create(
|
|
|
73 |
if 'output' not in st.session_state:
|
74 |
st.session_state['output'] = '--'
|
75 |
|
76 |
+
# UI Elements
|
77 |
st.title('GPT-3 Trip Scheduler')
|
78 |
st.subheader('Let us plan your trip!')
|
79 |
|
|
|
83 |
|
84 |
with c1:
|
85 |
st.subheader('Destination')
|
86 |
+
origin: str = st.text_input('Destination', value=random_destination, key='destination')
|
87 |
st.form_submit_button('Submit', on_click=submit)
|
88 |
|
89 |
with c2:
|
90 |
st.subheader('Arrival')
|
91 |
+
st.selectbox('Arrival To', ('Airport', 'Train Station', 'Bus Station', 'Ferry Terminal', 'Port', 'Other'), key='arrival_to
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|