Oshchepkov commited on
Commit
43f8454
β€’
1 Parent(s): 441212c

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +8 -43
  2. util.py +36 -0
app.py CHANGED
@@ -1,43 +1,10 @@
1
  import streamlit as st
2
  import torch
3
- from urllib.parse import urlparse, parse_qs
4
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
5
- # https://pypi.org/project/youtube-transcript-api/
6
- from youtube_transcript_api import YouTubeTranscriptApi
7
 
8
- def get_video_id(url: str) -> str:
9
- """
10
- Examples:
11
- - http://youtu.be/SA2iWivDJiE
12
- - http://www.youtube.com/watch?v=_oPAwA_Udwc&feature=feedu
13
- - http://www.youtube.com/embed/SA2iWivDJiE
14
- - http://www.youtube.com/v/SA2iWivDJiE?version=3&hl=en_US
15
- """
16
- query = urlparse(url)
17
- if query.hostname == 'youtu.be':
18
- return query.path[1:]
19
- if query.hostname in ('www.youtube.com', 'youtube.com'):
20
- if query.path == '/watch':
21
- p = parse_qs(query.query)
22
- return p['v'][0]
23
- if query.path[:7] == '/embed/':
24
- return query.path.split('/')[2]
25
- if query.path[:3] == '/v/':
26
- return query.path.split('/')[2]
27
- return None
28
-
29
-
30
- def get_youtube_subtitle(video_id: str) -> str:
31
- try:
32
- parse = YouTubeTranscriptApi.get_transcript(video_id, languages=['ru'])
33
- result = ''
34
- for i in parse:
35
- if (i['text'][0] =='[') & (i['text'][-1] ==']'): continue
36
- result += ' ' + i['text']
37
- result = result.strip()[0].upper() + result.strip()[1:]
38
- return result.strip()
39
- except:
40
- return None
41
 
42
  device = "cuda" if torch.cuda.is_available() else "cpu"
43
  m_name = '/home/user/app/model'
@@ -48,13 +15,12 @@ model.to(device)
48
 
49
  if __name__ == "__main__":
50
  st.header("Annotation of subtitles from YouTube")
51
-
52
- # url = st.text_input('Enter the URL of the Youtube video', 'https://www.youtube.com/watch?v=HGSVsK32rKA ')
53
  option = st.selectbox(
54
- 'Video for example:',
55
- ('https://www.youtube.com/watch?v=HGSVsK32rKA',
56
- 'https://www.youtube.com/watch?v=fSpARfZ3I50',
57
- 'https://www.youtube.com/watch?v=3lEMopaRSjw'))
 
58
  url = st.text_input(':green[Enter your URL of the Youtube video] πŸ‘‡', option)
59
 
60
  video_id = get_video_id(url)
@@ -72,7 +38,6 @@ if __name__ == "__main__":
72
  return_tensors="pt",
73
  )["input_ids"]
74
 
75
-
76
  if st.button('Compute summary', help='Click me'):
77
  outputs = model.generate(inputs.to(device), max_new_tokens=100, do_sample=False)
78
  summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
 
1
  import streamlit as st
2
  import torch
3
+ #from urllib.parse import urlparse, parse_qs
4
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
 
 
5
 
6
+ #from youtube_transcript_api import YouTubeTranscriptApi
7
+ from util import get_video_id, get_youtube_subtitle
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
  m_name = '/home/user/app/model'
 
15
 
16
  if __name__ == "__main__":
17
  st.header("Annotation of subtitles from YouTube")
 
 
18
  option = st.selectbox(
19
+ 'Video for example:',
20
+ ('https://www.youtube.com/watch?v=HGSVsK32rKA',
21
+ 'https://www.youtube.com/watch?v=fSpARfZ3I50',
22
+ 'https://www.youtube.com/watch?v=3lEMopaRSjw')
23
+ )
24
  url = st.text_input(':green[Enter your URL of the Youtube video] πŸ‘‡', option)
25
 
26
  video_id = get_video_id(url)
 
38
  return_tensors="pt",
39
  )["input_ids"]
40
 
 
41
  if st.button('Compute summary', help='Click me'):
42
  outputs = model.generate(inputs.to(device), max_new_tokens=100, do_sample=False)
43
  summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
util.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from urllib.parse import urlparse, parse_qs
2
+ from youtube_transcript_api import YouTubeTranscriptApi
3
+
4
+ def get_video_id(url: str) -> str:
5
+ """
6
+ Examples:
7
+ - http://youtu.be/SA2iWivDJiE
8
+ - http://www.youtube.com/watch?v=_oPAwA_Udwc&feature=feedu
9
+ - http://www.youtube.com/embed/SA2iWivDJiE
10
+ - http://www.youtube.com/v/SA2iWivDJiE?version=3&hl=en_US
11
+ """
12
+ query = urlparse(url)
13
+ if query.hostname == 'youtu.be':
14
+ return query.path[1:]
15
+ if query.hostname in ('www.youtube.com', 'youtube.com'):
16
+ if query.path == '/watch':
17
+ p = parse_qs(query.query)
18
+ return p['v'][0]
19
+ if query.path[:7] == '/embed/':
20
+ return query.path.split('/')[2]
21
+ if query.path[:3] == '/v/':
22
+ return query.path.split('/')[2]
23
+ return None
24
+
25
+
26
+ def get_youtube_subtitle(video_id: str) -> str:
27
+ try:
28
+ parse = YouTubeTranscriptApi.get_transcript(video_id, languages=['ru'])
29
+ result = ''
30
+ for i in parse:
31
+ if (i['text'][0] =='[') & (i['text'][-1] ==']'): continue
32
+ result += ' ' + i['text']
33
+ result = result.strip()[0].upper() + result.strip()[1:]
34
+ return result.strip()
35
+ except:
36
+ return None