File size: 50,019 Bytes
1fde903
b1cfe73
1fde903
6c19f60
1fde903
 
6c19f60
1fde903
 
 
 
 
 
6c19f60
b1cfe73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1fde903
 
 
6c19f60
b1cfe73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c19f60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1fde903
 
 
 
6c19f60
1fde903
6c19f60
1fde903
 
 
 
6c19f60
1fde903
6c19f60
1fde903
6c19f60
1fde903
 
 
 
 
 
 
 
6c19f60
1fde903
 
 
 
6c19f60
1fde903
 
6c19f60
1fde903
6c19f60
1fde903
 
6c19f60
1fde903
 
 
 
 
 
 
6c19f60
1fde903
 
6c19f60
1fde903
6c19f60
1fde903
 
 
6c19f60
1fde903
 
 
 
 
6c19f60
1fde903
6c19f60
1fde903
 
 
6c19f60
 
1fde903
6c19f60
1fde903
 
 
 
6c19f60
1fde903
6c19f60
1fde903
 
 
6c19f60
1fde903
 
6c19f60
1fde903
6c19f60
1fde903
 
 
 
 
 
6c19f60
1fde903
 
 
 
 
 
 
6c19f60
 
1fde903
6c19f60
 
1fde903
6c19f60
1fde903
 
 
 
6c19f60
1fde903
 
 
 
6c19f60
1fde903
 
 
 
6c19f60
1fde903
 
 
 
6c19f60
1fde903
 
 
 
6c19f60
1fde903
 
 
 
6c19f60
1fde903
 
 
 
6c19f60
1fde903
 
 
 
6c19f60
1fde903
 
 
 
6c19f60
1fde903
 
 
 
6c19f60
1fde903
 
 
 
6c19f60
1fde903
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c19f60
1fde903
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c19f60
1fde903
 
 
 
 
 
 
 
 
 
6c19f60
1fde903
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c19f60
1fde903
 
6c19f60
1fde903
 
 
 
 
 
 
 
 
6c19f60
1fde903
 
 
 
 
6c19f60
1fde903
 
 
6c19f60
1fde903
 
 
6c19f60
1fde903
 
6c19f60
1fde903
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c19f60
1fde903
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c19f60
1fde903
 
6c19f60
1fde903
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c19f60
1fde903
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c19f60
1fde903
 
 
 
 
 
 
6c19f60
1fde903
 
 
6c19f60
1fde903
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c19f60
1fde903
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c19f60
1fde903
 
 
 
 
 
 
 
 
 
 
 
 
6c19f60
1fde903
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c19f60
1fde903
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c19f60
1fde903
 
6c19f60
1fde903
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c19f60
1fde903
 
 
 
 
 
 
 
 
 
 
 
 
6c19f60
 
1fde903
 
 
 
 
 
 
6c19f60
 
1fde903
6c19f60
 
1fde903
 
6c19f60
1fde903
 
6c19f60
1fde903
 
 
6c19f60
1fde903
 
6c19f60
1fde903
 
 
6c19f60
 
1fde903
6c19f60
 
1fde903
6c19f60
1fde903
6c19f60
1fde903
 
6c19f60
1fde903
6c19f60
1fde903
 
 
 
 
6c19f60
1fde903
 
 
 
 
 
6c19f60
1fde903
 
 
 
 
 
 
 
 
 
6c19f60
1fde903
6c19f60
1fde903
 
6c19f60
1fde903
 
 
 
 
 
 
6c19f60
1fde903
 
 
 
6c19f60
1fde903
 
 
 
6c19f60
1fde903
 
 
6c19f60
 
1fde903
6c19f60
73b4d62
1fde903
73b4d62
1fde903
 
 
6c19f60
1fde903
 
 
 
 
6c19f60
1fde903
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c19f60
1fde903
 
 
6c19f60
1fde903
 
 
6c19f60
1fde903
 
 
6c19f60
1fde903
 
 
6c19f60
1fde903
6c19f60
1fde903
 
 
6c19f60
1fde903
6c19f60
 
1fde903
 
 
 
 
 
6c19f60
1fde903
 
 
 
6c19f60
1fde903
 
 
6c19f60
1fde903
 
6c19f60
1fde903
 
6c19f60
1fde903
 
 
 
6c19f60
1fde903
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c19f60
1fde903
 
6c19f60
1fde903
 
6c19f60
1fde903
 
 
6c19f60
1fde903
 
 
6c19f60
1fde903
 
 
 
 
 
 
 
 
 
 
 
6c19f60
1fde903
 
 
 
 
 
 
 
 
6c19f60
1fde903
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c19f60
1fde903
 
6c19f60
1fde903
 
6c19f60
1fde903
 
 
6c19f60
1fde903
 
6c19f60
1fde903
 
 
 
6c19f60
1fde903
 
6c19f60
1fde903
 
6c19f60
1fde903
 
 
6c19f60
1fde903
 
6c19f60
1fde903
 
6c19f60
1fde903
 
 
 
 
6c19f60
1fde903
 
 
 
73b4d62
1fde903
 
 
 
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
from fastapi import FastAPI, Request
import gradio as gr
import uvicorn

# Initialize FastAPI app
app = FastAPI()

# FastAPI route to handle WhatsApp webhook
@app.post("/whatsapp-webhook")
async def whatsapp_webhook(request: Request):
    data = await request.json()  # Parse incoming JSON data
    print(f"Received data: {data}")  # Log incoming data for debugging
    return {"status": "success", "received_data": data}

# Create a simple Gradio Blocks interface
def greet(name):
    return f"Hello, {name}!"

with gr.Blocks() as demo:
    gr.Markdown("### Greeting App")
    name_input = gr.Textbox(placeholder="Enter your name")
    greet_button = gr.Button("Greet")
    output_text = gr.Textbox(label="Greeting")

    greet_button.click(fn=greet, inputs=name_input, outputs=output_text)

# Mount the Gradio app at "/gradio"
app.mount("/gradio", demo)

# Run the FastAPI app with Uvicorn
if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)

# from fastapi import FastAPI, Request
# import uvicorn

# # Initialize FastAPI app
# app = FastAPI()

# # FastAPI route to handle WhatsApp webhook
# @app.post("/whatsapp-webhook")
# async def whatsapp_webhook(request: Request):
#     data = await request.json()  # Parse incoming JSON data
#     print(f"Received data: {data}")  # Log incoming data for debugging
#     return {"status": "success", "received_data": data}

