Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ from typing import Optional, Dict, Any
|
|
4 |
from composio_llamaindex import ComposioToolSet, App, Action
|
5 |
from datetime import datetime, timedelta
|
6 |
from collections import defaultdict, Counter
|
|
|
7 |
import gradio as gr
|
8 |
import os
|
9 |
import json
|
@@ -12,6 +13,8 @@ from dotenv import load_dotenv
|
|
12 |
# Load environment variables
|
13 |
load_dotenv()
|
14 |
|
|
|
|
|
15 |
class ConnectionStatus(Enum):
|
16 |
PENDING = "pending"
|
17 |
ACTIVE = "active"
|
@@ -114,36 +117,80 @@ class CalendarService:
|
|
114 |
|
115 |
return stats
|
116 |
|
117 |
-
def
|
118 |
try:
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
entity_id=entity_id,
|
124 |
-
app=App.GOOGLECALENDAR
|
125 |
-
)
|
126 |
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
|
|
|
|
|
|
131 |
}
|
132 |
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
'redirect_url': connection_request.redirectUrl,
|
138 |
-
'wait_time': 60,
|
139 |
-
'message': "Please authenticate using the provided link."
|
140 |
-
}
|
141 |
)
|
142 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
except Exception as e:
|
144 |
return APIResponse(
|
145 |
success=False,
|
146 |
-
error=f"Failed to
|
147 |
)
|
148 |
|
149 |
def check_status(self, entity_id: str) -> APIResponse:
|
@@ -191,7 +238,7 @@ class CalendarService:
|
|
191 |
params=request_params,
|
192 |
entity_id=entity_id
|
193 |
)
|
194 |
-
|
195 |
if events_response["successfull"]:
|
196 |
stats = self.analyze_calendar_events(events_response)
|
197 |
return APIResponse(
|
|
|
4 |
from composio_llamaindex import ComposioToolSet, App, Action
|
5 |
from datetime import datetime, timedelta
|
6 |
from collections import defaultdict, Counter
|
7 |
+
from llama_index_llms_openai import OpenAI
|
8 |
import gradio as gr
|
9 |
import os
|
10 |
import json
|
|
|
13 |
# Load environment variables
|
14 |
load_dotenv()
|
15 |
|
16 |
+
llm = OpenAI(model='gpt-4o', api_key=os.getenv('OPENAI_API_KEY'))
|
17 |
+
|
18 |
class ConnectionStatus(Enum):
|
19 |
PENDING = "pending"
|
20 |
ACTIVE = "active"
|
|
|
117 |
|
118 |
return stats
|
119 |
|
120 |
+
def generate_wrapped(self, entity_id: str) -> APIResponse:
|
121 |
try:
|
122 |
+
# Get current year's start and end dates
|
123 |
+
current_year = datetime.now().year
|
124 |
+
time_min = f"{current_year},1,1,0,0,0"
|
125 |
+
time_max = f"{current_year},12,31,23,59,59"
|
|
|
|
|
|
|
126 |
|
127 |
+
request_params = {
|
128 |
+
"calendar_id": "primary",
|
129 |
+
"timeMin": time_min,
|
130 |
+
"timeMax": time_max,
|
131 |
+
"single_events": True,
|
132 |
+
"max_results": 2500,
|
133 |
+
"order_by": "startTime"
|
134 |
}
|
135 |
|
136 |
+
events_response = self.toolset.execute_action(
|
137 |
+
action=Action.GOOGLECALENDAR_FIND_EVENT,
|
138 |
+
params=request_params,
|
139 |
+
entity_id=entity_id
|
|
|
|
|
|
|
|
|
140 |
)
|
141 |
|
142 |
+
if events_response["successfull"]:
|
143 |
+
stats = self.analyze_calendar_events(events_response)
|
144 |
+
|
145 |
+
# Create a prompt for the LLM with the stats
|
146 |
+
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:
|
147 |
+
|
148 |
+
Stats:
|
149 |
+
- Total meetings this year: {stats['total_meetings_this_year']}
|
150 |
+
- Total time in meetings: {stats['total_time_spent']}
|
151 |
+
- Busiest month: {stats['busiest_month']}
|
152 |
+
- Busiest day: {stats['busiest_day']}
|
153 |
+
- Average meeting duration: {stats['average_meeting_duration']}
|
154 |
+
- Most common meeting time: {stats['most_common_meeting_time']}
|
155 |
+
- Most frequent collaborator: {stats['most_frequent_participant']}
|
156 |
+
|
157 |
+
Please provide:
|
158 |
+
1. Which tech billionaire's schedule this most resembles and why
|
159 |
+
2. A one-sentence comment for each of the above metrics
|
160 |
+
Format your response as JSON with keys: 'billionaire_match' and 'metric_comments'
|
161 |
+
|
162 |
+
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"""
|
163 |
+
|
164 |
+
llm_analysis = llm.complete(prompt)
|
165 |
+
|
166 |
+
try:
|
167 |
+
llm_json = json.loads(llm_analysis)
|
168 |
+
except json.JSONDecodeError:
|
169 |
+
llm_json = {
|
170 |
+
"billionaire_match": "Analysis unavailable",
|
171 |
+
"metric_comments": "Comments unavailable"
|
172 |
+
}
|
173 |
+
|
174 |
+
# Add LLM analysis to stats
|
175 |
+
stats.update({
|
176 |
+
"schedule_analysis": llm_json["billionaire_match"],
|
177 |
+
"metric_insights": llm_json["metric_comments"]
|
178 |
+
})
|
179 |
+
|
180 |
+
return APIResponse(
|
181 |
+
success=True,
|
182 |
+
data=stats
|
183 |
+
)
|
184 |
+
else:
|
185 |
+
return APIResponse(
|
186 |
+
success=False,
|
187 |
+
error=events_response["error"] or "Failed to fetch calendar events"
|
188 |
+
)
|
189 |
+
|
190 |
except Exception as e:
|
191 |
return APIResponse(
|
192 |
success=False,
|
193 |
+
error=f"Failed to generate wrapped: {str(e)}"
|
194 |
)
|
195 |
|
196 |
def check_status(self, entity_id: str) -> APIResponse:
|
|
|
238 |
params=request_params,
|
239 |
entity_id=entity_id
|
240 |
)
|
241 |
+
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??")
|
242 |
if events_response["successfull"]:
|
243 |
stats = self.analyze_calendar_events(events_response)
|
244 |
return APIResponse(
|