qqwjq1981 commited on
Commit
017294a
·
verified ·
1 Parent(s): 90b1ed3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +323 -1203
app.py CHANGED
@@ -1,1281 +1,401 @@
1
- from fastapi import FastAPI, Request
2
- import gradio as gr
3
- import uvicorn
4
-
5
- # Initialize FastAPI app
6
- app = FastAPI()
7
-
8
- # FastAPI route to handle WhatsApp webhook
9
- @app.post("/whatsapp-webhook")
10
- async def whatsapp_webhook(request: Request):
11
- data = await request.json() # Parse incoming JSON data
12
- print(f"Received data: {data}") # Log incoming data for debugging
13
- return {"status": "success", "received_data": data}
14
-
15
- # Create a simple Gradio Blocks interface
16
- def greet(name):
17
- return f"Hello, {name}!"
18
-
19
- with gr.Blocks() as demo:
20
- gr.Markdown("### Greeting App")
21
- name_input = gr.Textbox(placeholder="Enter your name")
22
- greet_button = gr.Button("Greet")
23
- output_text = gr.Textbox(label="Greeting")
24
-
25
- greet_button.click(fn=greet, inputs=name_input, outputs=output_text)
26
-
27
- # Mount the Gradio app at "/gradio"
28
- gr.mount_gradio_app(app, demo, path="/gradio")
29
-
30
- # Run the FastAPI app with Uvicorn
31
- if __name__ == "__main__":
32
- uvicorn.run(app, host="0.0.0.0", port=7860)
33
-
34
- # from fastapi import FastAPI, Request
35
- # import uvicorn
36
-
37
- # # Initialize FastAPI app
38
- # app = FastAPI()
39
-
40
- # # FastAPI route to handle WhatsApp webhook
41
- # @app.post("/whatsapp-webhook")
42
- # async def whatsapp_webhook(request: Request):
43
- # data = await request.json() # Parse incoming JSON data
44
- # print(f"Received data: {data}") # Log incoming data for debugging
45
- # return {"status": "success", "received_data": data}
46
-
47
- # # Run the FastAPI app with Uvicorn
48
- # if __name__ == "__main__":
49
- # uvicorn.run(app, host="0.0.0.0", port=7860)
50
-
51
  #!/usr/bin/env python
52
  # coding: utf-8
53
 
 
54
 
55
- # In[2]:
56
-
57
-
58
- #pip install evernote-sdk-python3
59
- # import evernote.edam.notestore.NoteStore as NoteStore
60
- # import evernote.edam.type.ttypes as Types
61
- # from evernote.api.client import EvernoteClient
62
 
 
63
 
64
- # In[3]:
65
 
 
66
 
67
- # import os
68
- # import yaml
69
- # import pandas as pd
70
- # import numpy as np
71
-
72
- # from datetime import datetime, timedelta
73
-
74
- # # perspective generation
75
- # import openai
76
- # import os
77
- # from openai import OpenAI
78
-
79
- # import gradio as gr
80
-
81
- # import json
82
-
83
- # import sqlite3
84
- # import uuid
85
- # import socket
86
- # import difflib
87
- # import time
88
- # import shutil
89
- # import requests
90
- # import re
91
-
92
- # import json
93
- # import markdown
94
- # from fpdf import FPDF
95
- # import hashlib
96
-
97
- # from transformers import pipeline
98
- # from transformers.pipelines.audio_utils import ffmpeg_read
99
-
100
- # from todoist_api_python.api import TodoistAPI
101
 
102
- # # from flask import Flask, request, jsonify
103
- # from twilio.rest import Client
104
 
105
- # import asyncio
106
- # import uvicorn
107
- # import fastapi
108
- # from fastapi import FastAPI, Request, HTTPException
109
- # from fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse
110
- # from fastapi.staticfiles import StaticFiles
111
- # from pathlib import Path
112
 
113
- # import nest_asyncio
114
- # from twilio.twiml.messaging_response import MessagingResponse
 
 
 
 
 
 
115
 
116
- # from requests.auth import HTTPBasicAuth
117
 
118
- # from google.cloud import storage, exceptions # Import exceptions for error handling
119
- # from google.cloud.exceptions import NotFound
120
- # from google.oauth2 import service_account
121
 
122
- # from reportlab.pdfgen import canvas
123
- # from reportlab.lib.pagesizes import letter
124
- # from reportlab.pdfbase import pdfmetrics
125
- # from reportlab.lib import colors
126
- # from reportlab.pdfbase.ttfonts import TTFont
127
 
128
- # import logging
 
 
 
129
 
130
- # # Configure logging
131
- # logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s")
132
- # logger = logging.getLogger(__name__)
 
 
 
 
 
 
133
 
134
 
135
- # # In[4]:
136
 
137
- # # Access the API keys and other configuration data
138
- # openai_api_key = os.environ["OPENAI_API_KEY"]
139
- # # Access the API keys and other configuration data
140
- # todoist_api_key = os.environ["TODOIST_API_KEY"]
141
 
142
- # EVERNOTE_API_TOKEN = os.environ["EVERNOTE_API_TOKEN"]
143
 
144
- # account_sid = os.environ["TWILLO_ACCOUNT_SID"]
145
- # auth_token = os.environ["TWILLO_AUTH_TOKEN"]
146
- # twilio_phone_number = os.environ["TWILLO_PHONE_NUMBER"]
147
 
148
- # google_credentials_json = os.environ["GOOGLE_APPLICATION_CREDENTIALS"]
149
- # twillo_client = Client(account_sid, auth_token)
 
 
 
150
 
151
- # # Set the GOOGLE_APPLICATION_CREDENTIALS environment variable
152
 
153
- # # Load Reasoning Graph JSON File
154
- # def load_reasoning_json(filepath):
155
- # """Load JSON file and return the dictionary."""
156
- # with open(filepath, "r") as file:
157
- # data = json.load(file)
158
- # return data
159
-
160
- # # Load Action Map
161
- # def load_action_map(filepath):
162
- # """Load action map JSON file and map strings to actual function objects."""
163
- # with open(filepath, "r") as file:
164
- # action_map_raw = json.load(file)
165
- # # Map string names to actual functions using globals()
166
- # return {action: globals()[func_name] for action, func_name in action_map_raw.items()}
167
-
168
-
169
- # # In[5]:
170
 
171
 
