m8chaa commited on
Commit
5cdaf50
ยท
verified ยท
1 Parent(s): 6ed40a5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -72
app.py CHANGED
@@ -30,6 +30,8 @@ from fuzzywuzzy import fuzz
30
  from youtube_transcript_api import YouTubeTranscriptApi
31
  from langchain_community.document_loaders import YoutubeLoader
32
 
 
 
33
  # start application
34
  app = FastAPI()
35
 
@@ -78,8 +80,9 @@ APICredential = service_account.Credentials.from_service_account_file(
78
  sheet_service = build('sheets', 'v4', credentials=APICredential)
79
  drive_service = build('drive', 'v3', credentials=APICredential)
80
 
81
- # Use the same OAuth credentials for YouTube service
82
- youtube_service = build('youtube', 'v3', credentials=APICredential)
 
83
 
84
  with open('/app/secrets/gemini_key.txt', 'r') as f:
85
  api_key = f.read()
@@ -214,7 +217,7 @@ Example receipt:
214
  "์นด๋“œ๋ฒˆํ˜ธ": "5286-6420-****-077",
215
  "์นด๋“œ์ข…๋ฅ˜": "NH๊ธฐ์—…์ฒดํฌ",
216
  "์Šน์ธ๋ฒˆํ˜ธ": "49980444",
217
- "๊ธฐํƒ€": "์ „ํ™”: 031-8042-2047, ์ฃผ์†Œ: ๊ฒฝ๊ธฐ๋„ ์‹œํฅ๏ฟฝ๏ฟฝ๏ฟฝ ์ •์™•๋™ 2543, ๊ณ ๊ฐ๋ฌธ์˜ ์ „ํ™”: 1588-1300"
218
  }],
219
  "busi_cards": []
220
  }
