m8chaa commited on
Commit
e5cbe83
Β·
verified Β·
1 Parent(s): a86a8ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -28
app.py CHANGED
@@ -579,37 +579,56 @@ async def update_fb_transaction(transaction_ref: firestore.DocumentReference, no
579
  logging.error(f"Error updating transaction: {e}")
580
  return {"status": f"Error: {str(e)}"}
581
 
582
- async def process_youtube_video(video_id: str) -> dict:
583
  """
584
  Process YouTube video and generate summary using Gemini
585
  Returns summary data in JSON format
586
  """
587
  try:
588
- transcript_list = YouTubeTranscriptApi.get_transcript(video_id)
589
- full_transcript = " ".join([entry['text'] for entry in transcript_list])
590
-
591
- prompt = """
592
- You are a professional content summarizer. Summarize the following YouTube video transcript
593
- in Korean. Focus on the key points and main ideas. Keep the summary concise but informative.
594
-
595
- Transcript:
596
- {transcript}
597
-
598
- Please format the response as a JSON object with the following structure:
599
- {{
600
- "title": "Video title (if mentioned in transcript)",
601
- "summary": "Comprehensive summary in Korean",
602
- "key_points": ["Key point 1", "Key point 2", "..."],
603
- "timestamps": {{"topic": "timestamp"}} (if any significant timestamps are mentioned)
604
- }}
605
- """
606
-
607
- response = model.generate_content(prompt.format(transcript=full_transcript))
608
- return json.loads(response.text)
609
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
610
  except Exception as e:
611
  logging.error(f"Error processing YouTube video: {str(e)}")
612
- raise
 
 
 
 
 
 
613
 
614
  def extract_youtube_id(url: str) -> str:
615
  """
@@ -630,7 +649,9 @@ async def share_target_handle(request: Request):
630
  request_data = await request.json()
631
  logging.info(f"Received request data: {request_data}")
632
 
633
- shared_data = request_data.get("shared_data", {}).get("text", "") # Get the URL directly
 
 
634
  user_id = request_data.get("user_id")
635
  logging.info(f"Extracted shared_data: {shared_data}, user_id: {user_id}")
636
 
@@ -695,17 +716,17 @@ async def share_target_handle(request: Request):
695
  return {"status": "error", "message": f"Failed to create shared links sheet: {str(e)}"}
696
 
697
  # Process YouTube URL
698
- video_id = extract_youtube_id(shared_data)
699
  if not video_id:
700
  return {"status": "error", "message": "Not a valid YouTube URL"}
701
 
702
  # Generate video summary
703
- summary_data = await process_youtube_video(video_id)
704
 
705
  # Prepare sheet data
706
  sheet_data = [[
707
- f'=HYPERLINK("{shared_data}", "λ°”λ‘œκ°€κΈ°")',
708
- summary_data.get('title', ''),
709
  summary_data.get('summary', ''),
710
  datetime.now(kst).strftime("%Y-%m-%d %H:%M:%S")
711
  ]]
 
579
  logging.error(f"Error updating transaction: {e}")
580
  return {"status": f"Error: {str(e)}"}
581
 
582
+ async def process_youtube_video(video_id: str, video_title: str = "") -> dict:
583
  """
584
  Process YouTube video and generate summary using Gemini
585
  Returns summary data in JSON format
586
  """
587
  try:
588
+ try:
589
+ transcript_list = YouTubeTranscriptApi.get_transcript(video_id)
590
+ full_transcript = " ".join([entry['text'] for entry in transcript_list])
591
+ except Exception as e:
592
+ logging.info(f"Could not retrieve transcript: {str(e)}")
593
+ full_transcript = "" # Empty string if transcript not available
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
594
 
595
+ if full_transcript:
596
+ prompt = """
597
+ You are a professional content summarizer. Summarize the following YouTube video transcript
598
+ in Korean. Focus on the key points and main ideas. Keep the summary concise but informative.
599
+
600
+ Transcript:
601
+ {transcript}
602
+
603
+ Please format the response as a JSON object with the following structure:
604
+ {{
605
+ "title": "Video title (if mentioned in transcript)",
606
+ "summary": "Comprehensive summary in Korean",
607
+ "key_points": ["Key point 1", "Key point 2", "..."],
608
+ "timestamps": {{"topic": "timestamp"}} (if any significant timestamps are mentioned)
609
+ }}
610
+ """
611
+
612
+ response = model.generate_content(prompt.format(transcript=full_transcript))
613
+ return json.loads(response.text)
614
+ else:
615
+ # Return empty summary if no transcript available
616
+ return {
617
+ "title": video_title, # Use the provided title
618
+ "summary": "μžλ§‰μ΄ λΉ„ν™œμ„±ν™”λ˜μ–΄ μžˆμ–΄ μš”μ•½μ„ 생성할 수 μ—†μŠ΅λ‹ˆλ‹€.",
619
+ "key_points": [],
620
+ "timestamps": {}
621
+ }
622
+
623
  except Exception as e:
624
  logging.error(f"Error processing YouTube video: {str(e)}")
625
+ # Return empty summary on error
626
+ return {
627
+ "title": video_title, # Use the provided title
628
+ "summary": "λ™μ˜μƒ 처리 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€.",
629
+ "key_points": [],
630
+ "timestamps": {}
631
+ }
632
 
633
  def extract_youtube_id(url: str) -> str:
634
  """
 
649
  request_data = await request.json()
650
  logging.info(f"Received request data: {request_data}")
651
 
652
+ shared_data = request_data.get("shared_data", {})
653
+ video_url = shared_data.get("text", "")
654
+ video_title = shared_data.get("title", "") # Get the video title
655
  user_id = request_data.get("user_id")
656
  logging.info(f"Extracted shared_data: {shared_data}, user_id: {user_id}")
657
 
 
716
  return {"status": "error", "message": f"Failed to create shared links sheet: {str(e)}"}
717
 
718
  # Process YouTube URL
719
+ video_id = extract_youtube_id(video_url)
720
  if not video_id:
721
  return {"status": "error", "message": "Not a valid YouTube URL"}
722
 
723
  # Generate video summary
724
+ summary_data = await process_youtube_video(video_id, video_title)
725
 
726
  # Prepare sheet data
727
  sheet_data = [[
728
+ f'=HYPERLINK("{video_url}", "λ°”λ‘œκ°€κΈ°")',
729
+ summary_data.get('title', video_title), # Use video_title as fallback
730
  summary_data.get('summary', ''),
731
  datetime.now(kst).strftime("%Y-%m-%d %H:%M:%S")
732
  ]]