-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
146 lines (110 loc) · 4.35 KB
/
app.py
File metadata and controls
146 lines (110 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from flask import Flask, render_template, request, jsonify
from flask_httpauth import HTTPBasicAuth
from werkzeug.security import generate_password_hash, check_password_hash
from openai import OpenAI
import os
import re
from urllib.parse import urlparse, parse_qs
from youtube_transcript_api import YouTubeTranscriptApi
from dotenv import load_dotenv
import requests
# Load environment variables from .env file
load_dotenv()
# Initialize OpenAI client
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
# YouTube API key
YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY")
app = Flask(__name__)
auth = HTTPBasicAuth()
users = {
os.getenv("AUTH_USERNAME"): generate_password_hash(os.getenv("AUTH_PASSWORD"))
}
@auth.verify_password
def verify_password(username, password):
if username in users and check_password_hash(users.get(username), password):
return username
def extract_video_id(url):
patterns = [
r"(?:v=|\/)([0-9A-Za-z_-]{11}).*",
r"(?:embed\/)([0-9A-Za-z_-]{11})",
r"(?:shorts\/)([0-9A-Za-z_-]{11})",
r"^([0-9A-Za-z_-]{11})$"
]
parsed_url = urlparse(url)
query = parse_qs(parsed_url.query)
if 'v' in query:
return query['v'][0]
for pattern in patterns:
match = re.search(pattern, url)
if match:
return match.group(1)
return None
def get_video_info(video_id):
url = f"https://www.googleapis.com/youtube/v3/videos?part=snippet&id={video_id}&key={YOUTUBE_API_KEY}"
response = requests.get(url)
data = response.json()
if 'items' in data and len(data['items']) > 0:
snippet = data['items'][0]['snippet']
return {
'title': snippet['title'],
'thumbnail': snippet['thumbnails']['medium']['url']
}
return None
def get_video_transcript(video_id):
try:
transcript = YouTubeTranscriptApi.get_transcript(video_id)
return " ".join([entry['text'] for entry in transcript])
except Exception as e:
print(f"An error occurred: {e}")
return None
def summarize_text(text):
try:
prompt = f"""Analyze and summarize the following YouTube video transcript:
{text}
Provide a comprehensive summary of the video content using the following structure:
1. Main Topic: Briefly state the primary subject or theme of the video in one sentence.
2. Key Points: List the 3-5 most important ideas or arguments presented in the video.
3. Detailed Summary: In 2-3 paragraphs, provide a more in-depth summary of the video's content, including any significant examples, case studies, or data mentioned.
4. Notable Quotes: Extract 2-3 significant or memorable quotes from the transcript, if any.
5. Conclusion: Summarize the main takeaway or conclusion of the video in 1-2 sentences.
6. Target Audience: Briefly mention who would find this video most useful or interesting.
Please ensure the summary is concise yet informative, capturing the essence of the video content."""
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are an expert at analyzing and summarizing video content."},
{"role": "user", "content": prompt}
],
max_tokens=800
)
return response.choices[0].message.content.strip()
except Exception as e:
print(f"An error occurred: {e}")
return None
@app.route('/')
@auth.login_required
def index():
return render_template('index.html')
@app.route('/summarize', methods=['POST'])
@auth.login_required
def summarize():
url = request.json['url']
video_id = extract_video_id(url)
if not video_id:
return jsonify({'error': 'Invalid YouTube URL'}), 400
video_info = get_video_info(video_id)
if not video_info:
return jsonify({'error': 'Failed to fetch video information'}), 400
transcript = get_video_transcript(video_id)
if not transcript:
return jsonify({'error': 'Failed to fetch video transcript'}), 400
summary = summarize_text(transcript)
if not summary:
return jsonify({'error': 'Failed to generate summary'}), 500
return jsonify({
'summary': summary,
'title': video_info['title'],
'thumbnail': video_info['thumbnail']
})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 5000)))