@@ -588,86 +591,121 @@ async def update_fb_transaction(transaction_ref: firestore.DocumentReference, no
588
  logging.error(f"Error updating transaction: {e}")
589
  return {"status": f"Error: {str(e)}"}
590
 
591
- async def process_youtube_video(video_id: str, video_title: str = "") -> dict:
592
- """
593
- Process YouTube video and generate summary using Gemini
594
- Returns summary data in JSON format
595
- """
596
  try:
597
- try:
598
- # Get video details from YouTube Data API
599
- video_response = youtube_service.videos().list(
600
- part='snippet',
601
- id=video_id
602
- ).execute()
603
-
604
- if not video_response.get('items'):
605
- return {
606
- "title": video_title,
607
- "summary": "๋น„๋””์˜ค๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.",
608
- "key_points": [],
609
- "timestamps": {}
610
- }
611
-
612
- video_info = video_response['items'][0]['snippet']
613
- video_title = video_info.get('title', video_title)
614
-
615
- # Get transcript using youtube_transcript_api as fallback
616
- try:
617
- transcript_list = YouTubeTranscriptApi.get_transcript(
618
- video_id,
619
- languages=['ko', 'en', 'ko-KR', 'en-US']
620
- )
621
- full_transcript = " ".join([entry['text'] for entry in transcript_list])
622
- except Exception as e:
623
- logging.error(f"Failed to get transcript: {str(e)}")
624
- return {
625
- "title": video_title,
626
- "summary": "์ž๋ง‰์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.",
627
- "key_points": [],
628
- "timestamps": {}
629
- }
630
 
631
- # Generate summary using Gemini
632
- prompt = """
633
- You are a professional content summarizer. Summarize the following YouTube video transcript
634
- in Korean. Focus on the key points and main ideas. Keep the summary concise but informative.
635
 
636
- Video Title: {title}
637
- Description: {description}
638
 
639
- Transcript:
640
- {transcript}
 
 
 
 
 
641
 
642
- Please format the response as a JSON object with the following structure:
643
- {{
644
- "title": "{title}",
645
- "summary": "Comprehensive summary in Korean",
646
- "key_points": ["Key point 1", "Key point 2", "..."],
647
- "timestamps": {{"topic": "timestamp"}} (if any significant timestamps are mentioned)
648
- }}
649
- """
650
-
651
- response = model.generate_content(
652
- prompt.format(
653
- title=video_title,
654
- description=video_info.get('description', ''),
655
- transcript=full_transcript
656
- )
657
- )
658
- return json.loads(response.text)
659
-
660
- except Exception as e:
661
- logging.error(f"Error in summary generation: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
662
  return {
663
  "title": video_title,
664
- "summary": "๋™์˜์ƒ ์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.",
665
  "key_points": [],
666
  "timestamps": {}
667
  }
668
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
669
  except Exception as e:
670
- logging.error(f"Error in process_youtube_video: {str(e)}")
671
  return {
672
  "title": video_title,
673
  "summary": "๋™์˜์ƒ ์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.",
@@ -1054,7 +1092,7 @@ async def update_user_sheets_and_folders(user_data, transaction_ref, transaction
1054
  # https://drive.google.com/file/d/1cFIA09PBqFjM8YvIz1D60p7HMGk1uBvo/view?usp=sharing
1055
  download_image_url = f"https://drive.google.com/uc?id={image_id}&export=download"
1056
  image_url = f"https://drive.google.com/file/d/{image_id}/view?usp=sharing"
1057
- # ์ด๋ฏธ์ง€๋ฅผ ๋ณด์—ฌ์ฃผ์–ด์•ผ ํ• ๋•Œ =image({image_url})๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ๋กœ๋ฅผ ๋งŒ๋“ค๋•Œ๋Š” =HYPERLINK({image_url}, '๋ฐ”๋กœ๊ฐ€๊ธฐ')๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”.
1058
 
1059
  # Determine the primary folder to move the image to and secondary folder for shortcut
1060
  if found_receipt_no > 0:
 
30
  from youtube_transcript_api import YouTubeTranscriptApi
31
  from langchain_community.document_loaders import YoutubeLoader
32
 
33
+ import re
34
+
35
  # start application
36
  app = FastAPI()
37
 
 
80
  sheet_service = build('sheets', 'v4', credentials=APICredential)
81
  drive_service = build('drive', 'v3', credentials=APICredential)
82
 
83
+ # Add YouTube API key setup
84
+ YOUTUBE_API_KEY = os.getenv('YOUTUBE_API_KEY') # Add to your environment variables
85
+ youtube_service = build('youtube', 'v3', developerKey=YOUTUBE_API_KEY) # Use API key instead of OAuth
86
 
87
  with open('/app/secrets/gemini_key.txt', 'r') as f:
88
  api_key = f.read()
 
217
  "์นด๋“œ๋ฒˆํ˜ธ": "5286-6420-****-077",
218
  "์นด๋“œ์ข…๋ฅ˜": "NH๊ธฐ์—…์ฒดํฌ",
219
  "์Šน์ธ๋ฒˆํ˜ธ": "49980444",
220
+ "๊ธฐํƒ€": "์ „ํ™”: 031-8042-2047, ์ฃผ์†Œ: ๊ฒฝ๊ธฐ๋„ ์‹œํฅ์‹œ ์ •์™•๋™ 2543, ๊ณ ๊ฐ๋ฌธ์˜ ์ „ํ™”: 1588-1300"
221
  }],
222
  "busi_cards": []
223
  }
 
591
  logging.error(f"Error updating transaction: {e}")
592
  return {"status": f"Error: {str(e)}"}
593
 
594
+ async def get_youtube_transcript_url(video_id: str) -> str:
 
 
 
 
595
  try:
596
+ headers = {
597
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
598
+ 'Accept-Language': 'ko,en;q=0.9'
599
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
600
 
601
+ url = f"https://www.youtube.com/watch?v={video_id}"
602
+ async with httpx.AsyncClient() as client:
603
+ response = await client.get(url, headers=headers)
604
+ page_content = response.text
605
 
606
+ timedtext_pattern = r'"baseUrl":"(https://www\.youtube\.com/api/timedtext[^"]+)"'
607
+ matches = re.finditer(timedtext_pattern, page_content)
608
 
609
+ urls = {} # Dictionary to store language:url pairs
610
+ for match in matches:
611
+ url = match.group(1).replace('\\u0026', '&')
612
+ lang_match = re.search(r'lang=(\w+)', url)
613
+ if lang_match:
614
+ lang = lang_match.group(1)
615
+ urls[lang] = url
616
 
617
+ if urls:
618
+ if 'ko' in urls:
619
+ logging.info("Found Korean transcript")
620
+ return urls['ko']
621
+ elif 'en' in urls:
622
+ logging.info("Found English transcript")
623
+ return urls['en']
624
+ else:
625
+ lang = next(iter(urls))
626
+ logging.info(f"Found {lang.upper()} transcript")
627
+ return urls[lang]
628
+ else:
629
+ return None
630
+
631
+ except Exception as e:
632
+ logging.error(f"Error getting transcript URL: {str(e)}")
633
+ return None
634
+
635
+ async def process_youtube_video(video_id: str, video_title: str = "") -> dict:
636
+ try:
637
+ # Get video details from YouTube Data API
638
+ video_response = youtube_service.videos().list(
639
+ part='snippet',
640
+ id=video_id
641
+ ).execute()
642
+
643
+ if not video_response.get('items'):
644
+ return {
645
+ "title": video_title,
646
+ "summary": "๋น„๋””์˜ค๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.",
647
+ "key_points": [],
648
+ "timestamps": {}
649
+ }
650
+
651
+ video_info = video_response['items'][0]['snippet']
652
+ video_title = video_info.get('title', video_title)
653
+ description = video_info.get('description', '')
654
+
655
+ # Get transcript URL
656
+ transcript_url = await get_youtube_transcript_url(video_id)
657
+ if not transcript_url:
658
  return {
659
  "title": video_title,
660
+ "summary": "์ž๋ง‰์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.",
661
  "key_points": [],
662
  "timestamps": {}
663
  }
664
 
665
+ # Get the actual captions
666
+ async with httpx.AsyncClient() as client:
667
+ response = await client.get(transcript_url)
668
+ if response.status_code != 200:
669
+ return {
670
+ "title": video_title,
671
+ "summary": "์ž๋ง‰์„ ๊ฐ€์ ธ์˜ค๋Š”๋ฐ ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.",
672
+ "key_points": [],
673
+ "timestamps": {}
674
+ }
675
+
676
+ full_transcript = response.text
677
+
678
+ # Generate summary using Gemini
679
+ prompt = """
680
+ You are a professional content summarizer. Summarize the following YouTube video transcript
681
+ in Korean. Focus on the key points and main ideas. Keep the summary concise but informative.
682
+
683
+ Video Title: {title}
684
+ Description: {description}
685
+
686
+ Transcript:
687
+ {transcript}
688
+
689
+ Please format the response as a JSON object with the following structure:
690
+ {{
691
+ "title": "{title}",
692
+ "summary": "Comprehensive summary in Korean",
693
+ "key_points": ["Key point 1", "Key point 2", "..."],
694
+ "timestamps": {{"topic": "timestamp"}} (if any significant timestamps are mentioned)
695
+ }}
696
+ """
697
+
698
+ response = model.generate_content(
699
+ prompt.format(
700
+ title=video_title,
701
+ description=description,
702
+ transcript=full_transcript
703
+ )
704
+ )
705
+ return json.loads(response.text)
706
+
707
  except Exception as e:
708
+ logging.error(f"Error in summary generation: {str(e)}")
709
  return {
710
  "title": video_title,
711
  "summary": "๋™์˜์ƒ ์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.",
 
1092
  # https://drive.google.com/file/d/1cFIA09PBqFjM8YvIz1D60p7HMGk1uBvo/view?usp=sharing
1093
  download_image_url = f"https://drive.google.com/uc?id={image_id}&export=download"
1094
  image_url = f"https://drive.google.com/file/d/{image_id}/view?usp=sharing"
1095
+ # ์ด๋ฏธ์ง€๋ฅผ ๋ณด์—ฌ์ฃผ์–ด์•ผ ํ• ๋•Œ =image({image_url})๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ๋กœ๋ฅผ ๋งŒ๋“ค๋•Œ๋Š” =HYPERLINK({image_url}, '๋ฐ”๋กœ๊ฐ€๏ฟฝ๏ฟฝ')๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”.
1096
 
1097
  # Determine the primary folder to move the image to and secondary folder for shortcut
1098
  if found_receipt_no > 0: