Update app.py
Browse files
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 |
-
#
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 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 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 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": "
|
| 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(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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:
|