-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbrain.py
127 lines (97 loc) · 3.69 KB
/
brain.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
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
from langchain.chat_models import ChatOpenAI
from langchain.prompts.chat import (
ChatPromptTemplate,
SystemMessagePromptTemplate,
HumanMessagePromptTemplate,
)
from pytube import YouTube
from youtube_transcript_api import YouTubeTranscriptApi
import streamlit as st
from pytube import YouTube
OPENAI_API = st.secrets["OPENAI_API"]
template = ("""
I'm preparing notes from a video titled "{title}". I need you to
act as an expert professor and provide me with comprehensive and well-structured
notes from the following text.
Here is the text:
{transcription}
Condition: Please ensure the notes cover the following topics: ALL THE TOPICS.
Also do make sure you are printing everything in MARKDOWN.
Strictly do not print anything else other than the notes.
""")
def video_title(url):
"""
This function retrieves the title of a YouTube video given its URL.
Arguments:
url -- A string representing the URL of a YouTube video.
Returns:
str -- A string representing the title of the YouTube video.
Raises:
Exception -- If the provided URL is not valid or does not point to a YouTube video.
"""
yt = YouTube(url)
return yt.title
def text_extractor(url):
"""
This function extracts the text from a YouTube video transcript given its URL.
Args:
url: A string representing the URL of a YouTube video.
Returns:
A string containing the transcript text of the YouTube video.
Raises:
ConnectionError: If there is an error connecting to the YouTube Transcript API.
"""
try:
if "&list=" in url:
url = url.split("&list=")[0]
elif "?si=" in url:
url = url.split("?si=")[0]
video_id = url.split("?v=")[-1] if "?v=" in url else url.split("/")[-1]
except IndexError:
video_id = url.split("/")[-1]
try:
response = YouTubeTranscriptApi.get_transcript(video_id)
final = "".join([i['text'] for i in response])
if 4078 > len(final) > 5:
return final
else:
return None
except ConnectionError as e:
st.error(e)
@st.cache_data
def notes_generator(url):
"""
This function generates notes based on the provided URL.
Args:
url: A string representing the URL of the content for which notes are to be generated.
Returns:
A string containing the generated notes.
Raises:
Exception: If the URL is not valid or if there's an error in generating notes.
"""
chat = ChatOpenAI(temperature=0, openai_api_key=OPENAI_API)
system_message_prompt = SystemMessagePromptTemplate.from_template(template)
human_template = "{text}"
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
gpt_response = ChatPromptTemplate.from_messages(
[system_message_prompt, human_message_prompt]
)
response = chat(
gpt_response.format_prompt(
title=video_title(url), transcription=text_extractor(url) if text_extractor(url) is not None else "Sorry, couldn't extract the transcript for this video.",
text=url
).to_messages()
)
return response.content
def credits(url):
"""
This function generates credits of the content-creator based on the provided URL.
Args:
url: A string representing the URL of the content for which notes are to be generated.
Returns:
A string containing the credits for the YouTube video.
Raises:
Exception: If the URL is not valid or if there's an error in generating notes.
"""
yt = YouTube(url)
return yt.title, yt.author, yt.channel_url, yt.publish_date, yt.thumbnail_url