Abhay Mishra commited on
Commit
2dd63ef
·
1 Parent(s): 82b2152

Add timestamps and title support. Use tiny model

Browse files
Files changed (1) hide show
  1. app.py +29 -11
app.py CHANGED
@@ -5,18 +5,35 @@ import whisper
5
  from pytube import YouTube
6
  import gradio as gr
7
 
8
- infer_model = whisper.load_model("base")
9
 
10
- def infer(link):
 
11
  audio_path = download_audio(link)
12
- if audio_path is None:
 
13
  return "Unable to process request."
14
 
15
  result = infer_model.transcribe(audio_path)
16
- print(result["text"])
17
- return result["text"]
18
 
19
- def download_audio(link):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  try:
21
  yt = YouTube(link)
22
  stream = yt.streams.get_audio_only()
@@ -25,13 +42,14 @@ def download_audio(link):
25
  return audio_path
26
  except Exception as e:
27
  print(f"Unable to download file. Exception {e}")
28
- return None
 
29
 
30
-
31
  demo = gr.Interface(
32
  fn=infer,
33
- inputs= "text",
34
- outputs= "text"
 
35
  )
36
 
37
- demo.launch()
 
5
  from pytube import YouTube
6
  import gradio as gr
7
 
8
+ infer_model = whisper.load_model("tiny")
9
 
10
+
11
+ def infer(link: str, add_timestamps: bool) -> str:
12
  audio_path = download_audio(link)
13
+
14
+ if not audio_path:
15
  return "Unable to process request."
16
 
17
  result = infer_model.transcribe(audio_path)
 
 
18
 
19
+ title = "Content"
20
+ try:
21
+ title = audio_path.split("/")[-1]
22
+ title = title.split(".")[0]
23
+ except Exception as e:
24
+ print(f"Unable to extract title. Exception {e}")
25
+
26
+ if not add_timestamps:
27
+ print(result["text"])
28
+ return title + "\n" + result["text"]
29
+
30
+ result_text = title + "\n"
31
+ for segment in result["segments"]:
32
+ result_text += f"{float(segment['start']):.2f}s - {float(segment['end']):.2f}s : {segment['text']}\n"
33
+ return result_text.strip("\n")
34
+
35
+
36
+ def download_audio(link: str) -> str:
37
  try:
38
  yt = YouTube(link)
39
  stream = yt.streams.get_audio_only()
 
42
  return audio_path
43
  except Exception as e:
44
  print(f"Unable to download file. Exception {e}")
45
+ return ""
46
+
47
 
 
48
  demo = gr.Interface(
49
  fn=infer,
50
+ inputs=[gr.Textbox(label = "Youtube Link", placeholder="Copy link here"), gr.Checkbox(value=True, label="Add timestamps?")],
51
+ outputs=[gr.Textbox(label = "Transcription", placeholder="Should be here after almost same time as video length")],
52
+ examples=[ ["https://www.youtube.com/watch?v=KL2T0XRzWUI", False], ["https://www.youtube.com/watch?v=yGB_K_xlHdI", False], ["https://www.youtube.com/watch?v=dv9sgFHS2Do", True],]
53
  )
54
 
55
+ demo.launch()