-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
132 lines (109 loc) · 4.18 KB
/
app.py
File metadata and controls
132 lines (109 loc) · 4.18 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
from flask import Flask, request, jsonify
import subprocess
import tempfile
from pathlib import Path
import re
app = Flask(__name__)
# === TRANSCRIPT EXTRACTOR ===
class YTDLPCommandLineExtractor:
@staticmethod
def is_available():
try:
result = subprocess.run(['yt-dlp', '--version'], capture_output=True, text=True, timeout=5)
return result.returncode == 0
except:
return False
@staticmethod
def get_transcript(video_id):
if not YTDLPCommandLineExtractor.is_available():
return None, "yt-dlp not available. Please install yt-dlp."
try:
url = f'https://www.youtube.com/watch?v={video_id}'
temp_dir = tempfile.gettempdir()
cmd = [
'yt-dlp',
'--write-subs',
'--write-auto-subs',
'--sub-langs', 'en,en-US,en-GB',
'--sub-format', 'vtt',
'--skip-download',
'--output', f'{temp_dir}/%(id)s.%(ext)s',
url
]
result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
if result.returncode != 0:
return None, f"yt-dlp failed: {result.stderr}"
subtitle_files = list(Path(temp_dir).glob(f'{video_id}*.vtt'))
if subtitle_files:
with open(subtitle_files[0], 'r', encoding='utf-8') as f:
vtt_content = f.read()
# Clean up
for file in subtitle_files:
try:
file.unlink()
except:
pass
transcript = YTDLPCommandLineExtractor.parse_vtt(vtt_content)
return transcript, None if transcript.strip() else "Transcript was empty"
return None, "No subtitle file found"
except subprocess.TimeoutExpired:
return None, "yt-dlp timeout"
except Exception as e:
return None, str(e)
@staticmethod
def parse_vtt(vtt_content):
# Remove timestamps and metadata
text = re.sub(r"(\d{2}:\d{2}:\d{2}\.\d{3} --> .*\n)?", "", vtt_content)
text = re.sub(r"Kind:.*\n|Language:.*\n|Translator:.*\n|Reviewer:.*\n", "", text)
return text.strip()
# === OLLAMA CALL ===
def summarize_with_mistral(transcript):
try:
cmd = ['ollama', 'run', 'mistral']
process = subprocess.Popen(
cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
encoding="utf-8",
errors="replace"
)
prompt = f"Please provide a concise summary of this YouTube video transcript:\n\n{transcript}\n\nSummary:"
stdout, stderr = process.communicate(prompt, timeout=120)
if process.returncode != 0:
error_msg = f"Ollama error (code {process.returncode}): {stderr.strip()} | {stdout.strip()}"
return None, error_msg
return stdout.strip(), None
except subprocess.TimeoutExpired:
process.kill()
stdout, stderr = process.communicate()
return None, "Ollama summarization timed out"
except Exception as e:
return None, str(e)
# === FLASK ROUTE ===
@app.route('/summarize', methods=['POST'])
def summarize():
data = request.get_json()
if not data or 'url' not in data:
return jsonify({"error": "No URL provided"}), 400
url = data['url']
video_id_match = re.search(r"v=([a-zA-Z0-9_-]+)", url)
if not video_id_match:
return jsonify({"error": "Invalid YouTube URL"}), 400
video_id = video_id_match.group(1)
# Get transcript
transcript, error = YTDLPCommandLineExtractor.get_transcript(video_id)
if error:
return jsonify({"error": f"Transcript extraction failed: {error}"}), 500
# Summarize with Mistral
summary, error = summarize_with_mistral(transcript)
if error:
return jsonify({"error": f"Summarization failed: {error}"}), 500
return jsonify({
"summary": summary,
"transcript": transcript
})
# === MAIN ===
if __name__ == '__main__':
app.run(port=5500, debug=True)