# # Run the FastAPI app with Uvicorn
# if __name__ == "__main__":
#     uvicorn.run(app, host="0.0.0.0", port=7860)

#!/usr/bin/env python
# coding: utf-8


# In[2]:


#pip install  evernote-sdk-python3
# import evernote.edam.notestore.NoteStore as NoteStore
# import evernote.edam.type.ttypes as Types
# from evernote.api.client import EvernoteClient


# In[3]:


# import os
# import yaml
# import pandas as pd
# import numpy as np

# from datetime import datetime, timedelta

# # perspective generation
# import openai
# import os
# from openai import OpenAI

# import gradio as gr

# import json

# import sqlite3
# import uuid
# import socket
# import difflib
# import time
# import shutil
# import requests
# import re

# import json
# import markdown
# from fpdf import FPDF
# import hashlib

# from transformers import pipeline
# from transformers.pipelines.audio_utils import ffmpeg_read

# from todoist_api_python.api import TodoistAPI

# # from flask import Flask, request, jsonify
# from twilio.rest import Client

# import asyncio
# import uvicorn
# import fastapi
# from fastapi import FastAPI, Request, HTTPException
# from fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse
# from fastapi.staticfiles import StaticFiles
# from pathlib import Path

# import nest_asyncio
# from twilio.twiml.messaging_response import MessagingResponse

# from requests.auth import HTTPBasicAuth

# from google.cloud import storage, exceptions  # Import exceptions for error handling
# from google.cloud.exceptions import NotFound
# from google.oauth2 import service_account

# from reportlab.pdfgen import canvas
# from reportlab.lib.pagesizes import letter
# from reportlab.pdfbase import pdfmetrics
# from reportlab.lib import colors
# from reportlab.pdfbase.ttfonts import TTFont

# import logging

# # Configure logging
# logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s")
# logger = logging.getLogger(__name__)


# # In[4]:

# # Access the API keys and other configuration data
# openai_api_key = os.environ["OPENAI_API_KEY"]
# # Access the API keys and other configuration data
# todoist_api_key = os.environ["TODOIST_API_KEY"]

# EVERNOTE_API_TOKEN = os.environ["EVERNOTE_API_TOKEN"]

# account_sid = os.environ["TWILLO_ACCOUNT_SID"]
# auth_token = os.environ["TWILLO_AUTH_TOKEN"]
# twilio_phone_number = os.environ["TWILLO_PHONE_NUMBER"]

# google_credentials_json = os.environ["GOOGLE_APPLICATION_CREDENTIALS"]
# twillo_client = Client(account_sid, auth_token)

# # Set the GOOGLE_APPLICATION_CREDENTIALS environment variable

# # Load Reasoning Graph JSON File
# def load_reasoning_json(filepath):
#     """Load JSON file and return the dictionary."""
#     with open(filepath, "r") as file:
#         data = json.load(file)
#     return data

# # Load Action Map
# def load_action_map(filepath):
#     """Load action map JSON file and map strings to actual function objects."""
#     with open(filepath, "r") as file:
#         action_map_raw = json.load(file)
#     # Map string names to actual functions using globals()
#     return {action: globals()[func_name] for action, func_name in action_map_raw.items()}


# # In[5]:


# # Define all actions as functions

# def find_reference(task_topic):
#     """Finds a reference related to the task topic."""
#     print(f"Finding reference for topic: {task_topic}")
#     return f"Reference found for topic: {task_topic}"

# def generate_summary(reference):
#     """Generates a summary of the reference."""
#     print(f"Generating summary for reference: {reference}")
#     return f"Summary of {reference}"

# def suggest_relevance(summary):
#     """Suggests how the summary relates to the project."""
#     print(f"Suggesting relevance of summary: {summary}")
#     return f"Relevance of {summary} suggested"

# def tool_research(task_topic):
#     """Performs tool research and returns analysis."""
#     print("Performing tool research")
#     return "Tool analysis data"

# def generate_comparison_table(tool_analysis):
#     """Generates a comparison table for a competitive tool."""
#     print(f"Generating comparison table for analysis: {tool_analysis}")
#     return f"Comparison table for {tool_analysis}"

# def generate_integration_memo(tool_analysis):
#     """Generates an integration memo for a tool."""
#     print(f"Generating integration memo for analysis: {tool_analysis}")
#     return f"Integration memo for {tool_analysis}"

# def analyze_issue(task_topic):
#     """Analyzes an issue and returns the analysis."""
#     print("Analyzing issue")
#     return "Issue analysis data"

# def generate_issue_memo(issue_analysis):
#     """Generates an issue memo based on the analysis."""
#     print(f"Generating issue memo for analysis: {issue_analysis}")
#     return f"Issue memo for {issue_analysis}"

# def list_ideas(task_topic):
#     """Lists potential ideas for brainstorming."""
#     print("Listing ideas")
#     return ["Idea 1", "Idea 2", "Idea 3"]

# def construct_matrix(ideas):
#     """Constructs a matrix (e.g., feasibility or impact/effort) for the ideas."""
#     print(f"Constructing matrix for ideas: {ideas}")
#     return {"Idea 1": "High Impact/Low Effort", "Idea 2": "Low Impact/High Effort", "Idea 3": "High Impact/High Effort"}

# def prioritize_ideas(matrix):
#     """Prioritizes ideas based on the matrix."""
#     print(f"Prioritizing ideas based on matrix: {matrix}")
#     return ["Idea 3", "Idea 1", "Idea 2"]

# def setup_action_plan(prioritized_ideas):
#     """Sets up an action plan based on the prioritized ideas."""
#     print(f"Setting up action plan for ideas: {prioritized_ideas}")
#     return f"Action plan created for {prioritized_ideas}"

# def unsupported_task(task_topic):
#     """Handles unsupported tasks."""
#     print("Task not supported")
#     return "Unsupported task"


# # In[6]:


# todoist_api = TodoistAPI(todoist_api_key)

# # Fetch recent Todoist task
# def fetch_todoist_task():
#     try:
#         tasks = todoist_api.get_tasks()
#         if tasks:
#             recent_task = tasks[0]  # Fetch the most recent task
#             return f"Recent Task: {recent_task.content}"
#         return "No tasks found in Todoist."
#     except Exception as e:
#         return f"Error fetching tasks: {str(e)}"

