BroBro87 commited on
Commit
1597a23
·
verified ·
1 Parent(s): 103d454

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -28
app.py CHANGED
@@ -17,11 +17,14 @@ class CalendarWrappedAPI:
17
  self.llm = OpenAI(model="gpt-4", api_key=os.getenv('OPENAI_API_KEY'))
18
  self.connections = {}
19
 
20
- def initiate_connection(self, entity_id):
21
  """Initialize connection using entity_id (username)"""
22
-
 
 
23
  try:
24
  connection_request = self.toolset.initiate_connection(
 
25
  entity_id=entity_id,
26
  app=App.GOOGLECALENDAR
27
  )
@@ -31,24 +34,27 @@ class CalendarWrappedAPI:
31
  'status': connection_request.connectionStatus,
32
  'redirect_url': connection_request.redirectUrl
33
  }
34
- return {
35
- 'redirect_url':f'<a href="{connection_request.redirectUrl}" target="_blank">{connection_request.redirectUrl}</a>'
36
- }
37
  # Wait for random time between 60-100 seconds
38
  wait_time = random.randint(60, 100)
39
- print(f"Waiting for {wait_time} seconds before checking connection status...")
40
- time.sleep(wait_time)
41
 
 
 
 
 
 
 
 
42
  # Check final status after waiting
43
  final_status = 'active' # You would typically check the actual status here
44
  self.connections[entity_id]['status'] = final_status
45
 
46
  return {
47
- 'status': 'success',
48
- 'redirect_url': f'<a href="{connection_request.redirectUrl}" target="_blank">{connection_request.redirectUrl}</a>',
49
- 'connection_status': final_status,
50
- 'wait_time': wait_time
51
  }
 
52
  except Exception as e:
53
  return {
54
  'status': 'error',
@@ -58,12 +64,17 @@ class CalendarWrappedAPI:
58
  def check_connection_status(self, entity_id):
59
  """Check the connection status using entity_id"""
60
  if entity_id in self.connections:
61
- # Add a delay here too for subsequent status checks
62
  wait_time = random.randint(60, 100)
63
- print(f"Waiting for {wait_time} seconds before returning status...")
64
  time.sleep(wait_time)
65
- return self.connections[entity_id]['status']
66
- return 'not_found'
 
 
 
 
 
 
 
67
 
68
  def generate_wrapped(self, entity_id):
69
  """Generate Calendar Wrapped summary using entity_id"""
@@ -104,16 +115,17 @@ class CalendarWrappedAPI:
104
  def create_gradio_api():
105
  api = CalendarWrappedAPI()
106
 
107
- def handle_connection(entity_id):
108
- result = api.initiate_connection(entity_id)
109
- return {
110
- **result,
111
- 'message': f"Connection initiated. Waited for {result.get('wait_time', 0)} seconds."
112
- }
 
113
 
114
  def check_status(entity_id):
115
- status = api.check_connection_status(entity_id)
116
- return f"Status after waiting: {status}"
117
 
118
  def generate(entity_id):
119
  return api.generate_wrapped(entity_id)
@@ -121,7 +133,7 @@ def create_gradio_api():
121
  with gr.Blocks() as demo:
122
  gr.Markdown("""
123
  # Google Calendar Wrapped Generator
124
- **Note**: After initiating connection, check the connection status and generate your wrapped summary.
125
  """)
126
 
127
  with gr.Tab("Connection"):
@@ -129,13 +141,21 @@ def create_gradio_api():
129
  label="Entity ID (Username)",
130
  placeholder="Enter your username as entity ID"
131
  )
 
 
 
 
132
  connect_btn = gr.Button("Initialize Connection")
133
- connection_output = gr.HTML(label="Connection Status")
 
 
 
 
134
 
135
  connect_btn.click(
136
  fn=handle_connection,
137
- inputs=entity_id_input,
138
- outputs=connection_output
139
  )
140
 
141
  with gr.Tab("Status Check"):
@@ -170,4 +190,4 @@ def create_gradio_api():
170
 
171
  if __name__ == "__main__":
172
  demo = create_gradio_api()
173
- demo.launch(share=True)
 
17
  self.llm = OpenAI(model="gpt-4", api_key=os.getenv('OPENAI_API_KEY'))
