m8chaa commited on
Commit
b9b92be
ยท
verified ยท
1 Parent(s): 8b8a37f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -36
app.py CHANGED
@@ -27,15 +27,8 @@ from base64 import b64encode
27
  from openai import OpenAI
28
  from fuzzywuzzy import fuzz
29
 
30
- from youtube_transcript_api import YouTubeTranscriptApi
31
- from youtube_transcript_api.formatters import JSONFormatter
32
- import http.client
33
- import socket
34
-
35
- # Add these timeout settings before your route handler
36
- socket.setdefaulttimeout(15) # 15 seconds timeout
37
- http.client.HTTPConnection._http_vsn = 10
38
- http.client.HTTPConnection._http_vsn_str = 'HTTP/1.0'
39
 
40
  # start application
41
  app = FastAPI()
@@ -109,7 +102,7 @@ receipt_sheet_headers = ['๋ฐœํ–‰์ผ', '์ƒํ˜ธ', '์‚ฌ์—…์ž๋ฒˆํ˜ธ', 'ํ•ฉ๊ณ„๊ธˆ์•ก
109
  # at index 1 '๋“ฑ๋ก ์ผ์‹œ', at last index '๋ช…ํ•จ๋ณด๊ธฐ'
110
  business_card_sheet_headers = ['์ด๋ฆ„', 'ํšŒ์‚ฌ๋ช…', '์†Œ์† ๋ฐ ์ง์œ„', '์ฃผ์†Œ', '์ „ํ™”' ,'ํœด๋Œ€ํฐ', 'ํŒฉ์Šค','e-mail','ํ™ˆํŽ˜์ด์ง€']
111
  sms_sheet_headers = ['๋‚ ์งœ', '๊ณ„์ขŒ', '์ž…๊ธˆ', '์ถœ๊ธˆ', '์ž”์•ก', '๋ฌธ์ž']
112
- contact_sheet_headers = ['์ด๋ฆ„', '์ „ํ™”๋ฒˆํ˜ธ1', '์ „ํ™”๋ฒˆํ˜ธ2', '์ „ํ™”๋ฒˆํ˜ธ3', '์ด๋ฉ”์ผ์ฃผ์†Œ1', '์ด๋ฉ”๏ฟฝ๏ฟฝ์ฃผ์†Œ2', '์ด๋ฉ”์ผ์ฃผ์†Œ3', '๊ทธ๋ฃน']
113
 
114
  receipts_ss = 1395967745
115
  business_cards_ss = 1733555840
@@ -587,23 +580,38 @@ async def update_fb_transaction(transaction_ref: firestore.DocumentReference, no
587
  logging.error(f"Error updating transaction: {e}")
588
  return {"status": f"Error: {str(e)}"}
589
 
590
- async def get_transcript_with_retry(video_id, max_retries=3):
591
  """
592
- Helper function to get transcript with retry logic and proper error handling
 
593
  """
594
- for attempt in range(max_retries):
595
- try:
596
- transcript = YouTubeTranscriptApi.get_transcript(
597
- video_id,
598
- languages=['en'],
599
- proxies={'http': None, 'https': None} # Disable proxy
600
- )
601
- return transcript
602
- except Exception as e:
603
- if attempt == max_retries - 1: # Last attempt
604
- raise e
605
- continue
606
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
607
 
608
  @app.post("/share-target-handle")
609
  async def share_target_handle(request: Request):
@@ -622,22 +630,21 @@ async def share_target_handle(request: Request):
622
  video_id = shared_data.split("youtu.be/")[1].split("?")[0]
623
 
624
  if video_id:
625
- transcript = await get_transcript_with_retry(video_id)
626
- if transcript:
627
- first_entries = transcript[:2] if len(transcript) >= 2 else transcript
628
- return {
629
- "status": "success",
630
- "message": "Transcript retrieved successfully",
631
- "transcript": first_entries
632
- }
633
 
 
 
 
 
 
 
634
  except Exception as e:
635
- logging.error(f"Error processing YouTube transcript: {str(e)}")
636
  return {
637
  "status": "error",
638
- "message": f"Failed to process transcript: {str(e)}"
639
  }
640
-
641
  return {"status": "success", "message": "Data logged successfully"}
642
 
643
  @app.post("/check-userdoc")
 
27
  from openai import OpenAI
28
  from fuzzywuzzy import fuzz
29
 
30
+ from googleapiclient.discovery import build
31
+ from google.oauth2 import service_account
 
 
 
 
 
 
 
32
 
33
  # start application
34
  app = FastAPI()
 
102
  # at index 1 '๋“ฑ๋ก ์ผ์‹œ', at last index '๋ช…ํ•จ๋ณด๊ธฐ'
103
  business_card_sheet_headers = ['์ด๋ฆ„', 'ํšŒ์‚ฌ๋ช…', '์†Œ์† ๋ฐ ์ง์œ„', '์ฃผ์†Œ', '์ „ํ™”' ,'ํœด๋Œ€ํฐ', 'ํŒฉ์Šค','e-mail','ํ™ˆํŽ˜์ด์ง€']
104
  sms_sheet_headers = ['๋‚ ์งœ', '๊ณ„์ขŒ', '์ž…๊ธˆ', '์ถœ๊ธˆ', '์ž”์•ก', '๋ฌธ์ž']
105
+ contact_sheet_headers = ['์ด๋ฆ„', '์ „ํ™”๋ฒˆํ˜ธ1', '์ „ํ™”๋ฒˆํ˜ธ2', '์ „ํ™”๋ฒˆํ˜ธ3', '์ด๋ฉ”์ผ์ฃผ์†Œ1', '์ด๋ฉ”์ผ์ฃผ์†Œ2', '์ด๋ฉ”์ผ์ฃผ์†Œ3', '๊ทธ๋ฃน']
106
 
107
  receipts_ss = 1395967745
108
  business_cards_ss = 1733555840
 
580
  logging.error(f"Error updating transaction: {e}")
581
  return {"status": f"Error: {str(e)}"}
582
 
583
+ async def get_youtube_captions(video_id: str) -> List[dict]:
584
  """
585
+ # Necessary for handling video transcripts when users want to extract information from YouTube videos
586
+ # Uses official YouTube Data API instead of third-party transcript API for better reliability
587
  """
588
+ try:
589
+ # Build YouTube service using existing credentials
590
+ youtube_service = build('youtube', 'v3', credentials=APICredential)
591
+
592
+ # Get caption tracks available for the video
593
+ captions_response = youtube_service.captions().list(
594
+ part='snippet',
595
+ videoId=video_id
596
+ ).execute()
597
+
598
+ if not captions_response.get('items'):
599
+ return []
600
+
601
+ # Get the first available caption track (usually the default one)
602
+ caption_id = captions_response['items'][0]['id']
603
+
604
+ # Download the caption track
605
+ caption_track = youtube_service.captions().download(
606
+ id=caption_id,
607
+ tfmt='srt' # Request in SRT format
608
+ ).execute()
609
+
610
+ return caption_track
611
+
612
+ except Exception as e:
613
+ logging.error(f"Error getting YouTube captions: {str(e)}")
614
+ return []
615
 
616
  @app.post("/share-target-handle")
617
  async def share_target_handle(request: Request):
 
630
  video_id = shared_data.split("youtu.be/")[1].split("?")[0]
631
 
632
  if video_id:
633
+ captions = await get_youtube_captions(video_id)
 
 
 
 
 
 
 
634
 
635
+ return {
636
+ "status": "success",
637
+ "message": "Captions retrieved successfully",
638
+ "captions": captions
639
+ }
640
+
641
  except Exception as e:
642
+ logging.error(f"Error processing YouTube captions: {str(e)}")
643
  return {
644
  "status": "error",
645
+ "message": f"Failed to process captions: {str(e)}"
646
  }
647
+
648
  return {"status": "success", "message": "Data logged successfully"}
649
 
650
  @app.post("/check-userdoc")