# def add_to_todoist(task_topic, todoist_priority = 3):
#     try:
#         # Create a task in Todoist using the Todoist API
#         # Assuming you have a function `todoist_api.add_task()` that handles the API request
#         todoist_api.add_task(
#             content=task_topic,
#             priority=todoist_priority
#         )
#         msg = f"Task added: {task_topic} with priority {todoist_priority}"
#         logger.debug(msg)

#         return msg
#     except Exception as e:
#         # Return an error message if something goes wrong
#         return f"An error occurred: {e}"    

# # def save_todo(reasoning_steps):
# #     """
# #     Save reasoning steps to Todoist as tasks.

# #     Args:
# #         reasoning_steps (list of dict): A list of steps with "step" and "priority" keys.
# #     """
# #     try:
# #         # Validate that reasoning_steps is a list
# #         if not isinstance(reasoning_steps, list):
# #             raise ValueError("The input reasoning_steps must be a list.")

# #         # Iterate over the reasoning steps
# #         for step in reasoning_steps:
# #             # Ensure each step is a dictionary and contains required keys
# #             if not isinstance(step, dict) or "step" not in step or "priority" not in step:
# #                 logger.error(f"Invalid step data: {step}, skipping.")
# #                 continue

# #             task_content = step["step"]
# #             priority_level = step["priority"]

# #             # Map priority to Todoist's priority levels (1 - low, 4 - high)
# #             priority_mapping = {"Low": 1, "Medium": 2, "High": 4}
# #             todoist_priority = priority_mapping.get(priority_level, 1)  # Default to low if not found

# #             # Create a task in Todoist using the Todoist API
# #             # Assuming you have a function `todoist_api.add_task()` that handles the API request
# #             todoist_api.add_task(
# #                 content=task_content,
# #                 priority=todoist_priority
# #             )

# #             logger.debug(f"Task added: {task_content} with priority {priority_level}")

# #         return "All tasks processed."
# #     except Exception as e:
# #         # Return an error message if something goes wrong
# #         return f"An error occurred: {e}"


# # In[7]:


# # evernote_client = EvernoteClient(token=EVERNOTE_API_TOKEN, sandbox=False)
# # note_store = evernote_client.get_note_store()

# # def add_to_evernote(task_topic, notebook_title="Inspirations"):
# #     """
# #     Add a task topic to the 'Inspirations' notebook in Evernote. If the notebook doesn't exist, create it.

# #     Args:
# #         task_topic (str): The content of the task to be added.
# #         notebook_title (str): The title of the Evernote notebook. Default is 'Inspirations'.
# #     """
# #     try:
# #         # Check if the notebook exists
# #         notebooks = note_store.listNotebooks()
# #         notebook = next((nb for nb in notebooks if nb.name == notebook_title), None)

# #         # If the notebook doesn't exist, create it
# #         if not notebook:
# #             notebook = Types.Notebook()
# #             notebook.name = notebook_title
# #             notebook = note_store.createNotebook(notebook)

# #         # Search for an existing note with the same title
# #         filter = NoteStore.NoteFilter()
# #         filter.notebookGuid = notebook.guid
# #         filter.words = notebook_title
# #         notes_metadata_result = note_store.findNotesMetadata(filter, 0, 1, NoteStore.NotesMetadataResultSpec(includeTitle=True))

# #         # If a note with the title exists, append to it; otherwise, create a new note
# #         if notes_metadata_result.notes:
# #             note_guid = notes_metadata_result.notes[0].guid
# #             existing_note = note_store.getNote(note_guid, True, False, False, False)
# #             existing_note.content = existing_note.content.replace("</en-note>", f"<div>{task_topic}</div></en-note>")
# #             note_store.updateNote(existing_note)
# #         else:
# #             # Create a new note
# #             note = Types.Note()
# #             note.title = notebook_title
# #             note.notebookGuid = notebook.guid
# #             note.content = f'<?xml version="1.0" encoding="UTF-8"?>' \
# #                            f'<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">' \
# #                            f'<en-note><div>{task_topic}</div></en-note>'
# #             note_store.createNote(note)

# #         print(f"Task '{task_topic}' successfully added to Evernote under '{notebook_title}'.")
# #     except Exception as e:
# #         print(f"Error adding task to Evernote: {e}")

# # Mock Functions for Task Actions
# def add_to_evernote(task_topic):
#     return f"Task added to Evernote with title '{task_topic}'."


# # In[8]:


# # Access the API keys and other configuration data
# TASK_WORKFLOW_TREE = load_reasoning_json('curify_ideas_reasoning.json')
# action_map = load_action_map('action_map.json')

# # In[9]:


# def generate_task_hash(task_description):
#     try:
#         # Ensure task_description is a string
#         if not isinstance(task_description, str):
#             logger.warning("task_description is not a string, attempting conversion.")
#             task_description = str(task_description)
        
#         # Safely encode with UTF-8 and ignore errors
#         encoded_description = task_description.encode("utf-8", errors="ignore")
#         task_hash = hashlib.md5(encoded_description).hexdigest()

#         logger.debug(f"Generated task hash: {task_hash}")
#         return task_hash
#     except Exception as e:
#         # Log any unexpected issues
#         logger.error(f"Error generating task hash: {e}", exc_info=True)
#         return 'output'
    
# def save_to_google_storage(bucket_name, file_path, destination_blob_name, expiration_minutes = 1440):
#     credentials_dict = json.loads(google_credentials_json)

#     # Step 3: Use `service_account.Credentials.from_service_account_info` to authenticate directly with the JSON
#     credentials = service_account.Credentials.from_service_account_info(credentials_dict)
#     gcs_client = storage.Client(credentials=credentials, project=credentials.project_id)

#     # Check if the bucket exists; if not, create it
#     try:
#         bucket = gcs_client.get_bucket(bucket_name)
#     except NotFound:
#         print(f"❌ Bucket '{bucket_name}' not found. Please check the bucket name.")
#         bucket = gcs_client.create_bucket(bucket_name)
#         print(f"✅ Bucket '{bucket_name}' created.")
#     except Exception as e:
#         print(f"❌ An unexpected error occurred: {e}")
#         raise
#     # Get a reference to the blob
#     blob = bucket.blob(destination_blob_name)
    
#     # Upload the file
#     blob.upload_from_filename(file_path)
    