18
  self.connections = {}
19
 
20
+ def initiate_connection(self, entity_id, redirect_url=None):
21
  """Initialize connection using entity_id (username)"""
22
+ if redirect_url is None:
23
+ redirect_url = "https://yourwebsite.com/connection/success"
24
+
25
  try:
26
  connection_request = self.toolset.initiate_connection(
27
+ redirect_url=redirect_url,
28
  entity_id=entity_id,
29
  app=App.GOOGLECALENDAR
30
  )
 
34
  'status': connection_request.connectionStatus,
35
  'redirect_url': connection_request.redirectUrl
36
  }
37
+
 
 
38
  # Wait for random time between 60-100 seconds
39
  wait_time = random.randint(60, 100)
 
 
40
 
41
+ # Return redirect URL and wait time immediately
42
+ initial_response = {
43
+ 'status': 'initiated',
44
+ 'redirect_url': connection_request.redirectUrl,
45
+ 'wait_time': wait_time,
46
+ 'message': f'Please click the link below to authenticate. Waiting {wait_time} seconds for completion...'
47
+ }
48
  # Check final status after waiting
49
  final_status = 'active' # You would typically check the actual status here
50
  self.connections[entity_id]['status'] = final_status
51
 
52
  return {
53
+ 'status': final_status,
54
+ 'redirect_url': connection_request.redirectUrl,
55
+ 'message': f'Authentication process completed after {wait_time} seconds. Status: {final_status}'
 
56
  }
57
+
58
  except Exception as e:
59
  return {
60
  'status': 'error',
 
64
  def check_connection_status(self, entity_id):
65
  """Check the connection status using entity_id"""
66
  if entity_id in self.connections:
 
67
  wait_time = random.randint(60, 100)
 
68
  time.sleep(wait_time)
69
+ return {
70
+ 'status': self.connections[entity_id]['status'],
71
+ 'wait_time': wait_time,
72
+ 'message': f'Status checked after {wait_time} seconds'
73
+ }
74
+ return {
75
+ 'status': 'not_found',
76
+ 'message': 'No connection found for this entity ID'
77
+ }
78
 
79
  def generate_wrapped(self, entity_id):
80
  """Generate Calendar Wrapped summary using entity_id"""
 
115
  def create_gradio_api():
116
  api = CalendarWrappedAPI()
117
 
118
+ def handle_connection(entity_id, redirect_url):
119
+ result = api.initiate_connection(entity_id, redirect_url)
120
+ return (
121
+ result['message'],
122
+ result['redirect_url'],
123
+ result.get('status', 'unknown')
124
+ )
125
 
126
  def check_status(entity_id):
127
+ result = api.check_connection_status(entity_id)
128
+ return f"Status: {result['status']}\n{result['message']}"
129
 
130
  def generate(entity_id):
131
  return api.generate_wrapped(entity_id)
 
133
  with gr.Blocks() as demo:
134
  gr.Markdown("""
135
  # Google Calendar Wrapped Generator
136
+ Connect your Google Calendar and generate your personalized wrapped summary.
137
  """)
138
 
139
  with gr.Tab("Connection"):
 
141
  label="Entity ID (Username)",
142
  placeholder="Enter your username as entity ID"
143
  )
144
+ redirect_url_input = gr.Textbox(
145
+ label="Redirect URL",
146
+ placeholder="https://yourwebsite.com/connection/success"
147
+ )
148
  connect_btn = gr.Button("Initialize Connection")
149
+
150
+ # New outputs for better visibility
151
+ status_message = gr.Textbox(label="Status Message", interactive=False)
152
+ redirect_link = gr.HTML(label="Authentication Link")
153
+ connection_status = gr.Textbox(label="Connection Status", interactive=False)
154
 
155
  connect_btn.click(
156
  fn=handle_connection,
157
+ inputs=[entity_id_input, redirect_url_input],
158
+ outputs=[status_message, redirect_link, connection_status]
159
  )
160
 
161
  with gr.Tab("Status Check"):
 
190
 
191
  if __name__ == "__main__":
192
  demo = create_gradio_api()
193
+ demo.launch(share=True) # Set share=False in production