172
- # # Define all actions as functions
173
-
174
- # def find_reference(task_topic):
175
- # """Finds a reference related to the task topic."""
176
- # print(f"Finding reference for topic: {task_topic}")
177
- # return f"Reference found for topic: {task_topic}"
178
-
179
- # def generate_summary(reference):
180
- # """Generates a summary of the reference."""
181
- # print(f"Generating summary for reference: {reference}")
182
- # return f"Summary of {reference}"
183
-
184
- # def suggest_relevance(summary):
185
- # """Suggests how the summary relates to the project."""
186
- # print(f"Suggesting relevance of summary: {summary}")
187
- # return f"Relevance of {summary} suggested"
188
-
189
- # def tool_research(task_topic):
190
- # """Performs tool research and returns analysis."""
191
- # print("Performing tool research")
192
- # return "Tool analysis data"
193
-
194
- # def generate_comparison_table(tool_analysis):
195
- # """Generates a comparison table for a competitive tool."""
196
- # print(f"Generating comparison table for analysis: {tool_analysis}")
197
- # return f"Comparison table for {tool_analysis}"
198
-
199
- # def generate_integration_memo(tool_analysis):
200
- # """Generates an integration memo for a tool."""
201
- # print(f"Generating integration memo for analysis: {tool_analysis}")
202
- # return f"Integration memo for {tool_analysis}"
203
-
204
- # def analyze_issue(task_topic):
205
- # """Analyzes an issue and returns the analysis."""
206
- # print("Analyzing issue")
207
- # return "Issue analysis data"
208
-
209
- # def generate_issue_memo(issue_analysis):
210
- # """Generates an issue memo based on the analysis."""
211
- # print(f"Generating issue memo for analysis: {issue_analysis}")
212
- # return f"Issue memo for {issue_analysis}"
213
-
214
- # def list_ideas(task_topic):
215
- # """Lists potential ideas for brainstorming."""
216
- # print("Listing ideas")
217
- # return ["Idea 1", "Idea 2", "Idea 3"]
218
-
219
- # def construct_matrix(ideas):
220
- # """Constructs a matrix (e.g., feasibility or impact/effort) for the ideas."""
221
- # print(f"Constructing matrix for ideas: {ideas}")
222
- # return {"Idea 1": "High Impact/Low Effort", "Idea 2": "Low Impact/High Effort", "Idea 3": "High Impact/High Effort"}
223
-
224
- # def prioritize_ideas(matrix):
225
- # """Prioritizes ideas based on the matrix."""
226
- # print(f"Prioritizing ideas based on matrix: {matrix}")
227
- # return ["Idea 3", "Idea 1", "Idea 2"]
228
-
229
- # def setup_action_plan(prioritized_ideas):
230
- # """Sets up an action plan based on the prioritized ideas."""
231
- # print(f"Setting up action plan for ideas: {prioritized_ideas}")
232
- # return f"Action plan created for {prioritized_ideas}"
233
-
234
- # def unsupported_task(task_topic):
235
- # """Handles unsupported tasks."""
236
- # print("Task not supported")
237
- # return "Unsupported task"
238
-
239
-
240
- # # In[6]:
241
-
242
-
243
- # todoist_api = TodoistAPI(todoist_api_key)
244
-
245
- # # Fetch recent Todoist task
246
- # def fetch_todoist_task():
247
- # try:
248
- # tasks = todoist_api.get_tasks()
249
- # if tasks:
250
- # recent_task = tasks[0] # Fetch the most recent task
251
- # return f"Recent Task: {recent_task.content}"
252
- # return "No tasks found in Todoist."
253
- # except Exception as e:
254
- # return f"Error fetching tasks: {str(e)}"
255
-
256
- # def add_to_todoist(task_topic, todoist_priority = 3):
257
- # try:
258
- # # Create a task in Todoist using the Todoist API
259
- # # Assuming you have a function `todoist_api.add_task()` that handles the API request
260
- # todoist_api.add_task(
261
- # content=task_topic,
262
- # priority=todoist_priority
263
- # )
264
- # msg = f"Task added: {task_topic} with priority {todoist_priority}"
265
- # logger.debug(msg)
266
-
267
- # return msg
268
- # except Exception as e:
269
- # # Return an error message if something goes wrong
270
- # return f"An error occurred: {e}"
271
-
272
- # # def save_todo(reasoning_steps):
273
- # # """
274
- # # Save reasoning steps to Todoist as tasks.
275
-
276
- # # Args:
277
- # # reasoning_steps (list of dict): A list of steps with "step" and "priority" keys.
278
- # # """
279
- # # try:
280
- # # # Validate that reasoning_steps is a list
281
- # # if not isinstance(reasoning_steps, list):
282
- # # raise ValueError("The input reasoning_steps must be a list.")
283
-
284
- # # # Iterate over the reasoning steps
285
- # # for step in reasoning_steps:
286
- # # # Ensure each step is a dictionary and contains required keys
287
- # # if not isinstance(step, dict) or "step" not in step or "priority" not in step:
288
- # # logger.error(f"Invalid step data: {step}, skipping.")
289
- # # continue
290
-
291
- # # task_content = step["step"]
292
- # # priority_level = step["priority"]
293
-
294
- # # # Map priority to Todoist's priority levels (1 - low, 4 - high)
295
- # # priority_mapping = {"Low": 1, "Medium": 2, "High": 4}
296
- # # todoist_priority = priority_mapping.get(priority_level, 1) # Default to low if not found
297
-
298
- # # # Create a task in Todoist using the Todoist API
299
- # # # Assuming you have a function `todoist_api.add_task()` that handles the API request
300
- # # todoist_api.add_task(
301
- # # content=task_content,
302
- # # priority=todoist_priority
303
- # # )
304
-
305
- # # logger.debug(f"Task added: {task_content} with priority {priority_level}")
306
-
307
- # # return "All tasks processed."
308
- # # except Exception as e:
309
- # # # Return an error message if something goes wrong
310
- # # return f"An error occurred: {e}"
311
-
312
-
313
- # # In[7]:
314
-
315
-
316
- # # evernote_client = EvernoteClient(token=EVERNOTE_API_TOKEN, sandbox=False)
317
- # # note_store = evernote_client.get_note_store()
318
-
319
- # # def add_to_evernote(task_topic, notebook_title="Inspirations"):
320
- # # """
321
- # # Add a task topic to the 'Inspirations' notebook in Evernote. If the notebook doesn't exist, create it.
322
-
323
- # # Args:
324
- # # task_topic (str): The content of the task to be added.
325
- # # notebook_title (str): The title of the Evernote notebook. Default is 'Inspirations'.
326
- # # """
327
- # # try:
328
- # # # Check if the notebook exists
329
- # # notebooks = note_store.listNotebooks()
330
- # # notebook = next((nb for nb in notebooks if nb.name == notebook_title), None)
331
-
332
- # # # If the notebook doesn't exist, create it
333
- # # if not notebook:
334
- # # notebook = Types.Notebook()
335
- # # notebook.name = notebook_title
336
- # # notebook = note_store.createNotebook(notebook)
337
-
338
- # # # Search for an existing note with the same title
339
- # # filter = NoteStore.NoteFilter()
340
- # # filter.notebookGuid = notebook.guid
341
- # # filter.words = notebook_title
342
- # # notes_metadata_result = note_store.findNotesMetadata(filter, 0, 1, NoteStore.NotesMetadataResultSpec(includeTitle=True))
343
-
344
- # # # If a note with the title exists, append to it; otherwise, create a new note
345
- # # if notes_metadata_result.notes:
346
- # # note_guid = notes_metadata_result.notes[0].guid
347
- # # existing_note = note_store.getNote(note_guid, True, False, False, False)
348
- # # existing_note.content = existing_note.content.replace("</en-note>", f"<div>{task_topic}</div></en-note>")
349
- # # note_store.updateNote(existing_note)
350
- # # else:
351
- # # # Create a new note
352
- # # note = Types.Note()
353
- # # note.title = notebook_title
354
- # # note.notebookGuid = notebook.guid
355
- # # note.content = f'<?xml version="1.0" encoding="UTF-8"?>' \
356
- # # f'<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">' \
357
- # # f'<en-note><div>{task_topic}</div></en-note>'
358
- # # note_store.createNote(note)
359
-
360
- # # print(f"Task '{task_topic}' successfully added to Evernote under '{notebook_title}'.")
361
- # # except Exception as e:
362
- # # print(f"Error adding task to Evernote: {e}")
363
-
364
- # # Mock Functions for Task Actions
365
- # def add_to_evernote(task_topic):
366
- # return f"Task added to Evernote with title '{task_topic}'."
367
-
368
-
369
- # # In[8]:
370
-
371
-
372
- # # Access the API keys and other configuration data
373
- # TASK_WORKFLOW_TREE = load_reasoning_json('curify_ideas_reasoning.json')
374
- # action_map = load_action_map('action_map.json')
375
-
376
- # # In[9]:
377
-
378
-
379
- # def generate_task_hash(task_description):
380
- # try:
381
- # # Ensure task_description is a string
382
- # if not isinstance(task_description, str):
383
- # logger.warning("task_description is not a string, attempting conversion.")
384
- # task_description = str(task_description)
385
 
386
- # # Safely encode with UTF-8 and ignore errors
387
- # encoded_description = task_description.encode("utf-8", errors="ignore")
388
- # task_hash = hashlib.md5(encoded_description).hexdigest()
389
-
390
- # logger.debug(f"Generated task hash: {task_hash}")
391
- # return task_hash
392
- # except Exception as e:
393
- # # Log any unexpected issues
394
- # logger.error(f"Error generating task hash: {e}", exc_info=True)
395
- # return 'output'
396
-
397
- # def save_to_google_storage(bucket_name, file_path, destination_blob_name, expiration_minutes = 1440):
398
- # credentials_dict = json.loads(google_credentials_json)
399
-
400
- # # Step 3: Use `service_account.Credentials.from_service_account_info` to authenticate directly with the JSON
401
- # credentials = service_account.Credentials.from_service_account_info(credentials_dict)
402
- # gcs_client = storage.Client(credentials=credentials, project=credentials.project_id)
403
-
404
- # # Check if the bucket exists; if not, create it
405
- # try:
406
- # bucket = gcs_client.get_bucket(bucket_name)
407
- # except NotFound:
408
- # print(f"❌ Bucket '{bucket_name}' not found. Please check the bucket name.")
409
- # bucket = gcs_client.create_bucket(bucket_name)
410
- # print(f"✅ Bucket '{bucket_name}' created.")
411
- # except Exception as e:
412
- # print(f"❌ An unexpected error occurred: {e}")
413
- # raise
414
- # # Get a reference to the blob
415
- # blob = bucket.blob(destination_blob_name)
416
-
417
- # # Upload the file
418
- # blob.upload_from_filename(file_path)
419
-
420
- # # Generate a signed URL for the file
421
- # signed_url = blob.generate_signed_url(
422
- # version="v4",
423
- # expiration=timedelta(minutes=expiration_minutes),
424
- # method="GET"
425
- # )
426
- # print(f"✅ File uploaded to Google Cloud Storage. Signed URL: {signed_url}")
427
- # return signed_url
428
-
429
-
430
- # # Function to check if content is Simplified Chinese
431
- # def is_simplified(text):
432
- # simplified_range = re.compile('[\u4e00-\u9fff]') # Han characters in general
433
- # simplified_characters = [char for char in text if simplified_range.match(char)]
434
- # return len(simplified_characters) > len(text) * 0.5 # Threshold of 50% to be considered simplified
435
-
436
- # # Function to choose the appropriate font for the content
437
- # def choose_font_for_content(content):
438
- # return 'NotoSansSC' if is_simplified(content) else 'NotoSansTC'
439
-
440
- # # Function to generate and save a document using ReportLab
441
- # def generate_document(task_description, md_content, user_name='jayw', bucket_name='curify'):
442
- # logger.debug("Starting to generate document")
443
-
444
- # # Hash the task description to generate a unique filename
445
- # task_hash = generate_task_hash(task_description)
446
-
447
- # # Truncate the hash if needed (64 characters is sufficient for uniqueness)
448
- # max_hash_length = 64 # Adjust if needed
449
- # truncated_hash = task_hash[:max_hash_length]
450
-
451
- # # Generate PDF file locally
452
- # local_filename = f"{truncated_hash}.pdf" # Use the truncated hash as the local file name
453
- # c = canvas.Canvas(local_filename, pagesize=letter)
454
-
455
- # # Paths to the TTF fonts for Simplified and Traditional Chinese
456
- # sc_font_path = 'NotoSansSC-Regular.ttf' # Path to Simplified Chinese font
457
- # tc_font_path = 'NotoSansTC-Regular.ttf' # Path to Traditional Chinese font
458
-
459
- # try:
460
- # # Register the Simplified Chinese font
461
- # sc_font = TTFont('NotoSansSC', sc_font_path)
462
- # pdfmetrics.registerFont(sc_font)
463
-
464
- # # Register the Traditional Chinese font
465
- # tc_font = TTFont('NotoSansTC', tc_font_path)
466
- # pdfmetrics.registerFont(tc_font)
467
 
468
- # # Set default font (Simplified Chinese or Traditional Chinese depending on content)
469
- # c.setFont('NotoSansSC', 12)
470
- # except Exception as e:
471
- # logger.error(f"Error loading font files: {e}")
472
- # raise RuntimeError("Failed to load one or more fonts. Ensure the font files are accessible.")
473
-
474
- # # Set initial Y position for drawing text
475
- # y_position = 750 # Starting position for text
476
-
477
- # # Process dictionary and render content
478
- # for key, value in md_content.items():
479
- # # Choose the font based on the key (header)
480
- # c.setFont(choose_font_for_content(key), 14)
481
- # c.drawString(100, y_position, f"# {key}")
482
- # y_position -= 20
483
-
484
- # # Choose the font for the value
485
- # c.setFont(choose_font_for_content(str(value)), 12)
486
-
487
- # # Add value
488
- # if isinstance(value, list): # Handle lists
489
- # for item in value:
490
- # c.drawString(100, y_position, f"- {item}")
491
- # y_position -= 15
492
- # else: # Handle single strings
493
- # c.drawString(100, y_position, value)
494
- # y_position -= 15
495
-
496
- # # Check if the page needs to be broken (if Y position is too low)
497
- # if y_position < 100:
498
- # c.showPage() # Create a new page
499
- # c.setFont('NotoSansSC', 12) # Reset font
500
- # y_position = 750 # Reset the Y position for the new page
501
-
502
- # # Save the PDF
503
- # c.save()
504
-
505
- # # Organize files into user-specific folders
506
- # destination_blob_name = f"{user_name}/{truncated_hash}.pdf"
507
-
508
- # # Upload to Google Cloud Storage and get the public URL
509
- # public_url = save_to_google_storage(bucket_name, local_filename, destination_blob_name)
510
- # logger.debug("Finished generating document")
511
- # return public_url
512
-
513
- # # In[10]:
514
-
515
-
516
- # def execute_with_retry(sql, params=(), attempts=5, delay=1, db_name = 'curify_ideas.db'):
517
- # for attempt in range(attempts):
518
- # try:
519
- # with sqlite3.connect(db_name) as conn:
520
- # cursor = conn.cursor()
521
- # cursor.execute(sql, params)
522
- # conn.commit()
523
- # break
524
- # except sqlite3.OperationalError as e:
525
- # if "database is locked" in str(e) and attempt < attempts - 1:
526
- # time.sleep(delay)
527
- # else:
528
- # raise e
529
-
530
- # # def enable_wal_mode(db_name = 'curify_ideas.db'):
531
- # # with sqlite3.connect(db_name) as conn:
532
- # # cursor = conn.cursor()
533
- # # cursor.execute("PRAGMA journal_mode=WAL;")
534
- # # conn.commit()
535
-
536
- # # # Create SQLite DB and table
537
- # # def create_db(db_name = 'curify_ideas.db'):
538
- # # with sqlite3.connect(db_name, timeout=30) as conn:
539
- # # c = conn.cursor()
540
- # # c.execute('''CREATE TABLE IF NOT EXISTS sessions (
541
- # # session_id TEXT,
542
- # # ip_address TEXT,
543
- # # project_desc TEXT,
544
- # # idea_desc TEXT,
545
- # # idea_analysis TEXT,
546
- # # prioritization_steps TEXT,
547
- # # timestamp DATETIME,
548
- # # PRIMARY KEY (session_id, timestamp)
549
- # # )
550
- # # ''')
551
- # # conn.commit()
552
-
553
- # # # Function to insert session data into the SQLite database
554
- # # def insert_session_data(session_id, ip_address, project_desc, idea_desc, idea_analysis, prioritization_steps, db_name = 'curify_ideas.db'):
555
- # # execute_with_retry('''
556
- # # INSERT INTO sessions (session_id, ip_address, project_desc, idea_desc, idea_analysis, prioritization_steps, timestamp)
557
- # # VALUES (?, ?, ?, ?, ?, ?, ?)
558
- # # ''', (session_id, ip_address, project_desc, idea_desc, json.dumps(idea_analysis), json.dumps(prioritization_steps), datetime.now()), db_name)
559
-
560
-
561
- # # In[11]:
562
-
563
-
564
- # def convert_to_listed_json(input_string):
565
- # """
566
- # Converts a string to a listed JSON object.
567
-
568
- # Parameters:
569
- # input_string (str): The JSON-like string to be converted.
570
-
571
- # Returns:
572
- # list: A JSON object parsed into a Python list of dictionaries.
573
- # """
574
- # try:
575
- # # Parse the string into a Python object
576
- # trimmed_string = input_string[input_string.index('['):input_string.rindex(']') + 1]
577
-
578
- # json_object = json.loads(trimmed_string)
579
- # return json_object
580
- # except json.JSONDecodeError as e:
581
- # return None
582
- # return None
583
- # #raise ValueError(f"Invalid JSON format: {e}")
584
-
585
- # def validate_and_extract_json(json_string):
586
- # """
587
- # Validates the JSON string, extracts fields with possible variants using fuzzy matching.
588
-
589
- # Args:
590
- # - json_string (str): The JSON string to validate and extract from.
591
- # - field_names (list): List of field names to extract, with possible variants.
592
-
593
- # Returns:
594
- # - dict: Extracted values with the best matched field names.
595
- # """
596
- # # Try to parse the JSON string
597
- # trimmed_string = json_string[json_string.index('{'):json_string.rindex('}') + 1]
598
- # try:
599
- # parsed_json = json.loads(trimmed_string)
600
- # return parsed_json
601
- # except json.JSONDecodeError as e:
602
- # return None
603
-
604
- # # {"error": "Parsed JSON is not a dictionary."}
605
- # return None
606
-
607
- # def json_to_pandas(dat_json, dat_schema = {'name':"", 'description':""}):
608
- # dat_df = pd.DataFrame([dat_schema])
609
- # try:
610
- # dat_df = pd.DataFrame(dat_json)
611
-
612
- # except Exception as e:
613
- # dat_df = pd.DataFrame([dat_schema])
614
- # # ValueError(f"Failed to parse LLM output as JSON: {e}\nOutput: {res}")
615
- # return dat_df
616
-
617
-
618
- # # In[12]:
619
-
620
-
621
- # client = OpenAI(
622
- # api_key= os.environ.get("OPENAI_API_KEY"), # This is the default and can be omitted
623
- # )
624
-
625
- # # Function to call OpenAI API with compact error handling
626
- # def call_openai_api(prompt, model="gpt-4o", max_tokens=5000, retries=3, backoff_factor=2):
627
- # """
628
- # Send a prompt to the OpenAI API and handle potential errors robustly.
629
-
630
- # Parameters:
631
- # prompt (str): The user input or task prompt to send to the model.
632
- # model (str): The OpenAI model to use (default is "gpt-4").
633
- # max_tokens (int): The maximum number of tokens in the response.
634
- # retries (int): Number of retry attempts in case of transient errors.
635
- # backoff_factor (int): Backoff time multiplier for retries.
636
-
637
- # Returns:
638
- # str: The model's response content if successful.
639
- # """
640
- # for attempt in range(1, retries + 1):
641
- # try:
642
- # response = client.chat.completions.create(
643
- # model="gpt-4o",
644
- # messages=[{"role": "user", "content": prompt}],
645
- # max_tokens=5000,
646
- # )
647
- # return response.choices[0].message.content.strip()
648
 
649
- # except (openai.RateLimitError, openai.APIConnectionError) as e:
650
- # logging.warning(f"Transient error: {e}. Attempt {attempt} of {retries}. Retrying...")
651
- # except (openai.BadRequestError, openai.AuthenticationError) as e:
652
- # logging.error(f"Unrecoverable error: {e}. Check your inputs or API key.")
653
- # break
654
- # except Exception as e:
655
- # logging.error(f"Unexpected error: {e}. Attempt {attempt} of {retries}. Retrying...")
656
 
657
- # # Exponential backoff before retrying
658
- # if attempt < retries:
659
- # time.sleep(backoff_factor * attempt)
660
-
661
- # raise RuntimeError(f"Failed to fetch response from OpenAI API after {retries} attempts.")
662
-
663
- # def fn_analyze_task(project_context, task_description):
664
- # prompt = (
665
- # f"You are working in the context of {project_context}. "
666
- # f"Your task is to analyze the task: {task_description} "
667
- # "Please analyze the following aspects: "
668
- # "1) Determine which project this item belongs to. If the idea does not belong to any existing project, categorize it under 'Other'. "
669
- # "2) Assess whether this idea can be treated as a concrete task. "
670
- # "3) Evaluate whether a document can be generated as an intermediate result. "
671
- # "4) Identify the appropriate category of the task. Possible categories are: 'Blogs/Papers', 'Tools', 'Brainstorming', 'Issues', and 'Others'. "
672
- # "5) Extract the topic of the task. "
673
- # "Please provide the output in JSON format using the structure below: "
674
- # "{"
675
- # " \"description\": \"\", "
676
- # " \"project_association\": \"\", "
677
- # " \"is_task\": \"Yes/No\", "
678
- # " \"is_document\": \"Yes/No\", "
679
- # " \"task_category\": \"\", "
680
- # " \"task_topic\": \"\" "
681
- # "}"
682
- # )
683
- # res_task_analysis = call_openai_api(prompt)
684
-
685
- # try:
686
- # json_task_analysis = validate_and_extract_json(res_task_analysis)
687
-
688
- # return json_task_analysis
689
- # except ValueError as e:
690
- # logger.debug("ValueError occurred: %s", str(e), exc_info=True) # Log the exception details
691
- # return None
692
-
693
-
694
- # # In[13]:
695
-
696
- # # Recursive Task Executor
697
- # def fn_process_task(project_desc_table, task_description, bucket_name='curify'):
698
-
699
- # project_context = project_desc_table.to_string(index=False)
700
- # task_analysis = fn_analyze_task(project_context, task_description)
701
-
702
- # if task_analysis:
703
- # execution_status = []
704
- # execution_results = task_analysis.copy()
705
- # execution_results['deliverables'] = ''
706
-
707
- # def traverse(node, previous_output=None):
708
- # if not node: # If the node is None or invalid
709
- # return # Exit if the node is invalid
710
-
711
- # # Check if there is a condition to evaluate
712
- # if "check" in node:
713
- # # Safely attempt to retrieve the value from execution_results
714
- # if node["check"] in execution_results:
715
- # value = execution_results[node["check"]] # Evaluate the check condition
716
- # traverse(node.get(value, node.get("default")), previous_output)
717
- # else:
718
- # # Log an error and exit, but keep partial results
719
- # logger.error(f"Key '{node['check']}' not found in execution_results.")
720
- # return
721
 
722
- # # If the node contains an action
723
- # elif "action" in node:
724
- # action_name = node["action"]
725
- # input_key = node.get("input", 'task_topic')
726
-
727
- # if input_key in execution_results.keys():
728
- # inputs = {input_key: execution_results[input_key]}
729
- # else:
730
- # # Log an error and exit, but keep partial results
731
- # logger.error(f"Workflow action {action_name} input key {input_key} not in execution_results.")
732
- # return
733
-
734
- # logger.debug(f"Executing: {action_name} with inputs: {inputs}")
735
-
736
- # # Execute the action function
737
- # action_func = action_map.get(action_name, unsupported_task)
738
- # try:
739
- # output = action_func(**inputs)
740
- # except Exception as e:
741
- # # Handle action function failure
742
- # logger.error(f"Error executing action '{action_name}': {e}")
743
- # return
744
-
745
- # # Store execution results or append to previous outputs
746
- # execution_status.append({"action": action_name, "output": output})
747
-
748
- # # Check if 'output' field exists in the node
749
- # if 'output' in node:
750
- # # If 'output' exists, assign the output to execution_results with the key from node['output']
751
- # execution_results[node['output']] = output
752
- # else:
753
- # # If 'output' does not exist, append the output to 'deliverables'
754
- # execution_results['deliverables'] += output
755
-
756
- # # Traverse to the next node, if it exists
757
- # if "next" in node and node["next"]:
758
- # traverse(node["next"], previous_output)
759
-
760
- # try:
761
- # traverse(TASK_WORKFLOW_TREE["start"])
762
- # execution_results['doc_url'] = generate_document(task_description, execution_results)
763
- # except Exception as e:
764
- # logger.error(f"Traverse Error: {e}")
765
- # finally:
766
- # # Always return partial results, even if an error occurs
767
- # return task_analysis, pd.DataFrame(execution_status), execution_results
768
- # else:
769
- # logger.error("Empty task analysis.")
770
- # return {}, pd.DataFrame(), {}
771
-
772
- # # In[14]:
773
-
774
-
775
- # # Initialize dataframes for the schema
776
- # ideas_df = pd.DataFrame(columns=["Idea ID", "Content", "Tags"])
777
-
778
- # def extract_ideas(context, text):
779
- # """
780
- # Extract project ideas from text, with or without a context, and return in JSON format.
781
-
782
- # Parameters:
783
- # context (str): Context of the extraction. Can be empty.
784
- # text (str): Text to extract ideas from.
785
-
786
- # Returns:
787
- # list: A list of ideas, each represented as a dictionary with name and description.
788
- # """
789
- # if context:
790
- # # Template when context is provided
791
- # prompt = (
792
- # f"You are working in the context of {context}. "
793
- # "Please extract the ongoing projects with project name and description."
794
- # "Please only the listed JSON as output string."
795
- # f"Ongoing projects: {text}"
796
- # )
797
- # else:
798
- # # Template when context is not provided
799
- # prompt = (
800
- # "Given the following information about the user."
801
- # "Please extract the ongoing projects with project name and description."
802
- # "Please only the listed JSON as output string."
803
- # f"Ongoing projects: {text}"
804
- # )
805
-
806
- # # return the raw string
807
- # return call_openai_api(prompt)
808
-
809
- # def df_to_string(df, empty_message = ''):
810
- # """
811
- # Converts a DataFrame to a string if it is not empty.
812
- # If the DataFrame is empty, returns an empty string.
813
-
814
- # Parameters:
815
- # ideas_df (pd.DataFrame): The DataFrame to be converted.
816
 
817
- # Returns:
818
- # str: A string representation of the DataFrame or an empty string.
819
- # """
820
- # if df.empty:
821
- # return empty_message
822
- # else:
823
- # return df.to_string(index=False)
824
-
825
 
826
- # # In[15]:
827
 
 
828
 
829
- # # Shared state variables
830
- # shared_state = {"project_desc_table": pd.DataFrame(), "task_analysis_txt": "", "execution_status": pd.DataFrame(), "execution_results": {}}
831
 