#     # Generate a signed URL for the file
#     signed_url = blob.generate_signed_url(
#         version="v4",
#         expiration=timedelta(minutes=expiration_minutes),
#         method="GET"
#     )
#     print(f"✅ File uploaded to Google Cloud Storage. Signed URL: {signed_url}")
#     return signed_url


# # Function to check if content is Simplified Chinese
# def is_simplified(text):
#     simplified_range = re.compile('[\u4e00-\u9fff]')  # Han characters in general
#     simplified_characters = [char for char in text if simplified_range.match(char)]
#     return len(simplified_characters) > len(text) * 0.5  # Threshold of 50% to be considered simplified

# # Function to choose the appropriate font for the content
# def choose_font_for_content(content):
#     return 'NotoSansSC' if is_simplified(content) else 'NotoSansTC'

# # Function to generate and save a document using ReportLab
# def generate_document(task_description, md_content, user_name='jayw', bucket_name='curify'):
#     logger.debug("Starting to generate document")

#     # Hash the task description to generate a unique filename
#     task_hash = generate_task_hash(task_description)

#     # Truncate the hash if needed (64 characters is sufficient for uniqueness)
#     max_hash_length = 64  # Adjust if needed
#     truncated_hash = task_hash[:max_hash_length]

#     # Generate PDF file locally
#     local_filename = f"{truncated_hash}.pdf"  # Use the truncated hash as the local file name
#     c = canvas.Canvas(local_filename, pagesize=letter)

#     # Paths to the TTF fonts for Simplified and Traditional Chinese
#     sc_font_path = 'NotoSansSC-Regular.ttf'  # Path to Simplified Chinese font
#     tc_font_path = 'NotoSansTC-Regular.ttf'  # Path to Traditional Chinese font

#     try:
#         # Register the Simplified Chinese font
#         sc_font = TTFont('NotoSansSC', sc_font_path)
#         pdfmetrics.registerFont(sc_font)

#         # Register the Traditional Chinese font
#         tc_font = TTFont('NotoSansTC', tc_font_path)
#         pdfmetrics.registerFont(tc_font)
        
#         # Set default font (Simplified Chinese or Traditional Chinese depending on content)
#         c.setFont('NotoSansSC', 12)
#     except Exception as e:
#         logger.error(f"Error loading font files: {e}")
#         raise RuntimeError("Failed to load one or more fonts. Ensure the font files are accessible.")

#     # Set initial Y position for drawing text
#     y_position = 750  # Starting position for text

#     # Process dictionary and render content
#     for key, value in md_content.items():
#         # Choose the font based on the key (header)
#         c.setFont(choose_font_for_content(key), 14)
#         c.drawString(100, y_position, f"# {key}")
#         y_position -= 20

#         # Choose the font for the value
#         c.setFont(choose_font_for_content(str(value)), 12)

#         # Add value
#         if isinstance(value, list):  # Handle lists
#             for item in value:
#                 c.drawString(100, y_position, f"- {item}")
#                 y_position -= 15
#         else:  # Handle single strings
#             c.drawString(100, y_position, value)
#             y_position -= 15

#         # Check if the page needs to be broken (if Y position is too low)
#         if y_position < 100:
#             c.showPage()  # Create a new page
#             c.setFont('NotoSansSC', 12)  # Reset font
#             y_position = 750  # Reset the Y position for the new page

#     # Save the PDF
#     c.save()

#     # Organize files into user-specific folders
#     destination_blob_name = f"{user_name}/{truncated_hash}.pdf"

#     # Upload to Google Cloud Storage and get the public URL
#     public_url = save_to_google_storage(bucket_name, local_filename, destination_blob_name)
#     logger.debug("Finished generating document")
#     return public_url
    
# # In[10]:


# def execute_with_retry(sql, params=(), attempts=5, delay=1, db_name = 'curify_ideas.db'):
#     for attempt in range(attempts):
#         try:
#             with sqlite3.connect(db_name) as conn:
#                 cursor = conn.cursor()
#                 cursor.execute(sql, params)
#                 conn.commit()
#                 break
#         except sqlite3.OperationalError as e:
#             if "database is locked" in str(e) and attempt < attempts - 1:
#                 time.sleep(delay)
#             else:
#                 raise e

# # def enable_wal_mode(db_name = 'curify_ideas.db'):
# #     with sqlite3.connect(db_name) as conn:
# #         cursor = conn.cursor()
# #         cursor.execute("PRAGMA journal_mode=WAL;")
# #         conn.commit()

# # # Create SQLite DB and table
# # def create_db(db_name = 'curify_ideas.db'):
# #     with sqlite3.connect(db_name, timeout=30) as conn:
# #         c = conn.cursor()
# #         c.execute('''CREATE TABLE IF NOT EXISTS sessions (
# #             session_id TEXT,
# #             ip_address TEXT,
# #             project_desc TEXT,
# #             idea_desc TEXT,
# #             idea_analysis TEXT,
# #             prioritization_steps TEXT,
# #             timestamp DATETIME,
# #             PRIMARY KEY (session_id, timestamp)
# #             )
# #         ''')
# #         conn.commit()

# # # Function to insert session data into the SQLite database
# # def insert_session_data(session_id, ip_address, project_desc, idea_desc, idea_analysis, prioritization_steps, db_name = 'curify_ideas.db'):
# #     execute_with_retry('''
# #         INSERT INTO sessions (session_id, ip_address, project_desc, idea_desc, idea_analysis, prioritization_steps, timestamp)
# #         VALUES (?, ?, ?, ?, ?, ?, ?)
# #     ''', (session_id, ip_address, project_desc, idea_desc, json.dumps(idea_analysis), json.dumps(prioritization_steps), datetime.now()), db_name)


# # In[11]:


# def convert_to_listed_json(input_string):
#     """
#     Converts a string to a listed JSON object.
    
#     Parameters:
#         input_string (str): The JSON-like string to be converted.
    
#     Returns:
#         list: A JSON object parsed into a Python list of dictionaries.
#     """
#     try:
#         # Parse the string into a Python object
#         trimmed_string = input_string[input_string.index('['):input_string.rindex(']') + 1]

#         json_object = json.loads(trimmed_string)
#         return json_object
#     except json.JSONDecodeError as e:
#         return None
#     return None
#     #raise ValueError(f"Invalid JSON format: {e}")

# def validate_and_extract_json(json_string):
#     """
#     Validates the JSON string, extracts fields with possible variants using fuzzy matching.
    
#     Args:
#     - json_string (str): The JSON string to validate and extract from.
#     - field_names (list): List of field names to extract, with possible variants.
    
#     Returns:
#     - dict: Extracted values with the best matched field names.
#     """
#     # Try to parse the JSON string
#     trimmed_string = json_string[json_string.index('{'):json_string.rindex('}') + 1]
#     try:
#         parsed_json = json.loads(trimmed_string)
#         return parsed_json
#     except json.JSONDecodeError as e:
#         return None

#     # {"error": "Parsed JSON is not a dictionary."}
#     return None

# def json_to_pandas(dat_json, dat_schema = {'name':"", 'description':""}):
#     dat_df = pd.DataFrame([dat_schema])
#     try:
#         dat_df = pd.DataFrame(dat_json)

#     except Exception as e:
#         dat_df = pd.DataFrame([dat_schema])
#     # ValueError(f"Failed to parse LLM output as JSON: {e}\nOutput: {res}")
#     return dat_df


# # In[12]:


# client = OpenAI(
#     api_key= os.environ.get("OPENAI_API_KEY"),  # This is the default and can be omitted
# )

# # Function to call OpenAI API with compact error handling
# def call_openai_api(prompt, model="gpt-4o", max_tokens=5000, retries=3, backoff_factor=2):
#     """
#     Send a prompt to the OpenAI API and handle potential errors robustly.

#     Parameters:
#         prompt (str): The user input or task prompt to send to the model.
#         model (str): The OpenAI model to use (default is "gpt-4").
#         max_tokens (int): The maximum number of tokens in the response.
#         retries (int): Number of retry attempts in case of transient errors.
#         backoff_factor (int): Backoff time multiplier for retries.

#     Returns:
#         str: The model's response content if successful.
#     """
#     for attempt in range(1, retries + 1):
#         try:
#             response = client.chat.completions.create(
#             model="gpt-4o",
#             messages=[{"role": "user", "content": prompt}],
#             max_tokens=5000,
#             )        
#             return response.choices[0].message.content.strip()
        
#         except (openai.RateLimitError, openai.APIConnectionError) as e:
#             logging.warning(f"Transient error: {e}. Attempt {attempt} of {retries}. Retrying...")
#         except (openai.BadRequestError, openai.AuthenticationError) as e:
#             logging.error(f"Unrecoverable error: {e}. Check your inputs or API key.")
#             break
#         except Exception as e:
#             logging.error(f"Unexpected error: {e}. Attempt {attempt} of {retries}. Retrying...")
        
#         # Exponential backoff before retrying
#         if attempt < retries:
#             time.sleep(backoff_factor * attempt)
    
#     raise RuntimeError(f"Failed to fetch response from OpenAI API after {retries} attempts.")

# def fn_analyze_task(project_context, task_description):
#     prompt = (
#             f"You are working in the context of {project_context}. "
#             f"Your task is to analyze the task: {task_description} "
#             "Please analyze the following aspects: "
#             "1) Determine which project this item belongs to. If the idea does not belong to any existing project, categorize it under 'Other'. "
#             "2) Assess whether this idea can be treated as a concrete task. "
#             "3) Evaluate whether a document can be generated as an intermediate result. "
#             "4) Identify the appropriate category of the task. Possible categories are: 'Blogs/Papers', 'Tools', 'Brainstorming', 'Issues', and 'Others'. "
#             "5) Extract the topic of the task. "
#             "Please provide the output in JSON format using the structure below: "
#             "{"
#             "  \"description\": \"\", "
#             "  \"project_association\": \"\", "
#             "  \"is_task\": \"Yes/No\", "
#             "  \"is_document\": \"Yes/No\", "
#             "  \"task_category\": \"\", "
#             "  \"task_topic\": \"\" "
#             "}"
#         )
#     res_task_analysis = call_openai_api(prompt)

#     try:
#         json_task_analysis = validate_and_extract_json(res_task_analysis)

#         return json_task_analysis
#     except ValueError as e:
#         logger.debug("ValueError occurred: %s", str(e), exc_info=True)  # Log the exception details
#         return None


# # In[13]:

# # Recursive Task Executor
# def fn_process_task(project_desc_table, task_description, bucket_name='curify'):
    
#     project_context = project_desc_table.to_string(index=False)
#     task_analysis = fn_analyze_task(project_context, task_description)

#     if task_analysis:
#         execution_status = []
#         execution_results = task_analysis.copy()
#         execution_results['deliverables'] = ''

#         def traverse(node, previous_output=None):
#             if not node:  # If the node is None or invalid
#                 return  # Exit if the node is invalid

#             # Check if there is a condition to evaluate
#             if "check" in node:
#                 # Safely attempt to retrieve the value from execution_results
#                 if node["check"] in execution_results:
#                     value = execution_results[node["check"]]  # Evaluate the check condition
#                     traverse(node.get(value, node.get("default")), previous_output)
#                 else:
#                     # Log an error and exit, but keep partial results
#                     logger.error(f"Key '{node['check']}' not found in execution_results.")
#                     return
        
#             # If the node contains an action
#             elif "action" in node:
#                 action_name = node["action"]
#                 input_key = node.get("input", 'task_topic')

#                 if input_key in execution_results.keys():
#                     inputs = {input_key: execution_results[input_key]}
#                 else:
#                     # Log an error and exit, but keep partial results
#                     logger.error(f"Workflow action {action_name} input key {input_key} not in execution_results.")
#                     return

#                 logger.debug(f"Executing: {action_name} with inputs: {inputs}")
                
#                 # Execute the action function
#                 action_func = action_map.get(action_name, unsupported_task)
#                 try:
#                     output = action_func(**inputs)
#                 except Exception as e:
#                     # Handle action function failure
#                     logger.error(f"Error executing action '{action_name}': {e}")
#                     return

#                 # Store execution results or append to previous outputs
#                 execution_status.append({"action": action_name, "output": output})

#                 # Check if 'output' field exists in the node
#                 if 'output' in node:
#                     # If 'output' exists, assign the output to execution_results with the key from node['output']
#                     execution_results[node['output']] = output
#                 else:
#                     # If 'output' does not exist, append the output to 'deliverables'
#                     execution_results['deliverables'] += output
                
#                 # Traverse to the next node, if it exists
#                 if "next" in node and node["next"]:
#                     traverse(node["next"], previous_output)

#         try:
#             traverse(TASK_WORKFLOW_TREE["start"])
#             execution_results['doc_url'] = generate_document(task_description, execution_results)
#         except Exception as e:
#             logger.error(f"Traverse Error: {e}")
#         finally:
#             # Always return partial results, even if an error occurs
#             return task_analysis, pd.DataFrame(execution_status), execution_results
#     else:
#         logger.error("Empty task analysis.")
#         return {}, pd.DataFrame(), {}

# # In[14]:


# # Initialize dataframes for the schema
# ideas_df = pd.DataFrame(columns=["Idea ID", "Content", "Tags"])

# def extract_ideas(context, text):
#     """
#     Extract project ideas from text, with or without a context, and return in JSON format.

#     Parameters:
#         context (str): Context of the extraction. Can be empty.
#         text (str): Text to extract ideas from.

#     Returns:
#         list: A list of ideas, each represented as a dictionary with name and description.
#     """
#     if context:
#         # Template when context is provided
#         prompt = (
#             f"You are working in the context of {context}. "
#             "Please extract the ongoing projects with project name and description."
#             "Please only the listed JSON as output string."
#             f"Ongoing projects: {text}"
#         )
#     else:
#         # Template when context is not provided
#         prompt = (
#             "Given the following information about the user."
#             "Please extract the ongoing projects with project name and description."
#             "Please only the listed JSON as output string."
#             f"Ongoing projects: {text}"
#         )

#     # return the raw string
#     return call_openai_api(prompt)

# def df_to_string(df, empty_message = ''):
#     """
#     Converts a DataFrame to a string if it is not empty. 
#     If the DataFrame is empty, returns an empty string.
    
#     Parameters:
#         ideas_df (pd.DataFrame): The DataFrame to be converted.
    
#     Returns:
#         str: A string representation of the DataFrame or an empty string.
#     """
#     if df.empty:
#         return empty_message
#     else:
#         return df.to_string(index=False)


# # In[15]:


# # Shared state variables
# shared_state = {"project_desc_table": pd.DataFrame(), "task_analysis_txt": "", "execution_status": pd.DataFrame(), "execution_results": {}}

# # Button Action: Fetch State
# def fetch_updated_state():
#     # Iterating and logging the shared state
#     for key, value in shared_state.items():
#         if isinstance(value, pd.DataFrame):
#             logger.debug(f"{key}: DataFrame:\n{value.to_string()}")
#         elif isinstance(value, dict):
#             logger.debug(f"{key}: Dictionary: {value}")
#         elif isinstance(value, str):
#             logger.debug(f"{key}: String: {value}")
#         else:
#             logger.debug(f"{key}: Unsupported type: {value}")
#     return shared_state['project_desc_table'], shared_state['task_analysis_txt'], shared_state['execution_status'], shared_state['execution_results']
    
#     # response = requests.get("http://localhost:5000/state")
#     # # Check the status code and the raw response
#     # if response.status_code == 200:
#     #     try:
#     #         state = response.json()  # Try to parse JSON
#     #         return pd.DataFrame(state["project_desc_table"]), state["task_analysis_txt"], pd.DataFrame(state["execution_status"]), state["execution_results"]
#     #     except ValueError as e:
#     #         logger.error(f"JSON decoding failed: {e}")
#     #         logger.debug("Raw response body:", response.text)
#     # else:
#     #     logger.error(f"Error: {response.status_code} - {response.text}")
#     # """Fetch the updated shared state from FastAPI."""
#     # return pd.DataFrame(), "", pd.DataFrame(), {}
    

# def update_gradio_state(project_desc_table, task_analysis_txt, execution_status, execution_results):
#     # You can update specific components like Textbox or State
#     shared_state['project_desc_table'] = project_desc_table
#     shared_state['task_analysis_txt'] = task_analysis_txt
#     shared_state['execution_status'] = execution_status
#     shared_state['execution_results'] = execution_results
#     return True


# # In[16]:


# # # Initialize the database
# # new_db = 'curify.db'

# # # Copy the old database to a new one
# # shutil.copy("curify_idea.db", new_db)

# #create_db(new_db)
# #enable_wal_mode(new_db)
# def project_extraction(project_description):

#     str_projects = extract_ideas('AI-powered tools for productivity', project_description)
#     json_projects = convert_to_listed_json(str_projects)

#     project_desc_table = json_to_pandas(json_projects)
#     update_gradio_state(project_desc_table, "", pd.DataFrame(), {})
#     return project_desc_table


# # In[17]:


# # project_description = 'work on a number of projects including curify (digest, ideas, careers, projects etc), and writing a book on LLM for recommendation system, educating my 3.5-year-old boy and working on a paper for LLM reasoning.'

# # # convert_to_listed_json(extract_ideas('AI-powered tools for productivity', project_description))

# # task_description = 'Build an interview bot for the curify digest project.'
# # task_analysis, reasoning_path = generate_reasoning_path(project_description, task_description)

# # steps = store_and_execute_task(task_description, reasoning_path)

# def message_back(task_message, execution_status, doc_url, from_whatsapp):
#     # Convert task steps to a simple numbered list
#     task_steps_list = "\n".join(
#         [f"{i + 1}. {step['action']} - {step.get('output', '')}" for i, step in enumerate(execution_status.to_dict(orient="records"))]
#     )

#     # Format the body message
#     body_message = (
#         f"*Task Message:*\n{task_message}\n\n"
#         f"*Execution Status:*\n{task_steps_list}\n\n"
#         f"*Doc URL:*\n{doc_url}\n\n"
#     )

#     # Send response back to WhatsApp
#     try:
#         twillo_client.messages.create(
#             from_=twilio_phone_number,
#             to=from_whatsapp,
#             body=body_message
#         )
#     except Exception as e:
#         logger.error(f"Twilio Error: {e}")
#         raise HTTPException(status_code=500, detail=f"Error sending WhatsApp message: {str(e)}")

#     return {"status": "success"}

# # Initialize the Whisper pipeline
# whisper_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-medium")

# # Function to transcribe audio from a media URL
# def transcribe_audio_from_media_url(media_url):
#     try:
#         media_response = requests.get(media_url, auth=HTTPBasicAuth(account_sid, auth_token))
#         # Download the media file
#         media_response.raise_for_status()
#         audio_data = media_response.content

