Oshchepkov commited on
Commit
e2a3db7
1 Parent(s): 7687263

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -28
app.py CHANGED
@@ -1,47 +1,68 @@
1
- import streamlit as st
 
 
2
  # https://pypi.org/project/youtube-transcript-api/
3
  from youtube_transcript_api import YouTubeTranscriptApi
4
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
5
-
6
 
7
  def get_video_id(url: str) -> str:
8
  """
9
- https://github.com/KennethSC/YouTube-Captions-Formatter/blob/master/YouTube_Captions.py
 
 
 
 
10
  """
11
- split_at = 'watch?v='
12
- if 'https://www.youtube.com/watch?v=' in url:
13
- video_id = url.partition(split_at)[2]
14
- else:
15
- raise Exception("This is not a valid video URL")
16
- return video_id
 
 
 
 
 
 
 
17
 
18
 
19
  def get_youtube_subtitle(video_id: str) -> str:
20
- try:
21
  parse = YouTubeTranscriptApi.get_transcript(video_id, languages=['ru'])
22
  result = ''
23
  for i in parse:
24
- if (i['text'][0] =='[') & (i['text'][-1] ==']'):
25
- continue
26
  result += ' ' + i['text']
27
- return result
 
28
  except:
29
- raise Exception("This video doesn't have a captions transcript")
30
 
 
 
 
 
 
 
31
 
32
- url = st.text_input('Enter the URL of the Youtube video', 'https://www.youtube.com/watch?v=yR4VmxwZh0s')
33
  video_id = get_video_id(url)
34
- subtitle = get_youtube_subtitle(video_id)
35
- st.write('Video_id', video_id)
36
 
37
- st.text(subtitle)
38
- m_name = 'summarize1'
39
- tokenizer = AutoTokenizer.from_pretrained(m_name)
40
- inputs = tokenizer(subtitle[:1024], return_tensors="pt").input_ids
 
 
41
 
42
- model = AutoModelForSeq2SeqLM.from_pretrained(m_name)
43
- outputs = model.generate(inputs, max_new_tokens=100, do_sample=False)
44
-
45
- summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
46
- st.write('subtitle', "")
47
- st.text(summary)
 
 
 
 
 
1
+ import streamlit as st
2
+ from urllib.parse import urlparse, parse_qs
3
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4
  # https://pypi.org/project/youtube-transcript-api/
5
  from youtube_transcript_api import YouTubeTranscriptApi
 
 
6
 
7
  def get_video_id(url: str) -> str:
8
  """
9
+ Examples:
10
+ - http://youtu.be/SA2iWivDJiE
11
+ - http://www.youtube.com/watch?v=_oPAwA_Udwc&feature=feedu
12
+ - http://www.youtube.com/embed/SA2iWivDJiE
13
+ - http://www.youtube.com/v/SA2iWivDJiE?version=3&hl=en_US
14
  """
15
+ query = urlparse(url)
16
+ if query.hostname == 'youtu.be':
17
+ return query.path[1:]
18
+ if query.hostname in ('www.youtube.com', 'youtube.com'):
19
+ if query.path == '/watch':
20
+ p = parse_qs(query.query)
21
+ return p['v'][0]
22
+ if query.path[:7] == '/embed/':
23
+ return query.path.split('/')[2]
24
+ if query.path[:3] == '/v/':
25
+ return query.path.split('/')[2]
26
+ # fail?
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
+ st.header("Annotation of subtitles from YouTube")
43
+ st.text('Load model...')
44
+ m_name = '/content/drive/MyDrive/Colab Notebooks/Netology/diplom_neto/summarize1'
45
+ tokenizer = AutoTokenizer.from_pretrained(m_name)
46
+ model = AutoModelForSeq2SeqLM.from_pretrained(m_name)
47
+ st.text('Model is loaded')
48
 
49
+ url = st.text_input('Enter the URL of the Youtube video', 'https://www.youtube.com/watch?v=HGSVsK32rKA')
50
  video_id = get_video_id(url)
 
 
51
 
52
+ if video_id is not None:
53
+ subtitle = get_youtube_subtitle(video_id)
54
+ if subtitle is not None:
55
+ st.subheader('Subtitles')
56
+ st.text(subtitle)
57
+ st.text('Compute summary...')
58
 
59
+ inputs = tokenizer(subtitle[:1024], return_tensors="pt").input_ids
60
+ outputs = model.generate(inputs, max_new_tokens=100, do_sample=False)
61
+ summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
62
+
63
+ st.subheader('Summary')
64
+ st.text(summary)
65
+ else:
66
+ st.write('Subtitles are disabled for this video')
67
+ else:
68
+ st.write('Video clip is not detected')