Update app.py
Browse files
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 |
-
#
|
| 82 |
-
|
|
|
|
| 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, ์ฃผ์: ๊ฒฝ๊ธฐ๋ ์ํฅ
|
| 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
|
| 592 |
-
"""
|
| 593 |
-
Process YouTube video and generate summary using Gemini
|
| 594 |
-
Returns summary data in JSON format
|
| 595 |
-
"""
|
| 596 |
try:
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
|
| 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 |
-
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
|
| 636 |
-
|
| 637 |
-
|
| 638 |
|
| 639 |
-
|
| 640 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 641 |
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
|
| 650 |
-
|
| 651 |
-
|
| 652 |
-
|
| 653 |
-
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 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:
|