#         # Save the audio data to a file for processing
#         audio_file_path = "temp_audio_file.mp3"
#         with open(audio_file_path, "wb") as audio_file:
#             audio_file.write(audio_data)

#         # Transcribe the audio using Whisper
#         transcription = whisper_pipeline(audio_file_path, return_timestamps=True)
#         logger.debug(f"Transcription: {transcription['text']}")
#         return transcription["text"]

#     except Exception as e:
#         logger.error(f"An error occurred: {e}")
#         return None


# # In[18]:


# app = FastAPI()

# @app.get("/state")
# async def fetch_state():
#     return shared_state

# @app.route("/whatsapp-webhook/", methods=["POST"])
# async def whatsapp_webhook(request: Request):
#     form_data = await request.form()
#     # Log the form data to debug
#     print("Received data:", form_data)
    
#     # Extract message and user information
#     incoming_msg = form_data.get("Body", "").strip()
#     from_number = form_data.get("From", "")
#     media_url = form_data.get("MediaUrl0", "")
#     media_type = form_data.get("MediaContentType0", "")

#     # Initialize response variables
#     transcription = None

#     if media_type.startswith("audio"):
#         # If the media is an audio or video file, process it
#         try:
#             transcription = transcribe_audio_from_media_url(media_url)
#         except Exception as e:
#             return JSONResponse(
#                 {"error": f"Failed to process voice input: {str(e)}"}, status_code=500
#             )
#     # Determine message content: use transcription if available, otherwise use text message
#     processed_input = transcription if transcription else incoming_msg

#     logger.debug(f"Processed input: {processed_input}")

#     try:
#         # Generate response
#         project_desc_table, _ = fetch_updated_state()
        
#         # If the project_desc_table is empty, return an empty JSON response
#         if project_desc_table.empty:
#             return JSONResponse(content={})  # Returning an empty JSON object
    
#         # Continue processing if the table is not empty
#         task_analysis_txt, execution_status, execution_results = fn_process_task(project_desc_table, processed_input)
#         update_gradio_state(task_analysis_txt, execution_status, execution_results)
    
#         doc_url = 'Fail to generate doc'
#         if 'doc_url' in execution_results:
#             doc_url = execution_results['doc_url']
    
#         # Respond to the user on WhatsApp with the processed idea
#         response = message_back(processed_input, execution_status, doc_url, from_number)
#         logger.debug(response)
    
#         return JSONResponse(content=str(response))
    
#     except Exception as e:
#         logger.error(f"Error during task processing: {e}")
#         return JSONResponse(content={"error": str(e)}, status_code=500)

# # In[19]:


# # Mock Gmail Login Function
# def mock_login(email):
#     if email.endswith("@gmail.com"):
#         return f"✅ Logged in as {email}", gr.update(visible=False), gr.update(visible=True)
#     else:
#         return "❌ Invalid Gmail address. Please try again.", gr.update(), gr.update()

# # User Onboarding Function
# def onboarding_survey(role, industry, project_description):
#     return (project_extraction(project_description),
#             gr.update(visible=False), gr.update(visible=True))

# # Mock Integration Functions
# def integrate_todoist():
#     return "✅ Successfully connected to Todoist!"

# def integrate_evernote():
#     return "✅ Successfully connected to Evernote!"

# def integrate_calendar():
#     return "✅ Successfully connected to Google Calendar!"

# def load_svg_with_size(file_path, width="600px", height="400px"):
#     # Read the SVG content from the file
#     with open(file_path, "r", encoding="utf-8") as file:
#         svg_content = file.read()
    
#     # Add inline styles to control width and height
#     styled_svg = f"""
#     <div style="width: {width}; height: {height}; overflow: auto;">
#         {svg_content}
#     </div>
#     """
#     return styled_svg


# # In[20]:


# # Gradio Demo
# def create_gradio_interface(state=None):
#     with gr.Blocks(
#         css="""
#         .gradio-table td {
#             white-space: normal !important;
#             word-wrap: break-word !important;
#         }
#         .gradio-table {
#             width: 100% !important;  /* Adjust to 100% to fit the container */
#             table-layout: fixed !important;  /* Fixed column widths */
#             overflow-x: hidden !important;  /* Disable horizontal scrolling */
#         }
#         .gradio-container {
#             overflow-x: hidden !important;  /* Disable horizontal scroll for entire container */
#             padding: 0 !important;  /* Remove any default padding */
#         }
#         .gradio-column {
#             max-width: 100% !important;  /* Ensure columns take up full width */
#             overflow: hidden !important;  /* Hide overflow to prevent horizontal scroll */
#         }
#         .gradio-row {
#             overflow-x: hidden !important;  /* Prevent horizontal scroll on rows */
#         }
#     """) as demo:

#     # Page 1: Mock Gmail Login
#         with gr.Group(visible=True) as login_page:
#             gr.Markdown("### **1️⃣ Login with Gmail**")
#             email_input = gr.Textbox(label="Enter your Gmail Address", placeholder="[email protected]")
#             login_button = gr.Button("Login")
#             login_result = gr.Textbox(label="Login Status", interactive=False, visible=False)
#         # Page 2: User Onboarding
#         with gr.Group(visible=False) as onboarding_page:
#             gr.Markdown("### **2️⃣ Tell Us About Yourself**")
#             role = gr.Textbox(label="What is your role?", placeholder="e.g. Developer, Designer")
#             industry = gr.Textbox(label="Which industry are you in?", placeholder="e.g. Software, Finance")
#             project_description = gr.Textbox(label="Describe your project", placeholder="e.g. A task management app")
#             submit_survey = gr.Button("Submit")

#         # Page 3: Mock Integrations with Separate Buttons
#         with gr.Group(visible=False) as integrations_page:
#             gr.Markdown("### **3️⃣ Connect Integrations**")
#             gr.Markdown("Click on the buttons below to connect each tool:")

#             # Separate Buttons and Results for Each Integration
#             todoist_button = gr.Button("Connect to Todoist")
#             todoist_result = gr.Textbox(label="Todoist Status", interactive=False, visible=False)
            
#             evernote_button = gr.Button("Connect to Evernote")
#             evernote_result = gr.Textbox(label="Evernote Status", interactive=False, visible=False)
            
#             calendar_button = gr.Button("Connect to Google Calendar")
#             calendar_result = gr.Textbox(label="Google Calendar Status", interactive=False, visible=False)

