-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathYoutubeTranscriber_streamlit.py
More file actions
121 lines (99 loc) · 5.78 KB
/
Copy pathYoutubeTranscriber_streamlit.py
File metadata and controls
121 lines (99 loc) · 5.78 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
from youtube_transcript_api import YouTubeTranscriptApi
from urllib import parse
import streamlit as st
# import openai
list_languages = ["nl", "en", "de", "fr", "it", "sv", "da", "af", "sq", "am", "ar", "hy", "az", "eu", "be", "bn", "my",
"bs", "bg", "ca", "ceb", "zh-Hant", "zh-Hans",
"co", "eo", "et", "fil", "fi", "fr", "fy", "gl", "ka", "el", "gu", "ht", "ha", "haw", "iw",
"hi", "hmn", "hu", "ga", "ig", "is", "id", "ja", "jv", "yi", "kn", "kk", "km", "rw", "ky", "ku", "ko",
"hr", "lo", "la", "lv", "lt", "lb", "mk", "mg", "ml", "ms", "mt", "mi", "mr", "mn", "ne", "no", "ny",
"or", "ug", "uk", "uz", "ps", "fa", "pl", "pt", "pa", "ro", "ru", "sm", "gd", "sr", "sn", "sd", "si", "sl",
"sk", "su", "so", "es", "sw", "tg", "ta", "tt", "te", "th", "cs", "tk", "tr", "ur", "vi", "cy", "xh", "yo", "zu", "st"]
def transcribe_video(video_id, translate, language_from, translate_to):
total_text, num_words = transcribe_really(video_id, translate, language_from, translate_to, list_languages)
st.info(f"Number of words {num_words} / number of characters {len(total_text)} / number of tokens {int(len(total_text)/4)} (max. 4097) ")
st.write(total_text)
st.subheader("*To copy the text, roll over the mouse the box below and click the copy to clipboard icon at the right*")
st.code (total_text)
st.sidebar.markdown(
'<br><br><a href="http://bark.phon.ioc.ee/punctuator" target="_blank">Go to Punctuator to punctuate the text</a>',
unsafe_allow_html=True)
st.sidebar.markdown(
'<br><br><a href="https://beta.openai.com/playground/" target="_blank">Or go to Open AI playground to punctuate the text</a>',
unsafe_allow_html=True)
def transcribe_really(video_id, translate, language_from, translate_to,list_languages):
try:
if translate == True:
transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
transcript = transcript_list.find_transcript([language_from])
translated_transcript = transcript.translate(translate_to)
transcript_fetched = (translated_transcript.fetch())
else:
transcript_fetched = YouTubeTranscriptApi.get_transcript(video_id, languages=list_languages)
except Exception as e:
st.error(f"ERROR: {e}")
return None, None
total_text = ""
for t in transcript_fetched:
total_text += (t["text"])+ " "
#st.write(total_text)
num_words = len(total_text.split())
return total_text,num_words
def do_punctuate(total_text):
# VERY VERY SLOW !!!!!
p = Punctuator('C:\\Users\\rcxsm\\AppData\\Local\\Programs\\Python\\Python38\\Lib\\site-packages\\punctuator\\Demo-Europarl-EN.pcl')
punctuated_text = p.punctuate(total_text)
print (punctuated_text)
pc.copy(punctuated_text)
print ("Text copied")
def get_video_id(url):
if url != "":
st.info (f"Transcribing {url }")
url_parsed = parse.urlparse(url)
video_id_ = parse.parse_qs(url_parsed.query)
video_id__ = video_id_['v']
for vx in video_id__:
video_id = vx
return video_id
else:
st.warning("Enter an URL to transcribe")
def main():
st.sidebar.title ("Video transcriber")
languages_full = ["Afrikaans", "Albanees", "Amhaars", "Arabisch", "Armeens", "Azerbeidzjaans", "Baskisch", "Belarussisch",
"Bengaals", "Birmaans", "Bosnisch", "Bulgaars", "Catalaans", "Cebuano", "Chinees (Traditioneel)", "Chinees (Vereenvoudigd)",
"Corsicaans", "Deens", "Duits", "Engels", "Esperanto", "Estisch", "Filipijns", "Fins", "Frans", "Fries", "Galicisch",
"Georgisch", "Grieks", "Gujarati", "Haïtiaans Creools", "Hausa", "Hawaïaans", "Hebreeuws", "Hindi", "Hmong",
"Hongaars", "Iers", "Igbo", "IJslands", "Indonesisch", "Italiaans", "Japans", "Javaans", "Jiddisch", "Kannada",
"Kazachs", "Khmer", "Kinyarwanda", "Kirgizisch", "Koerdisch", "Koreaans", "Kroatisch", "Laotiaans", "Latijn",
"Lets", "Litouws", "Luxemburgs", "Macedonisch", "Malagassisch", "Malayalam", "Maleis", "Maltees", "Maori",
"Marathi", "Mongools", "Nederlands", "Nepalees", "Noors", "Nyanja", "Odia", "Oeigoers", "Oekraïens",
"Oezbeeks", "Pasjtoe", "Perzisch", "Pools", "Portugees", "Punjabi", "Roemeens", "Russisch", "Samoaans",
"Schots-Gaelisch", "Servisch", "Shona", "Sindhi", "Singalees", "Sloveens", "Slowaaks", "Soendanees",
"Somalisch", "Spaans", "Swahili", "Tadzjieks", "Tamil", "Tataars", "Telugu", "Thai", "Tsjechisch",
"Turkmeens", "Turks", "Urdu", "Vietnamees", "Welsh", "Xhosa", "Yoruba", "Zoeloe", "Zuid-Sotho", "Zweeds"]
url = st.sidebar.text_input("Video to transcribe")
translate = st.sidebar.selectbox("Translate", [True, False], index=1)
if translate:
language_from = st.sidebar.selectbox("Translate from", list_languages, index=1)
translate_to = st.sidebar.selectbox("Translate to", list_languages, index=0)
else:
language_from, translate_to = None, None
video_id = get_video_id(url)
if video_id != None:
transcribe_video(video_id, translate, language_from, translate_to)
# summary = summarize_video(video_id)
# st.write(summary)
def summarize_video(video_id):
transcript = YouTubeTranscriptApi.get_transcript(video_id)
text = "\n".join([t['text'] for t in transcript])
try:
resp = openai.ChatCompletion.create(
model="gpt-4o-mini",
messages=[{"role":"user", "content": f"Summarize:\n{text}"}]
)
except Exception as e:
st.error(f"ERROR: {e}")
return None
return resp.choices[0].message.content
if __name__ == "__main__":
main()