Spaces:
Build error
Build error
Luis G. Santini commited on
Commit ·
81927b7
1
Parent(s): efcbeaf
Add application file with transcript
Browse files- app.py +33 -2
- requirements.txt +0 -0
app.py
CHANGED
|
@@ -1,4 +1,35 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
| 3 |
|
| 4 |
+
def get_video_id(url):
|
| 5 |
+
from urllib.parse import urlparse, parse_qs
|
| 6 |
+
query = urlparse(url)
|
| 7 |
+
if query.hostname == 'youtu.be':
|
| 8 |
+
return query.path[1:]
|
| 9 |
+
if query.hostname in ('www.youtube.com', 'youtube.com'):
|
| 10 |
+
if query.path == '/watch':
|
| 11 |
+
p = parse_qs(query.query)
|
| 12 |
+
return p['v'][0]
|
| 13 |
+
if query.path[:7] == '/embed/':
|
| 14 |
+
return query.path.split('/')[2]
|
| 15 |
+
if query.path[:3] == '/v/':
|
| 16 |
+
return query.path.split('/')[2]
|
| 17 |
+
return None
|
| 18 |
+
|
| 19 |
+
def get_transcript(video_id):
|
| 20 |
+
try:
|
| 21 |
+
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
| 22 |
+
return ' '.join([entry['text'] for entry in transcript])
|
| 23 |
+
except Exception as e:
|
| 24 |
+
return str(e)
|
| 25 |
+
|
| 26 |
+
st.title('YouTube Transcript Reader')
|
| 27 |
+
|
| 28 |
+
url = st.text_input('Enter YouTube URL')
|
| 29 |
+
if url:
|
| 30 |
+
video_id = get_video_id(url)
|
| 31 |
+
if video_id:
|
| 32 |
+
transcript = get_transcript(video_id)
|
| 33 |
+
st.write(transcript)
|
| 34 |
+
else:
|
| 35 |
+
st.write('Invalid YouTube URL')
|
requirements.txt
ADDED
|
Binary file (1.6 kB). View file
|
|
|