#             # Skip Button to proceed directly to next page
#             skip_integrations = gr.Button("Skip ➡️")
#             next_button = gr.Button("Proceed to QR Code")

#         with gr.Group(visible=False) as qr_code_page:
#             # Page 4: QR Code and Curify Ideas
#             gr.Markdown("## Curify: Unified AI Tools for Productivity")
            
#             with gr.Tab("Curify Idea"):
#                 with gr.Row():
#                     with gr.Column():
#                         gr.Markdown("#### ** QR Code**")
#                         # Path to your local SVG file
#                         svg_file_path = "qr.svg"
#                         # Load the SVG content
#                         svg_content = load_svg_with_size(svg_file_path, width="200px", height="200px")
#                         gr.HTML(svg_content)

#                     # Column 1: Webpage rendering
#                     with gr.Column():
                        
#                         gr.Markdown("## Projects Overview")
#                         project_desc_table = gr.DataFrame(
#                             type="pandas"
#                         )

#                         gr.Markdown("## Enter task message.")
#                         idea_input = gr.Textbox(
#                             label=None,
#                             placeholder="Describe the task you want to execute (e.g., Research Paper Review)")
            
#                         task_btn = gr.Button("Generate Task Steps")
#                         fetch_state_btn = gr.Button("Fetch Updated State")

#                     with gr.Column():
#                         gr.Markdown("## Task analysis")
#                         task_analysis_txt = gr.Textbox(
#                             label=None,
#                             placeholder="Here is the execution status of your task...")

#                         gr.Markdown("## Execution status")
#                         execution_status = gr.DataFrame(
#                             type="pandas"
#                         )
#                         gr.Markdown("## Execution output")
#                         execution_results = gr.JSON(
#                             label=None
#                         )
#                         state_output = gr.State()  # Add a state output to hold the state

#                 task_btn.click(
#                     fn_process_task, 
#                     inputs=[project_desc_table, idea_input], 
#                     outputs=[task_analysis_txt, execution_status, execution_results]
#                 )

#                 fetch_state_btn.click(
#                     fetch_updated_state, 
#                     inputs=None, 
#                     outputs=[project_desc_table, task_analysis_txt, execution_status, execution_results]
#                 )

#                 # Page 1 -> Page 2 Transition
#                 login_button.click(
#                     mock_login, 
#                     inputs=email_input, 
#                     outputs=[login_result, login_page, onboarding_page]
#                 )

#                 # Page 2 -> Page 3 Transition (Submit and Skip)
#                 submit_survey.click(
#                     onboarding_survey, 
#                     inputs=[role, industry, project_description], 
#                     outputs=[project_desc_table, onboarding_page, integrations_page]
#                 )

#                 # Integration Buttons
#                 todoist_button.click(integrate_todoist, outputs=todoist_result)
#                 evernote_button.click(integrate_evernote, outputs=evernote_result)
#                 calendar_button.click(integrate_calendar, outputs=calendar_result)

#                 # Skip Integrations and Proceed
#                 skip_integrations.click(
#                     lambda: (gr.update(visible=False), gr.update(visible=True)),
#                     outputs=[integrations_page, qr_code_page]
#                 )

#         # # Set the load_fn to initialize the state when the page is loaded
#         # demo.load(
#         #     curify_ideas, 
#         #     inputs=[project_input, idea_input], 
#         #     outputs=[task_steps, task_analysis_txt, state_output]
#         # )
#     return demo
#     # Load function to initialize the state
#     # demo.load(load_fn, inputs=None, outputs=[state])  # Initialize the state when the page is loaded

# # Function to launch Gradio
# # def launch_gradio():
# #     demo = create_gradio_interface()
# #     demo.launch(share=True, inline=False)  # Gradio in the foreground

# # # Function to run FastAPI server using uvicorn in the background
# # async def run_fastapi():
# #     config = uvicorn.Config(app, host="0.0.0.0", port=5000, reload=True, log_level="debug")
# #     server = uvicorn.Server(config)
# #     await server.serve()

# # # FastAPI endpoint to display a message
# # @app.get("/", response_class=HTMLResponse)
# # async def index():
# #     return "FastAPI is running. Visit Gradio at the provided public URL."

# # # Main entry point for the asynchronous execution
# # async def main():
# #     # Run Gradio in the foreground and FastAPI in the background
# #     loop = asyncio.get_event_loop()
    
# #     # Run Gradio in a separate thread (non-blocking)
# #     loop.run_in_executor(None, launch_gradio)
    
# #     # Run FastAPI in the background (asynchronous)
# #     await run_fastapi()

# # if __name__ == "__main__":
# #     import nest_asyncio
# #     nest_asyncio.apply()  # Allow nested use of asyncio event loops in Jupyter notebooks
    
# #     # Run the main function to launch both services concurrently
# #     asyncio.run(main())
    
# # In[21]:
# demo = create_gradio_interface()
# # Use Gradio's `server_app` to get an ASGI app for Blocks
# gradio_asgi_app = demo.launch(share=False, inbrowser=False, server_name="0.0.0.0", server_port=7860, inline=False)

# logging.debug(f"Gradio version: {gr.__version__}")
# logging.debug(f"FastAPI version: {fastapi.__version__}")

# # # Mount the Gradio ASGI app at "/gradio"
# # app.mount("/gradio", gradio_asgi_app)

# # # create a static directory to store the static files
# # static_dir = Path('./static')
# # static_dir.mkdir(parents=True, exist_ok=True)

# # # mount FastAPI StaticFiles server
# # app.mount("/static", StaticFiles(directory=static_dir), name="static")

# # Dynamically check for the Gradio asset directory
# # gradio_assets_path = os.path.join(os.path.dirname(gr.__file__), "static")

# # if os.path.exists(gradio_assets_path):
# #     # If assets exist, mount them
# #     app.mount("/assets", StaticFiles(directory=gradio_assets_path), name="assets")
# # else:
# #     logging.error(f"Gradio assets directory not found at: {gradio_assets_path}")

# # Redirect from the root endpoint to the Gradio app
# @app.get("/", response_class=RedirectResponse)
# async def index():
#     return RedirectResponse(url="/gradio", status_code=307)

# # Run the FastAPI server using uvicorn
# if __name__ == "__main__":
#     # port = int(os.getenv("PORT", 5000))  # Default to 7860 if PORT is not set
#     uvicorn.run(app, host="0.0.0.0", port=7860)