File size: 1,732 Bytes
82af44b
 
 
 
 
20633ca
 
82af44b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20633ca
82af44b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import json
import os

from openai import OpenAI
import datetime
import gradio as gr

from schedule import create_event

def get_api_key():
    key = os.getenv('OPENAI_API_KEY')
    if not key:
        raise ValueError("Need to set the environment variable OPENAI_API_KEY")
    return key

client = OpenAI(api_key=get_api_key())

def parse_event(description):
    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."

    today = datetime.date.today().strftime("%B %d, %Y")
    response = client.chat.completions.create(
        model="gpt-4o",
        response_format={"type": "json_object"},
        messages=[
            {
                "role": "system",
                "content": f"You are a helpful assistant designed to output JSON. Today's date is {today}.",
            },
            {"role": "user", "content": prompt},
        ],
    )
    structured_response = response.choices[0].message.content.strip()
    return json.loads(structured_response)


def schedule(parsed):
    create_event(
        parsed["start_datetime"],
        parsed["end_datetime"],
        parsed["name"],
        parsed["description"],
        parsed["location"],
    )
    return "Event scheduled!"


with gr.Blocks() as demo:
    description = gr.TextArea(label="description")
    parse = gr.Button("parse")
    parsed = gr.JSON(label="parsed event")
    parse.click(parse_event, description, parsed)
    scheduleBtn = gr.Button("schedule")
    output = gr.Label(label="output")
    scheduleBtn.click(schedule, parsed, output)

demo.launch()