832
- # # Button Action: Fetch State
833
- # def fetch_updated_state():
834
- # # Iterating and logging the shared state
835
- # for key, value in shared_state.items():
836
- # if isinstance(value, pd.DataFrame):
837
- # logger.debug(f"{key}: DataFrame:\n{value.to_string()}")
838
- # elif isinstance(value, dict):
839
- # logger.debug(f"{key}: Dictionary: {value}")
840
- # elif isinstance(value, str):
841
- # logger.debug(f"{key}: String: {value}")
842
- # else:
843
- # logger.debug(f"{key}: Unsupported type: {value}")
844
- # return shared_state['project_desc_table'], shared_state['task_analysis_txt'], shared_state['execution_status'], shared_state['execution_results']
845
-
846
- # # response = requests.get("http://localhost:5000/state")
847
- # # # Check the status code and the raw response
848
- # # if response.status_code == 200:
849
- # # try:
850
- # # state = response.json() # Try to parse JSON
851
- # # return pd.DataFrame(state["project_desc_table"]), state["task_analysis_txt"], pd.DataFrame(state["execution_status"]), state["execution_results"]
852
- # # except ValueError as e:
853
- # # logger.error(f"JSON decoding failed: {e}")
854
- # # logger.debug("Raw response body:", response.text)
855
- # # else:
856
- # # logger.error(f"Error: {response.status_code} - {response.text}")
857
- # # """Fetch the updated shared state from FastAPI."""
858
- # # return pd.DataFrame(), "", pd.DataFrame(), {}
859
-
860
 
861
- # def update_gradio_state(project_desc_table, task_analysis_txt, execution_status, execution_results):
862
- # # You can update specific components like Textbox or State
863
- # shared_state['project_desc_table'] = project_desc_table
864
- # shared_state['task_analysis_txt'] = task_analysis_txt
865
- # shared_state['execution_status'] = execution_status
866
- # shared_state['execution_results'] = execution_results
867
- # return True
868
 
869
 
870
- # # In[16]:
871
 
872
 
873
- # # # Initialize the database
874
- # # new_db = 'curify.db'
875
 
876
- # # # Copy the old database to a new one
877
- # # shutil.copy("curify_idea.db", new_db)
878
 
879
- # #create_db(new_db)
880
- # #enable_wal_mode(new_db)
881
- # def project_extraction(project_description):
 
 
 
 
 
 
 
882
 
883
- # str_projects = extract_ideas('AI-powered tools for productivity', project_description)
884
- # json_projects = convert_to_listed_json(str_projects)
 
885
 
886
- # project_desc_table = json_to_pandas(json_projects)
887
- # update_gradio_state(project_desc_table, "", pd.DataFrame(), {})
888
- # return project_desc_table
889
 
 
 
 
890
 
891
- # # In[17]:
 
 
 
 
 
 
 
 
 
892
 
 
 
893
 
894
- # # 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.'
 
 
895
 
896
- # # # convert_to_listed_json(extract_ideas('AI-powered tools for productivity', project_description))
897
 
898
- # # task_description = 'Build an interview bot for the curify digest project.'
899
- # # task_analysis, reasoning_path = generate_reasoning_path(project_description, task_description)
900
 
901
- # # steps = store_and_execute_task(task_description, reasoning_path)
902
 
903
- # def message_back(task_message, execution_status, doc_url, from_whatsapp):
904
- # # Convert task steps to a simple numbered list
905
- # task_steps_list = "\n".join(
906
- # [f"{i + 1}. {step['action']} - {step.get('output', '')}" for i, step in enumerate(execution_status.to_dict(orient="records"))]
907
- # )
 
 
 
 
908
 
909
- # # Format the body message
910
- # body_message = (
911
- # f"*Task Message:*\n{task_message}\n\n"
912
- # f"*Execution Status:*\n{task_steps_list}\n\n"
913
- # f"*Doc URL:*\n{doc_url}\n\n"
914
- # )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
915
 
916
- # # Send response back to WhatsApp
917
- # try:
918
- # twillo_client.messages.create(
919
- # from_=twilio_phone_number,
920
- # to=from_whatsapp,
921
- # body=body_message
922
- # )
923
- # except Exception as e:
924
- # logger.error(f"Twilio Error: {e}")
925
- # raise HTTPException(status_code=500, detail=f"Error sending WhatsApp message: {str(e)}")
926
 
927
- # return {"status": "success"}
928
 
929
- # # Initialize the Whisper pipeline
930
- # whisper_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-medium")
931
 
932
- # # Function to transcribe audio from a media URL
933
- # def transcribe_audio_from_media_url(media_url):
934
- # try:
935
- # media_response = requests.get(media_url, auth=HTTPBasicAuth(account_sid, auth_token))
936
- # # Download the media file
937
- # media_response.raise_for_status()
938
- # audio_data = media_response.content
939
 
940
- # # Save the audio data to a file for processing
941
- # audio_file_path = "temp_audio_file.mp3"
942
- # with open(audio_file_path, "wb") as audio_file:
943
- # audio_file.write(audio_data)
 
 
 
944
 
945
- # # Transcribe the audio using Whisper
946
- # transcription = whisper_pipeline(audio_file_path, return_timestamps=True)
947
- # logger.debug(f"Transcription: {transcription['text']}")
948
- # return transcription["text"]
949
 
950
- # except Exception as e:
951
- # logger.error(f"An error occurred: {e}")
952
- # return None
953
 
954
 
955
- # # In[18]:
 
 
 
 
 
 
 
 
 
 
 
 
956
 
957
 
958
- # app = FastAPI()
959
 
960
- # @app.get("/state")
961
- # async def fetch_state():
962
- # return shared_state
963
 
964
- # @app.route("/whatsapp-webhook/", methods=["POST"])
965
- # async def whatsapp_webhook(request: Request):
966
- # form_data = await request.form()
967
- # # Log the form data to debug
968
- # print("Received data:", form_data)
969
 
970
- # # Extract message and user information
971
- # incoming_msg = form_data.get("Body", "").strip()
972
- # from_number = form_data.get("From", "")
973
- # media_url = form_data.get("MediaUrl0", "")
974
- # media_type = form_data.get("MediaContentType0", "")
975
-
976
- # # Initialize response variables
977
- # transcription = None
978
-
979
- # if media_type.startswith("audio"):
980
- # # If the media is an audio or video file, process it
981
- # try:
982
- # transcription = transcribe_audio_from_media_url(media_url)
983
- # except Exception as e:
984
- # return JSONResponse(
985
- # {"error": f"Failed to process voice input: {str(e)}"}, status_code=500
986
- # )
987
- # # Determine message content: use transcription if available, otherwise use text message
988
- # processed_input = transcription if transcription else incoming_msg
989
-
990
- # logger.debug(f"Processed input: {processed_input}")
991
-
992
- # try:
993
- # # Generate response
994
- # project_desc_table, _ = fetch_updated_state()
 
 
 
 
995
 
996
- # # If the project_desc_table is empty, return an empty JSON response
997
- # if project_desc_table.empty:
998
- # return JSONResponse(content={}) # Returning an empty JSON object
999
-
1000
- # # Continue processing if the table is not empty
1001
- # task_analysis_txt, execution_status, execution_results = fn_process_task(project_desc_table, processed_input)
1002
- # update_gradio_state(task_analysis_txt, execution_status, execution_results)
1003
-
1004
- # doc_url = 'Fail to generate doc'
1005
- # if 'doc_url' in execution_results:
1006
- # doc_url = execution_results['doc_url']
1007
-
1008
- # # Respond to the user on WhatsApp with the processed idea
1009
- # response = message_back(processed_input, execution_status, doc_url, from_number)
1010
- # logger.debug(response)
1011
-
1012
- # return JSONResponse(content=str(response))
1013
-
1014
- # except Exception as e:
1015
- # logger.error(f"Error during task processing: {e}")
1016
- # return JSONResponse(content={"error": str(e)}, status_code=500)
1017
 
1018
- # # In[19]:
 
 
 
 
1019
 
1020
 
1021
- # # Mock Gmail Login Function
1022
- # def mock_login(email):
1023
- # if email.endswith("@gmail.com"):
1024
- # return f"✅ Logged in as {email}", gr.update(visible=False), gr.update(visible=True)
1025
- # else:
1026
- # return "❌ Invalid Gmail address. Please try again.", gr.update(), gr.update()
1027
 
1028
- # # User Onboarding Function
1029
- # def onboarding_survey(role, industry, project_description):
1030
- # return (project_extraction(project_description),
1031
- # gr.update(visible=False), gr.update(visible=True))
1032
 
1033
- # # Mock Integration Functions
1034
- # def integrate_todoist():
1035
- # return "✅ Successfully connected to Todoist!"
1036
 
1037
- # def integrate_evernote():
1038
- # return " Successfully connected to Evernote!"
 
 
1039
 
1040
- # def integrate_calendar():
1041
- # return "✅ Successfully connected to Google Calendar!"
1042
 
