File size: 11,955 Bytes
d62bdc3
 
 
58d2388
62e41c2
 
7edabc5
5b1e4ca
 
d62bdc3
62e41c2
5b1e4ca
58d2388
 
 
7edabc5
 
d62bdc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b1e4ca
 
d62bdc3
62e41c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b1e4ca
7edabc5
5b1e4ca
7edabc5
 
 
 
5b1e4ca
7edabc5
 
 
 
 
 
 
f3ccd4f
 
7edabc5
 
 
 
d62bdc3
1597a23
7edabc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b1e4ca
d62bdc3
 
7edabc5
5b1e4ca
 
d62bdc3
 
 
 
 
 
 
1597a23
d62bdc3
5b1e4ca
d62bdc3
 
 
 
 
 
5b1e4ca
 
d62bdc3
 
 
 
5b1e4ca
d62bdc3
 
 
62e41c2
 
 
 
d62bdc3
62e41c2
 
 
 
 
 
 
 
d62bdc3
62e41c2
 
 
 
5b1e4ca
7edabc5
62e41c2
 
 
 
 
 
 
 
 
 
 
5b1e4ca
d62bdc3
 
 
 
5b1e4ca
d62bdc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b1e4ca
d62bdc3
58d2388
5b1e4ca
d62bdc3
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
from dataclasses import dataclass
from enum import Enum
from typing import Optional, Dict, Any
from composio_llamaindex import ComposioToolSet, App, Action
from datetime import datetime, timedelta
from collections import defaultdict, Counter
from llama_index_llms_openai import OpenAI
import gradio as gr
import os
import json
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

llm = OpenAI(model='gpt-4o', api_key=os.getenv('OPENAI_API_KEY'))

class ConnectionStatus(Enum):
    PENDING = "pending"
    ACTIVE = "active"
    FAILED = "failed"
    NOT_FOUND = "not_found"

@dataclass
class APIResponse:
    success: bool
    data: Optional[Dict[str, Any]] = None
    error: Optional[str] = None
    
    def to_json(self) -> str:
        return json.dumps({
            "success": self.success,
            "data": self.data,
            "error": self.error
        })

