Spaces:
Sleeping
Sleeping
import gradio as gr | |
from composio import ComposioToolSet, App, Action | |
from datetime import datetime | |
import json | |
# Initialize the Composio toolset | |
toolset = ComposioToolSet() | |
# In-memory store for user connections | |
user_connections = {} | |
# Endpoint 1: Connect the user | |
def connect_user(entity_id): | |
connection_request = toolset.initiate_connection( | |
entity_id=entity_id, | |
app=App.GMAIL, | |
) | |
user_connections[entity_id] = { | |
"connection_status": connection_request.connectionStatus | |
} | |
if connection_request.connectionStatus == "INITIATED": | |
user_connections[entity_id]["redirect_url"] = connection_request.redirectUrl | |
return f"Connection initiated. Redirect user to: {connection_request.redirectUrl}" | |
elif connection_request.connectionStatus == "ACTIVE": | |
return "Connection is active." | |
else: | |
return "Connection failed. Please try again." | |
# Endpoint 2: Check connection status | |
def check_connection_status(entity_id): | |
if entity_id not in user_connections: | |
return "No connection found for this user." | |
return user_connections[entity_id]["connection_status"] | |
# Endpoint 3: Generate wrapped analysis | |
def generate_wrapped(entity_id): | |
if entity_id not in user_connections or user_connections[entity_id]["connection_status"] != "ACTIVE": | |
return "User is not connected or connection is not active." | |
current_year = datetime.now().year | |
time_min = f"{current_year}-01-01T00:00:00Z" | |
time_max = f"{current_year}-12-31T23:59:59Z" | |
request_params = { | |
"calendar_id": "primary", | |
"timeMin": time_min, | |
"timeMax": time_max, | |
"single_events": True, | |
"max_results": 2500, | |
"order_by": "startTime", | |
} | |
try: | |
events_response = toolset.execute_action( | |
action=Action.GOOGLECALENDAR_FIND_EVENT, | |
params=request_params, | |
entity_id=entity_id | |
) | |
if events_response["successfull"]: | |
stats = analyze_calendar_events(events_response) | |
return json.dumps(stats, indent=2) | |
else: | |
return "Failed to fetch calendar events." | |
except Exception as e: | |
return f"Error: {e}" | |
# Helper function to analyze events (mocked for simplicity) | |
def analyze_calendar_events(events_response): | |
# Mocking the analysis part for simplicity | |
return { | |
"total_meetings_this_year": 120, | |
"total_time_spent": "150 hours", | |
"busiest_day": "Tuesday", | |
"busiest_month": "September", | |
"average_meeting_duration": "1 hour", | |
"most_common_meeting_time": "10:00 AM", | |
"most_frequent_participant": "John Doe", | |
} | |
# Gradio interface | |
def connect(entity_id): | |
return connect_user(entity_id) | |
def status(entity_id): | |
return check_connection_status(entity_id) | |
def wrapped(entity_id): | |
return generate_wrapped(entity_id) | |
with gr.Blocks() as demo: | |
gr.Markdown("# Calendar Wrapped API") | |
with gr.Row(): | |
entity_id_input = gr.Textbox(label="Entity ID") | |
with gr.Row(): | |
connect_btn = gr.Button("Connect User") | |
status_btn = gr.Button("Check Status") | |
wrapped_btn = gr.Button("Generate Wrapped") | |
with gr.Row(): | |
connect_output = gr.Textbox(label="Connect Output") | |
status_output = gr.Textbox(label="Status Output") | |
wrapped_output = gr.Textbox(label="Wrapped Output") | |
connect_btn.click(connect, inputs=entity_id_input, outputs=connect_output) | |
status_btn.click(status, inputs=entity_id_input, outputs=status_output) | |
wrapped_btn.click(wrapped, inputs=entity_id_input, outputs=wrapped_output) | |
demo.launch() | |