1043
- # def load_svg_with_size(file_path, width="600px", height="400px"):
1044
- # # Read the SVG content from the file
1045
- # with open(file_path, "r", encoding="utf-8") as file:
1046
- # svg_content = file.read()
1047
-
1048
- # # Add inline styles to control width and height
1049
- # styled_svg = f"""
1050
- # <div style="width: {width}; height: {height}; overflow: auto;">
1051
- # {svg_content}
1052
- # </div>
1053
- # """
1054
- # return styled_svg
1055
-
1056
-
1057
- # # In[20]:
1058
-
1059
-
1060
- # # Gradio Demo
1061
- # def create_gradio_interface(state=None):
1062
- # with gr.Blocks(
1063
- # css="""
1064
- # .gradio-table td {
1065
- # white-space: normal !important;
1066
- # word-wrap: break-word !important;
1067
- # }
1068
- # .gradio-table {
1069
- # width: 100% !important; /* Adjust to 100% to fit the container */
1070
- # table-layout: fixed !important; /* Fixed column widths */
1071
- # overflow-x: hidden !important; /* Disable horizontal scrolling */
1072
- # }
1073
- # .gradio-container {
1074
- # overflow-x: hidden !important; /* Disable horizontal scroll for entire container */
1075
- # padding: 0 !important; /* Remove any default padding */
1076
- # }
1077
- # .gradio-column {
1078
- # max-width: 100% !important; /* Ensure columns take up full width */
1079
- # overflow: hidden !important; /* Hide overflow to prevent horizontal scroll */
1080
- # }
1081
- # .gradio-row {
1082
- # overflow-x: hidden !important; /* Prevent horizontal scroll on rows */
1083
- # }
1084
- # """) as demo:
1085
-
1086
- # # Page 1: Mock Gmail Login
1087
- # with gr.Group(visible=True) as login_page:
1088
- # gr.Markdown("### **1️⃣ Login with Gmail**")
1089
- # email_input = gr.Textbox(label="Enter your Gmail Address", placeholder="[email protected]")
1090
- # login_button = gr.Button("Login")
1091
- # login_result = gr.Textbox(label="Login Status", interactive=False, visible=False)
1092
- # # Page 2: User Onboarding
1093
- # with gr.Group(visible=False) as onboarding_page:
1094
- # gr.Markdown("### **2️⃣ Tell Us About Yourself**")
1095
- # role = gr.Textbox(label="What is your role?", placeholder="e.g. Developer, Designer")
1096
- # industry = gr.Textbox(label="Which industry are you in?", placeholder="e.g. Software, Finance")
1097
- # project_description = gr.Textbox(label="Describe your project", placeholder="e.g. A task management app")
1098
- # submit_survey = gr.Button("Submit")
1099
-
1100
- # # Page 3: Mock Integrations with Separate Buttons
1101
- # with gr.Group(visible=False) as integrations_page:
1102
- # gr.Markdown("### **3️⃣ Connect Integrations**")
1103
- # gr.Markdown("Click on the buttons below to connect each tool:")
1104
-
1105
- # # Separate Buttons and Results for Each Integration
1106
- # todoist_button = gr.Button("Connect to Todoist")
1107
- # todoist_result = gr.Textbox(label="Todoist Status", interactive=False, visible=False)
1108
-
1109
- # evernote_button = gr.Button("Connect to Evernote")
1110
- # evernote_result = gr.Textbox(label="Evernote Status", interactive=False, visible=False)
1111
-
1112
- # calendar_button = gr.Button("Connect to Google Calendar")
1113
- # calendar_result = gr.Textbox(label="Google Calendar Status", interactive=False, visible=False)
1114
-
1115
- # # Skip Button to proceed directly to next page
1116
- # skip_integrations = gr.Button("Skip ➡️")
1117
- # next_button = gr.Button("Proceed to QR Code")
1118
-
1119
- # with gr.Group(visible=False) as qr_code_page:
1120
- # # Page 4: QR Code and Curify Ideas
1121
- # gr.Markdown("## Curify: Unified AI Tools for Productivity")
1122
-
1123
- # with gr.Tab("Curify Idea"):
1124
- # with gr.Row():
1125
- # with gr.Column():
1126
- # gr.Markdown("#### ** QR Code**")
1127
- # # Path to your local SVG file
1128
- # svg_file_path = "qr.svg"
1129
- # # Load the SVG content
1130
- # svg_content = load_svg_with_size(svg_file_path, width="200px", height="200px")
1131
- # gr.HTML(svg_content)
1132
-
1133
- # # Column 1: Webpage rendering
1134
- # with gr.Column():
1135
-
1136
- # gr.Markdown("## Projects Overview")
1137
- # project_desc_table = gr.DataFrame(
1138
- # type="pandas"
1139
- # )
1140
-
1141
- # gr.Markdown("## Enter task message.")
1142
- # idea_input = gr.Textbox(
1143
- # label=None,
1144
- # placeholder="Describe the task you want to execute (e.g., Research Paper Review)")
1145
-
1146
- # task_btn = gr.Button("Generate Task Steps")
1147
- # fetch_state_btn = gr.Button("Fetch Updated State")
1148
-
1149
- # with gr.Column():
1150
- # gr.Markdown("## Task analysis")
1151
- # task_analysis_txt = gr.Textbox(
1152
- # label=None,
1153
- # placeholder="Here is the execution status of your task...")
1154
-
1155
- # gr.Markdown("## Execution status")
1156
- # execution_status = gr.DataFrame(
1157
- # type="pandas"
1158
- # )
1159
- # gr.Markdown("## Execution output")
1160
- # execution_results = gr.JSON(
1161
- # label=None
1162
- # )
1163
- # state_output = gr.State() # Add a state output to hold the state
1164
-
1165
- # task_btn.click(
1166
- # fn_process_task,
1167
- # inputs=[project_desc_table, idea_input],
1168
- # outputs=[task_analysis_txt, execution_status, execution_results]
1169
- # )
1170
-
1171
- # fetch_state_btn.click(
1172
- # fetch_updated_state,
1173
- # inputs=None,
1174
- # outputs=[project_desc_table, task_analysis_txt, execution_status, execution_results]
1175
- # )
1176
-
1177
- # # Page 1 -> Page 2 Transition
1178
- # login_button.click(
1179
- # mock_login,
1180
- # inputs=email_input,
1181
- # outputs=[login_result, login_page, onboarding_page]
1182
- # )
1183
-
1184
- # # Page 2 -> Page 3 Transition (Submit and Skip)
1185
- # submit_survey.click(
1186
- # onboarding_survey,
1187
- # inputs=[role, industry, project_description],
1188
- # outputs=[project_desc_table, onboarding_page, integrations_page]
1189
- # )
1190
-
1191
- # # Integration Buttons
1192
- # todoist_button.click(integrate_todoist, outputs=todoist_result)
1193
- # evernote_button.click(integrate_evernote, outputs=evernote_result)
1194
- # calendar_button.click(integrate_calendar, outputs=calendar_result)
1195
-
1196
- # # Skip Integrations and Proceed
1197
- # skip_integrations.click(
1198
- # lambda: (gr.update(visible=False), gr.update(visible=True)),
1199
- # outputs=[integrations_page, qr_code_page]
1200
- # )
1201
-
1202
- # # # Set the load_fn to initialize the state when the page is loaded
1203
- # # demo.load(
1204
- # # curify_ideas,
1205
- # # inputs=[project_input, idea_input],
1206
- # # outputs=[task_steps, task_analysis_txt, state_output]
1207
- # # )
1208
- # return demo
1209
- # # Load function to initialize the state
1210
- # # demo.load(load_fn, inputs=None, outputs=[state]) # Initialize the state when the page is loaded
1211
-
1212
- # # Function to launch Gradio
1213
- # # def launch_gradio():
1214
- # # demo = create_gradio_interface()
1215
- # # demo.launch(share=True, inline=False) # Gradio in the foreground
1216
-
1217
- # # # Function to run FastAPI server using uvicorn in the background
1218
- # # async def run_fastapi():
1219
- # # config = uvicorn.Config(app, host="0.0.0.0", port=5000, reload=True, log_level="debug")
1220
- # # server = uvicorn.Server(config)
1221
- # # await server.serve()
1222
-
1223
- # # # FastAPI endpoint to display a message
1224
- # # @app.get("/", response_class=HTMLResponse)
1225
- # # async def index():
1226
- # # return "FastAPI is running. Visit Gradio at the provided public URL."
1227
-
1228
- # # # Main entry point for the asynchronous execution
1229
- # # async def main():
1230
- # # # Run Gradio in the foreground and FastAPI in the background
1231
- # # loop = asyncio.get_event_loop()
1232
-
1233
- # # # Run Gradio in a separate thread (non-blocking)
1234
- # # loop.run_in_executor(None, launch_gradio)
1235
-
1236
- # # # Run FastAPI in the background (asynchronous)
1237
- # # await run_fastapi()
1238
 
