Spaces:
Sleeping

m8chaa commited on
Commit
6e7520f
Β·
verified Β·
1 Parent(s): b28f16e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -22
app.py CHANGED
@@ -74,6 +74,11 @@ APICredential = service_account.Credentials.from_service_account_file(
74
  sheet_service = build('sheets', 'v4', credentials=APICredential)
75
  drive_service = build('drive', 'v3', credentials=APICredential)
76
 
 
 
 
 
 
77
  with open('/app/secrets/gemini_key.txt', 'r') as f:
78
  api_key = f.read()
79
 
@@ -588,32 +593,33 @@ async def process_youtube_video(video_id: str, video_title: str = "") -> dict:
588
  """
589
  try:
590
  try:
591
- # Create loader with Korean language preference, falling back to English
592
- loader = YoutubeLoader(
593
- video_id=video_id,
594
- language=["ko", "en"],
595
- add_video_info=True,
596
- continue_on_failure=True
597
- )
598
-
599
- # Load transcript
600
- transcript_doc = loader.load()
601
-
602
- if not transcript_doc:
603
  return {
604
  "title": video_title,
605
- "summary": "μžλ§‰μ„ 찾을 수 μ—†μŠ΅λ‹ˆλ‹€.",
606
  "key_points": [],
607
  "timestamps": {}
608
  }
609
-
610
- full_transcript = transcript_doc[0].page_content
611
- # If video info was added, update the title
612
- if transcript_doc[0].metadata.get("title"):
613
- video_title = transcript_doc[0].metadata["title"]
 
 
 
 
 
 
614
 
615
  except Exception as e:
616
- logging.error(f"Failed to get captions: {str(e)}")
617
  return {
618
  "title": video_title,
619
  "summary": "μžλ§‰μ„ κ°€μ Έμ˜€λŠ” 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€.",
@@ -621,24 +627,33 @@ async def process_youtube_video(video_id: str, video_title: str = "") -> dict:
621
  "timestamps": {}
622
  }
623
 
624
- # Generate summary if we got transcript
625
  prompt = """
626
  You are a professional content summarizer. Summarize the following YouTube video transcript
627
  in Korean. Focus on the key points and main ideas. Keep the summary concise but informative.
628
 
 
 
 
629
  Transcript:
630
  {transcript}
631
 
632
  Please format the response as a JSON object with the following structure:
633
  {{
634
- "title": "Video title (if mentioned in transcript)",
635
  "summary": "Comprehensive summary in Korean",
636
  "key_points": ["Key point 1", "Key point 2", "..."],
637
  "timestamps": {{"topic": "timestamp"}} (if any significant timestamps are mentioned)
638
  }}
639
  """
640
 
641
- response = model.generate_content(prompt.format(transcript=full_transcript))
 
 
 
 
 
 
642
  return json.loads(response.text)
643
 
644
  except Exception as e:
 
74
  sheet_service = build('sheets', 'v4', credentials=APICredential)
75
  drive_service = build('drive', 'v3', credentials=APICredential)
76
 
77
+ with open('/app/secrets/youtube_api_key.txt', 'r') as f:
78
+ YOUTUBE_API_KEY = f.read().strip()
79
+
80
+ youtube_service = build('youtube', 'v3', developerKey=YOUTUBE_API_KEY)
81
+
82
  with open('/app/secrets/gemini_key.txt', 'r') as f:
83
  api_key = f.read()
84
 
 
593
  """
594
  try:
595
  try:
596
+ # Get video details from YouTube Data API
597
+ video_response = youtube_service.videos().list(
598
+ part='snippet',
599
+ id=video_id
600
+ ).execute()
601
+
602
+ if not video_response.get('items'):
 
 
 
 
 
603
  return {
604
  "title": video_title,
605
+ "summary": "λΉ„λ””μ˜€λ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€.",
606
  "key_points": [],
607
  "timestamps": {}
608
  }
609
+
610
+ video_info = video_response['items'][0]['snippet']
611
+ video_title = video_info.get('title', video_title)
612
+
613
+ # Get transcript using youtube_transcript_api
614
+ transcript_list = YouTubeTranscriptApi.get_transcript(
615
+ video_id,
616
+ languages=['ko', 'en']
617
+ )
618
+
619
+ full_transcript = " ".join([entry['text'] for entry in transcript_list])
620
 
621
  except Exception as e:
622
+ logging.error(f"Failed to get video info or captions: {str(e)}")
623
  return {
624
  "title": video_title,
625
  "summary": "μžλ§‰μ„ κ°€μ Έμ˜€λŠ” 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€.",
 
627
  "timestamps": {}
628
  }
629
 
630
+ # Generate summary using Gemini
631
  prompt = """
632
  You are a professional content summarizer. Summarize the following YouTube video transcript
633
  in Korean. Focus on the key points and main ideas. Keep the summary concise but informative.
634
 
635
+ Video Title: {title}
636
+ Description: {description}
637
+
638
  Transcript:
639
  {transcript}
640
 
641
  Please format the response as a JSON object with the following structure:
642
  {{
643
+ "title": "{title}",
644
  "summary": "Comprehensive summary in Korean",
645
  "key_points": ["Key point 1", "Key point 2", "..."],
646
  "timestamps": {{"topic": "timestamp"}} (if any significant timestamps are mentioned)
647
  }}
648
  """
649
 
650
+ response = model.generate_content(
651
+ prompt.format(
652
+ title=video_title,
653
+ description=video_info.get('description', ''),
654
+ transcript=full_transcript
655
+ )
656
+ )
657
  return json.loads(response.text)
658
 
659
  except Exception as e: