Upload 2 files
Browse files- app.py +54 -7
- schedule.py +55 -0
app.py
CHANGED
@@ -1,10 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
demo
|
7 |
-
inputs="text",
|
8 |
-
outputs="text",
|
9 |
-
allow_flagging="never")
|
10 |
-
demo.launch()
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
|
4 |
+
from openai import OpenAI
|
5 |
+
import datetime
|
6 |
import gradio as gr
|
7 |
|
8 |
+
from schedule import create_event
|
9 |
+
|
10 |
+
def get_api_key():
|
11 |
+
key = os.getenv('OPENAI_API_KEY')
|
12 |
+
if not key:
|
13 |
+
raise ValueError("Need to set the environment variable OPENAI_API_KEY")
|
14 |
+
return key
|
15 |
+
|
16 |
+
client = OpenAI(api_key=get_api_key())
|
17 |
+
|
18 |
+
def parse_event(description):
|
19 |
+
prompt = f"Parse the following event description into structured JSON fields: '{description}'. The fields should include name, description, location, start_datetime, and end_datetime. Put an emoji relevant to the event at the beginning of the name."
|
20 |
+
|
21 |
+
today = datetime.date.today().strftime("%B %d, %Y")
|
22 |
+
response = client.chat.completions.create(
|
23 |
+
model="gpt-4o",
|
24 |
+
response_format={"type": "json_object"},
|
25 |
+
messages=[
|
26 |
+
{
|
27 |
+
"role": "system",
|
28 |
+
"content": f"You are a helpful assistant designed to output JSON. Today's date is {today}.",
|
29 |
+
},
|
30 |
+
{"role": "user", "content": prompt},
|
31 |
+
],
|
32 |
+
)
|
33 |
+
structured_response = response.choices[0].message.content.strip()
|
34 |
+
return json.loads(structured_response)
|
35 |
+
|
36 |
+
|
37 |
+
def schedule(parsed):
|
38 |
+
create_event(
|
39 |
+
parsed["start_datetime"],
|
40 |
+
parsed["end_datetime"],
|
41 |
+
parsed["name"],
|
42 |
+
parsed["description"],
|
43 |
+
parsed["location"],
|
44 |
+
)
|
45 |
+
return "Event scheduled!"
|
46 |
+
|
47 |
+
|
48 |
+
with gr.Blocks() as demo:
|
49 |
+
description = gr.TextArea(label="description")
|
50 |
+
parse = gr.Button("parse")
|
51 |
+
parsed = gr.JSON(label="parsed event")
|
52 |
+
parse.click(parse_event, description, parsed)
|
53 |
+
scheduleBtn = gr.Button("schedule")
|
54 |
+
output = gr.Label(label="output")
|
55 |
+
scheduleBtn.click(schedule, parsed, output)
|
56 |
|
57 |
+
demo.launch()
|
|
|
|
|
|
|
|
schedule.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import datetime
|
2 |
+
import os
|
3 |
+
|
4 |
+
from google.oauth2.credentials import Credentials
|
5 |
+
from google_auth_oauthlib.flow import InstalledAppFlow
|
6 |
+
from google.auth.transport.requests import Request
|
7 |
+
from googleapiclient.discovery import build
|
8 |
+
|
9 |
+
|
10 |
+
# Scopes required to access and manipulate calendar events
|
11 |
+
SCOPES = ['https://www.googleapis.com/auth/calendar']
|
12 |
+
|
13 |
+
def get_calendar_service():
|
14 |
+
creds = None
|
15 |
+
# Load existing credentials
|
16 |
+
if os.path.exists('token.json'):
|
17 |
+
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
|
18 |
+
# If no valid credentials are available, let the user log in.
|
19 |
+
if not creds or not creds.valid:
|
20 |
+
if creds and creds.expired and creds.refresh_token:
|
21 |
+
creds.refresh(Request())
|
22 |
+
else:
|
23 |
+
flow = InstalledAppFlow.from_client_secrets_file(
|
24 |
+
'credentials.json', SCOPES)
|
25 |
+
creds = flow.run_local_server(port=0)
|
26 |
+
# Save the credentials for the next run
|
27 |
+
with open('token.json', 'w') as token:
|
28 |
+
token.write(creds.to_json())
|
29 |
+
|
30 |
+
service = build('calendar', 'v3', credentials=creds)
|
31 |
+
return service
|
32 |
+
|
33 |
+
def create_event(start_time_str, end_time_str, summary, description, location):
|
34 |
+
service = get_calendar_service()
|
35 |
+
start_time = datetime.datetime.fromisoformat(start_time_str)
|
36 |
+
end_time = datetime.datetime.fromisoformat(end_time_str)
|
37 |
+
event = {
|
38 |
+
'summary': summary,
|
39 |
+
'location': location,
|
40 |
+
'description': description,
|
41 |
+
'start': {
|
42 |
+
'dateTime': start_time.strftime("%Y-%m-%dT%H:%M:%S"),
|
43 |
+
'timeZone': 'Europe/Paris',
|
44 |
+
},
|
45 |
+
'end': {
|
46 |
+
'dateTime': end_time.strftime("%Y-%m-%dT%H:%M:%S"),
|
47 |
+
'timeZone': 'Europe/Paris',
|
48 |
+
},
|
49 |
+
}
|
50 |
+
|
51 |
+
event = service.events().insert(calendarId='primary', body=event).execute()
|
52 |
+
print('Event created: %s' % (event.get('htmlLink')))
|
53 |
+
|
54 |
+
# Example of adding an event
|
55 |
+
create_event('2024-05-15T09:00:00', '2024-05-15T13:00:00', 'Meeting with Bob', 'Discuss project', 'Cafe Paris')
|