1239
- # # if __name__ == "__main__":
1240
- # # import nest_asyncio
1241
- # # nest_asyncio.apply() # Allow nested use of asyncio event loops in Jupyter notebooks
1242
-
1243
- # # # Run the main function to launch both services concurrently
1244
- # # asyncio.run(main())
1245
-
1246
- # # In[21]:
1247
- # demo = create_gradio_interface()
1248
- # # Use Gradio's `server_app` to get an ASGI app for Blocks
1249
- # gradio_asgi_app = demo.launch(share=False, inbrowser=False, server_name="0.0.0.0", server_port=7860, inline=False)
1250
-
1251
- # logging.debug(f"Gradio version: {gr.__version__}")
1252
- # logging.debug(f"FastAPI version: {fastapi.__version__}")
1253
-
1254
- # # # Mount the Gradio ASGI app at "/gradio"
1255
- # # app.mount("/gradio", gradio_asgi_app)
1256
-
1257
- # # # create a static directory to store the static files
1258
- # # static_dir = Path('./static')
1259
- # # static_dir.mkdir(parents=True, exist_ok=True)
1260
-
1261
- # # # mount FastAPI StaticFiles server
1262
- # # app.mount("/static", StaticFiles(directory=static_dir), name="static")
1263
-
1264
- # # Dynamically check for the Gradio asset directory
1265
- # # gradio_assets_path = os.path.join(os.path.dirname(gr.__file__), "static")
1266
-
1267
- # # if os.path.exists(gradio_assets_path):
1268
- # # # If assets exist, mount them
1269
- # # app.mount("/assets", StaticFiles(directory=gradio_assets_path), name="assets")
1270
- # # else:
1271
- # # logging.error(f"Gradio assets directory not found at: {gradio_assets_path}")
1272
-
1273
- # # Redirect from the root endpoint to the Gradio app
1274
- # @app.get("/", response_class=RedirectResponse)
1275
- # async def index():
1276
- # return RedirectResponse(url="/gradio", status_code=307)
1277
-
1278
- # # Run the FastAPI server using uvicorn
1279
- # if __name__ == "__main__":
1280
- # # port = int(os.getenv("PORT", 5000)) # Default to 7860 if PORT is not set
1281
- # uvicorn.run(app, host="0.0.0.0", port=7860)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  #!/usr/bin/env python
2
  # coding: utf-8
3
 
4
+ # In[23]:
5
 
 
 
 
 
 
 
 
6
 
7
+ # In[24]:
8
 
 
9
 
10
+ # import subprocess
11
 
12
+ # try:
13
+ # result = subprocess.run(["ffmpeg", "-version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
14
+ # if result.returncode == 0:
15
+ # print("FFmpeg version:")
16
+ # print(result.stdout.split('\n')[0]) # Print the first line of the version output
17
+ # else:
18
+ # print("Error checking FFmpeg version:")
19
+ # print(result.stderr)
20
+ # except FileNotFoundError:
21
+ # print("FFmpeg is not installed or not found in PATH.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
 
 
23
 
24
+ # In[25]:
 
 
 
 
 
 
25
 
26
+ from urllib.parse import urlparse, parse_qs
27
+ import gradio as gr
28
+ import requests
29
+ from bs4 import BeautifulSoup
30
+ import openai
31
+ from openai import OpenAI
32
+ import speech_recognition as sr
33
+ from transformers import pipeline
34
 
35
+ from transformers.pipelines.audio_utils import ffmpeg_read
36
 
37
+ from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled
38
+ from youtube_transcript_api.formatters import TextFormatter
 
39
 
40
+ from urllib.parse import urlparse, parse_qs
41
+ import json
 
 
 
42
 
43
+ import os
44
+ import yaml
45
+ import pandas as pd
46
+ import numpy as np
47
 
48
+ import azureml.core
49
+ from azureml.core import Workspace, Datastore, ComputeTarget
50
+ from azure.identity import DefaultAzureCredential
51
+ from azure.ai.ml import MLClient
52
+ from azure.ai.ml import command
53
+ from azure.ai.ml import Input, Output
54
+ from azure.ai.ml import load_component
55
+ from azure.ai.ml.entities import Environment, Data, PipelineJob, Job, Schedule
56
+ from datetime import datetime, timedelta
57
 
58
 
59
+ # In[26]:
60
 
61
+ openai_api_key = os.environ["OPENAI_API_KEY"]
 
 
 
62
 
63
+ # In[27]:
64
 
 
 
 
65
 
66
+ # transcription = pipeline(
67
+ # "automatic-speech-recognition",
68
+ # model="openai/whisper-medium")
69
+ # result = transcription("2024_dairy.wav", return_timestamps=True)
70
+ # print(result["text"])
71
 
 
72
 
73
+ # In[28]:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
 
76
+ def is_youtube_url(url):
77
+ try:
78
+ # Parse the URL
79
+ parsed_url = urlparse(url)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
+ # Check if the domain is YouTube
82
+ if parsed_url.netloc in ["www.youtube.com", "youtube.com", "m.youtube.com", "youtu.be"]:
83
+ # For standard YouTube URLs, ensure it has a 'v' parameter
84
+ if "youtube.com" in parsed_url.netloc:
85
+ return "v" in parse_qs(parsed_url.query)
86
+ # For shortened YouTube URLs (youtu.be), check the path
87
+ elif "youtu.be" in parsed_url.netloc:
88
+ return len(parsed_url.path.strip("/")) > 0
89
+ return False
90
+ except Exception as e:
91
+ return False
92
+
93
+ def get_youtube_transcript(youtube_url):
94
+ try:
95
+ # Parse the video ID from the URL
96
+ parsed_url = urlparse(youtube_url)
97
+ video_id = parse_qs(parsed_url.query).get("v")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
 
99
+ if not video_id:
100
+ return "Invalid YouTube URL. Please provide a valid URL."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
+ video_id = video_id[0] # Extract the video ID
 
 
 
 
 
 
103
 
104
+ # Fetch the transcript
105
+ transcript = YouTubeTranscriptApi.get_transcript(video_id, proxies={"https": "http://localhost:8080"})
106
+
107
+ # Format the transcript as plain text
108
+ formatter = TextFormatter()
109
+ formatted_transcript = formatter.format_transcript(transcript)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
+ return formatted_transcript
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
 
113
+ except Exception as e:
114
+ return f"An error occurred: {str(e)}"
 
 
 
 
 
 
115
 
 
116
 
117
+ # In[29]:
118
 
 
 
119
 
120
+ def check_subtitles(video_id):
121
+ try:
122
+ transcripts = YouTubeTranscriptApi.list_transcripts(video_id)
123
+ print(f"Available transcripts: {transcripts}")
124
+ return True
125
+ except TranscriptsDisabled:
126
+ print("Subtitles are disabled for this video.")
127
+ return False
128
+ except Exception as e:
129
+ print(f"An unexpected error occurred: {e}")
130
+ return False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
 
132
+ # Test
133
+ video_id = "Um017R5Kr3A" # Replace with your YouTube video ID
134
+ check_subtitles(video_id)
 
 
 
 
135
 
136
 
137
+ # In[30]:
138
 
139
 
140
+ # 设置 OpenAI API
141
+ client = OpenAI(api_key=openai_api_key)
142
 
143
+ ### Curify Digest ###
 
144
 
145
+ # Function to fetch webpage, render it, and generate summary/perspectives
146
+ def process_webpage(url):
147
+ try:
148
+ if is_youtube_url(url):
149
+ rendered_content = get_youtube_transcript(url)
150
+ else:
151
+ # Fetch and parse webpage
152
+ response = requests.get(url)
153
+ soup = BeautifulSoup(response.text, "html.parser")
154
+ html_content = str(soup.prettify())
155
 
156
+ for script in soup(["script", "style"]):
157
+ script.decompose() # Remove script and style tags
158
+ rendered_content = soup.get_text(separator="\n").strip().replace("\n\n", "")
159
 
160
+ text_content = rendered_content[:2000] # Limit content length for processing
 
 
161
 
162
+ # Generate summary and perspectives
163
+ summary_prompt = f"Summarize the following content:\n{text_content}\n Please use the language of the originial content"
164
+ perspectives_prompt = f"Generate a reflective review for the following content:\n{text_content}\n Please output the perspectives in no more than 5 very concise bullet points. Please use the language of the originial content"
165
 
166
+ summary_response = client.chat.completions.create(
167
+ model="gpt-4o",
168
+ messages=[{"role": "user", "content": summary_prompt}],
169
+ max_tokens=500,
170
+ )
171
+ perspectives_response = client.chat.completions.create(
172
+ model="gpt-4o",
173
+ messages=[{"role": "user", "content": perspectives_prompt}],
174
+ max_tokens=500,
175
+ )
176
 
