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()