class CalendarService:
    def __init__(self):
        self.toolset = ComposioToolSet(api_key=os.getenv('COMPOSIO_API_KEY'))
        self.connections: Dict[str, Dict[str, Any]] = {}

    def analyze_calendar_events(self, response_data):
        """
        Analyze calendar events and return statistics about meetings.
        """
        current_year = datetime.now().year
        meetings = []
        participants = []
        meeting_times = []
        total_duration = timedelta()
        monthly_meetings = defaultdict(int)
        daily_meetings = defaultdict(int)
        
        events = response_data.get('data', {}).get('event_data', {}).get('event_data', [])
        
        for event in events:
            start_data = event.get('start', {})
            end_data = event.get('end', {})
            
            try:
                start = datetime.fromisoformat(start_data.get('dateTime').replace('Z', '+00:00'))
                end = datetime.fromisoformat(end_data.get('dateTime').replace('Z', '+00:00'))
                
                if start.year == current_year:
                    duration = end - start
                    total_duration += duration
                    
                    monthly_meetings[start.strftime('%B')] += 1
                    daily_meetings[start.strftime('%A')] += 1
                    meeting_times.append(start.strftime('%H:%M'))
                    
                    if 'attendees' in event:
                        for attendee in event['attendees']:
                            if attendee.get('responseStatus') != 'declined':
                                participants.append(attendee.get('email'))
                    
                    organizer_email = event.get('organizer', {}).get('email')
                    if organizer_email:
                        participants.append(organizer_email)
                    
                    meetings.append({
                        'start': start,
                        'duration': duration,
                        'summary': event.get('summary', 'No Title')
                    })
            except (ValueError, TypeError, AttributeError) as e:
                print(f"Error processing event: {e}")
                continue
        
        total_meetings = len(meetings)
        stats = {
            "total_meetings_this_year": total_meetings
        }
        
        if total_meetings > 0:
            stats.update({
                "total_time_spent": str(total_duration),
                "busiest_month": max(monthly_meetings.items(), key=lambda x: x[1])[0] if monthly_meetings else "N/A",
                "busiest_day": max(daily_meetings.items(), key=lambda x: x[1])[0] if daily_meetings else "N/A",
                "most_frequent_participant": Counter(participants).most_common(1)[0][0] if participants else "N/A",
                "average_meeting_duration": str(total_duration / total_meetings),
                "most_common_meeting_time": Counter(meeting_times).most_common(1)[0][0] if meeting_times else "N/A",
                "monthly_breakdown": dict(monthly_meetings),
                "daily_breakdown": dict(daily_meetings)
            })
        else:
            stats.update({
                "total_time_spent": "0:00:00",
                "busiest_month": "N/A",
                "busiest_day": "N/A",
                "most_frequent_participant": "N/A",
                "average_meeting_duration": "0:00:00",
                "most_common_meeting_time": "N/A",
                "monthly_breakdown": {},
                "daily_breakdown": {}
            })
        
        return stats
        
    def generate_wrapped(self, entity_id: str) -> APIResponse:
        try:
            # Get current year's start and end dates
            current_year = datetime.now().year
            time_min = f"{current_year},1,1,0,0,0"
            time_max = f"{current_year},12,31,23,59,59"
            
            request_params = {
                "calendar_id": "primary",
                "timeMin": time_min,
                "timeMax": time_max,
                "single_events": True,
                "max_results": 2500,
                "order_by": "startTime"
            }
            
            events_response = self.toolset.execute_action(
                action=Action.GOOGLECALENDAR_FIND_EVENT,
                params=request_params,
                entity_id=entity_id
            )
            
            if events_response["successfull"]:
                stats = self.analyze_calendar_events(events_response)
                
                # Create a prompt for the LLM with the stats
                prompt = f"""Based on the following calendar statistics, analyze which tech billionaire this person's schedule most resembles and provide brief comments for each metric:

Stats:
- Total meetings this year: {stats['total_meetings_this_year']}
- Total time in meetings: {stats['total_time_spent']}
- Busiest month: {stats['busiest_month']}
- Busiest day: {stats['busiest_day']}
- Average meeting duration: {stats['average_meeting_duration']}
- Most common meeting time: {stats['most_common_meeting_time']}
- Most frequent collaborator: {stats['most_frequent_participant']}

Please provide:
1. Which tech billionaire's schedule this most resembles and why
2. A one-sentence comment for each of the above metrics
Format your response as JSON with keys: 'billionaire_match' and 'metric_comments'

Dont make any extra comments before or after. Just give the required output and shut up. Dont send any sentences saying like here's your response, or the output is generated"""

                llm_analysis = llm.complete(prompt)
                
                try:
                    llm_json = json.loads(llm_analysis)
                except json.JSONDecodeError:
                    llm_json = {
                        "billionaire_match": "Analysis unavailable",
                        "metric_comments": "Comments unavailable"
                    }
                
                # Add LLM analysis to stats
                stats.update({
                    "schedule_analysis": llm_json["billionaire_match"],
                    "metric_insights": llm_json["metric_comments"]
                })
                
                return APIResponse(
                    success=True,
                    data=stats
                )
            else:
                return APIResponse(
                    success=False,
                    error=events_response["error"] or "Failed to fetch calendar events"
                )
            
        except Exception as e:
            return APIResponse(
                success=False,
                error=f"Failed to generate wrapped: {str(e)}"
            )
    
    def check_status(self, entity_id: str) -> APIResponse:
        try:
            if entity_id not in self.connections:
                return APIResponse(
                    success=False,
                    error="No connection found for this entity ID"
                )
            
            connection = self.connections[entity_id]
            
            return APIResponse(
                success=True,
                data={
                    'status': connection['status'],
                    'message': f"Connection status: {connection['status']}"
                }
            )
            
        except Exception as e:
            return APIResponse(
                success=False,
                error=f"Failed to check status: {str(e)}"
            )
    
    def generate_wrapped(self, entity_id: str) -> APIResponse:
        try:
            # Get current year's start and end dates
            current_year = datetime.now().year
            time_min = f"{current_year},1,1,0,0,0"
            time_max = f"{current_year},12,31,23,59,59"
            
            request_params = {
                "calendar_id": "primary",
                "timeMin": time_min,
                "timeMax": time_max,
                "single_events": True,
                "max_results": 2500,
                "order_by": "startTime"
            }
            
            events_response = self.toolset.execute_action(
                action=Action.GOOGLECALENDAR_FIND_EVENT,
                params=request_params,
                entity_id=entity_id
            )
            llm_response = llm.complete(f"{str(events_response)} is the event response. Based on this what tech billionaire are they most similar to and why??")
            if events_response["successfull"]:
                stats = self.analyze_calendar_events(events_response)
                return APIResponse(
                    success=True,
                    data=stats
                )
            else:
                return APIResponse(
                    success=False,
                    error=events_response["error"] or "Failed to fetch calendar events"
                )
            
        except Exception as e:
            return APIResponse(
                success=False,
                error=f"Failed to generate wrapped: {str(e)}"
            )

def create_gradio_api():
    service = CalendarService()
    
    def handle_connection(entity_id: str, redirect_url: Optional[str] = None) -> str:
        response = service.initiate_connection(entity_id, redirect_url)
        return response.to_json()
    
    def check_status(entity_id: str) -> str:
        response = service.check_status(entity_id)
        return response.to_json()
    
    def generate_wrapped(entity_id: str) -> str:
        response = service.generate_wrapped(entity_id)
        return response.to_json()
    
    # Create API endpoints
    connection_api = gr.Interface(
        fn=handle_connection,
        inputs=[
            gr.Textbox(label="Entity ID"),
            gr.Textbox(label="Redirect URL", placeholder="https://yourwebsite.com/connection/success")
        ],
        outputs=gr.JSON(),
        title="Initialize Calendar Connection",
        description="Start a new calendar connection for an entity",
        examples=[["user123", "https://example.com/callback"]]
    )
    
    status_api = gr.Interface(
        fn=check_status,
        inputs=gr.Textbox(label="Entity ID"),
        outputs=gr.JSON(),
        title="Check Connection Status",
        description="Check the status of an existing connection",
        examples=[["user123"]]
    )
    
    wrapped_api = gr.Interface(
        fn=generate_wrapped,
        inputs=gr.Textbox(label="Entity ID"),
        outputs=gr.JSON(),
        title="Generate Calendar Wrapped",
        description="Generate a calendar wrapped summary for an entity",
        examples=[["user123"]]
    )
    
    # Combine all interfaces
    api = gr.TabbedInterface(
        [connection_api, status_api, wrapped_api],
        ["Connect", "Check Status", "Generate Wrapped"],
        title="Calendar Wrapped API",
    )
    
    return api

if __name__ == "__main__":
    api = create_gradio_api()
    api.launch(server_name="0.0.0.0", server_port=7860)