177
+ summary = summary_response.choices[0].message.content.strip()
178
+ perspectives = perspectives_response.choices[0].message.content.strip()
179
 
180
+ return rendered_content, summary, perspectives
181
+ except Exception as e:
182
+ return f"Error fetching or processing content: {str(e)}", "", ""
183
 
 
184
 
185
+ # In[31]:
 
186
 
 
187
 
188
+ # Function for chatbot interaction
189
+ def chat_with_ai(chat_history, user_input, content):
190
+ try:
191
+ messages = [{"role": "system", "content": "You are a helpful assistant."}]
192
+
193
+ # Add chat history
194
+ for user, bot in chat_history:
195
+ messages.append({"role": "user", "content": user})
196
+ messages.append({"role": "assistant", "content": bot})
197
 
198
+ # Add user input with webpage content
199
+ messages.append({"role": "user", "content": f"Based on this content: {content}\n\n{user_input}"})
200
+
201
+ # Call OpenAI API
202
+ ai_response = client.chat.completions.create(
203
+ model="gpt-4o",
204
+ messages=messages,
205
+ max_tokens=300,
206
+ )
207
+ reply = ai_response.choices[0].message.content.strip()
208
+ chat_history.append((user_input, reply))
209
+ return chat_history
210
+ except Exception as e:
211
+ return chat_history + [(user_input, f"Error: {str(e)}")]
212
+
213
+
214
+ # In[32]:
215
+
216
+
217
+ def generate_reflection(chat_history):
218
+ """
219
+ Generate a reflection based on the chat history.
220
+
221
+ Args:
222
+ chat_history (list of tuples): List of (user_input, ai_reply) pairs.
223
+
224
+ Returns:
225
+ str: A reflective summary generated by AI.
226
+ """
227
+ try:
228
+ messages = [{"role": "system", "content": "You are a professional content summarizer. Generate thoughtful reflections."}]
229
+
230
+ # Add conversation to messages
231
+ for user, bot in chat_history:
232
+ messages.append({"role": "user", "content": user})
233
+ messages.append({"role": "assistant", "content": bot})
234
+
235
+ # Prompt for reflection
236
+ messages.append({"role": "user", "content": "Please provide a concise, reflective summary of this conversation."})
237
+
238
+ # Call OpenAI API
239
+ ai_response = client.chat.completions.create(
240
+ model="gpt-4o",
241
+ messages=messages,
242
+ max_tokens=200,
243
+ )
244
+ reflection = ai_response.choices[0].message.content.strip()
245
+ return reflection
246
+ except Exception as e:
247
+ return f"Error generating reflection: {str(e)}"
248
+
249
+
250
+ # In[33]:
251
+
252
+
253
+ import requests
254
+
255
+ def post_to_linkedin(access_token, reflection, visibility="PUBLIC"):
256
+ """
257
+ Post a reflection to LinkedIn.
258
+
259
+ Args:
260
+ access_token (str): LinkedIn API access token.
261
+ reflection (str): The content to post.
262
+ visibility (str): Visibility setting ("PUBLIC" or "CONNECTIONS"). Defaults to "PUBLIC".
263
+
264
+ Returns:
265
+ str: Confirmation or error message.
266
+ """
267
+ try:
268
+ url = "https://api.linkedin.com/v2/ugcPosts"
269
+ headers = {
270
+ "Authorization": f"Bearer {access_token}",
271
+ "Content-Type": "application/json",
272
+ }
273
+ your_linkedin_person_id = 'jay'
274
+ payload = {
275
+ "author": f"urn:li:person:{your_linkedin_person_id}", # Replace with your LinkedIn person URN
276
+ "lifecycleState": "PUBLISHED",
277
+ "visibility": {"com.linkedin.ugc.MemberNetworkVisibility": visibility},
278
+ "specificContent": {
279
+ "com.linkedin.ugc.ShareContent": {
280
+ "shareCommentary": {
281
+ "text": reflection
282
+ },
283
+ "shareMediaCategory": "NONE"
284
+ }
285
+ }
286
+ }
287
+
288
+ response = requests.post(url, headers=headers, json=payload)
289
+ if response.status_code == 201:
290
+ return "Reflection successfully posted to LinkedIn!"
291
+ else:
292
+ return f"Failed to post to LinkedIn. Error: {response.json()}"
293
+ except Exception as e:
294
+ return f"Error posting to LinkedIn: {str(e)}"
295
 
 
 
 
 
 
 
 
 
 
 
296
 
297
+ # In[34]:
298
 
 
 
299
 
300
+ ### Curify Ideas ###
301
+ ideas_db = []
 
 
 
 
 
302
 
303
+ def extract_ideas_from_text(text):
304
+ # Mock idea extraction
305
+ ideas = text.split(". ")
306
+ for idea in ideas:
307
+ if idea.strip():
308
+ ideas_db.append({"content": idea.strip(), "timestamp": datetime.now()})
309
+ return [idea["content"] for idea in ideas_db]
310
 
 
 
 
 
311
 
312
+ # In[35]:
 
 
313
 
314
 
315
+ ### Curify Projects ###
316
+ def prepare_meeting(json_input):
317
+ try:
318
+ meetings = json.loads(json_input)
319
+ preparations = []
320
+ for meeting in meetings:
321
+ title = meeting.get("title", "No Title")
322
+ time = meeting.get("time", "No Time")
323
+ description = meeting.get("description", "No Description")
324
+ preparations.append(f"Meeting: {title}\nTime: {time}\nDetails: {description}")
325
+ return "\n\n".join(preparations)
326
+ except Exception as e:
327
+ return f"Error processing input: {e}"
328
 
329
 
330
+ # In[36]:
331
 
 
 
 
332
 
333
+ ### Gradio Demo ###
334
+ with gr.Blocks() as demo:
335
+ gr.Markdown("## Curify: Unified AI Tools for Productivity")
 
 
336
 
337
+ with gr.Tab("Curify Digest"):
338
+ with gr.Row():
339
+ # Column 1: Webpage rendering
340
+ with gr.Column():
341
+ gr.Markdown("## Render Webpage")
342
+ url_input = gr.Textbox(label="Enter URL")
343
+ # Shared Button: Fetch content, show webpage, and summary/perspectives
344
+ fetch_btn = gr.Button("Fetch and Process Webpage")
345
+ text_output = gr.Textbox(label="Webpage Content", lines=7)
346
+ # Column 2: Summary and Perspectives
347
+ with gr.Column():
348
+ gr.Markdown("## Summary & Perspectives")
349
+ summary_output = gr.Textbox(label="Summary", lines=5)
350
+ perspectives_output = gr.Textbox(label="Perspectives", lines=5)
351
+
352
+ # Column 3: Chatbot
353
+ with gr.Column():
354
+ gr.Markdown("## Interactive Chatbot")
355
+ chatbot_history_gr = gr.Chatbot(label="Chat History")
356
+ user_input = gr.Textbox(label="Ask a Question", placeholder="Type your question here...")
357
+ chatbot_btn = gr.Button("Send")
358
+ reflection_btn = gr.Button("Generate reflection")
359
+ reflection_output = gr.Textbox(label="Reflections", lines=5)
360
+
361
+ fetch_btn.click(
362
+ process_webpage,
363
+ inputs=url_input,
364
+ outputs=[text_output, summary_output, perspectives_output],
365
+ )
366
 
367
+ chatbot_btn.click(
368
+ chat_with_ai,
369
+ inputs=[chatbot_history_gr, user_input, text_output],
370
+ outputs=chatbot_history_gr,
371
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
372
 
373
+ reflection_btn.click(
374
+ generate_reflection,
375
+ inputs=chatbot_history_gr,
376
+ outputs=reflection_output,
377
+ )
378
 
379
 
380
+ with gr.Tab("Curify Ideas"):
381
+ text_input = gr.Textbox(label="Enter text or ideas")
382
+ extracted_ideas = gr.Textbox(label="Extracted Ideas", interactive=False)
383
+ extract_button = gr.Button("Extract Ideas")
 
 
384
 
385
+ def process_ideas(text):
386
+ return ", ".join(extract_ideas_from_text(text))
 
 
387
 
388
+ extract_button.click(process_ideas, inputs=[text_input], outputs=[extracted_ideas])
 
 
389
 
390
+ with gr.Tab("Curify Projects"):
391
+ json_input = gr.Textbox(label="Enter meeting data (JSON format)")
392
+ prepared_meetings = gr.Textbox(label="Meeting Preparations", interactive=False)
393
+ prepare_button = gr.Button("Prepare Meetings")
394
 
395
+ prepare_button.click(prepare_meeting, inputs=[json_input], outputs=[prepared_meetings])
 
396
 
397
+ demo.launch(share=True)
398
+
399
+
400
+ # In[ ]:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
401