-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
86 lines (69 loc) · 2.69 KB
/
main.py
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
import logging
import re
import sys
from rich.console import Console
from rich.prompt import Prompt
from rich.text import Text
from models.llm_option import LLMOption
from summary.summarizer import YouTubeSummarizer
logging.basicConfig(level=logging.INFO)
console = Console()
def validate_youtube_url(url: str) -> bool:
"""
Basic validation to check if the URL matches a YouTube watch link.
"""
pattern = r"^(https?://(www\.)?youtube\.com/watch\?v=[\w-]+)$"
return bool(re.match(pattern, url))
def get_youtube_url_from_user() -> str:
"""
Prompt the user repeatedly until a valid YouTube URL is entered.
"""
while True:
console.print(
"Please enter a valid YouTube URL (e.g. [bold]https://www.youtube.com/watch?v=kpTxAIPcEAY[/bold]):",
style="cyan")
url = Prompt.ask("YouTube URL")
if validate_youtube_url(url):
return url
else:
console.print(Text("The provided URL is not a valid YouTube watch link. Please try again.", style="red"))
def select_llm() -> LLMOption:
options = LLMOption.list_options()
console.print("Please select the language model you want to use:")
for idx, option in enumerate(options, start=1):
console.print(f"[cyan]{idx}[/cyan]: {option['name']}")
llm_value = Prompt.ask(
f"Select language model ({'/'.join(str(i) for i in range(1, len(options) + 1))})",
default="4"
)
try:
llm_index = int(llm_value) - 1
if 0 <= llm_index < len(options):
return LLMOption.from_name(options[llm_index]["value"])
except (ValueError, IndexError):
pass
console.print(Text("Invalid selection. Please try again.", style="red"))
def get_youtube_url_from_params():
"""
Get the YouTube URL from the command-line parameters.
"""
youtube_url = sys.argv[1]
if not validate_youtube_url(youtube_url):
console.print(Text("Error: The provided command-line URL is not valid.", style="red"))
console.print(
Text("Please run the script again with a valid URL or omit the parameter to be prompted for one.",
style="yellow"))
sys.exit(1)
return youtube_url
def select_language() -> str:
language = Prompt.ask("Please enter the language you want to summarize the video in (e.g. English):",
default="English")
return language.strip().lower()
def main():
youtube_url = get_youtube_url_from_params() if len(sys.argv) > 1 else get_youtube_url_from_user()
llm = select_llm()
language = select_language()
summarizer = YouTubeSummarizer(youtube_url, llm, language)
summarizer.run()
if __name__ == "__main__":
main()