learningai commited on
Commit
b403268
·
1 Parent(s): 85c7303

youtube links

Browse files
Files changed (5) hide show
  1. app.py +27 -5
  2. config.py +1 -0
  3. logger.py +6 -0
  4. requirements.txt +2 -1
  5. utils.py +59 -0
app.py CHANGED
@@ -1,21 +1,43 @@
1
- import whisper
2
- import config
3
  import gradio as gr
 
 
 
4
 
5
 
 
6
  def get_text(audio_path):
7
  if not audio_path:
8
  return "No audio file chosen..."
9
  model = whisper.load_model(name='base', download_root=config.MODEL_DIR)
10
  results = model.transcribe(audio_path)
11
  return results['text']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
 
 
 
 
13
 
14
- label = gr.components.Text()
15
- vd = gr.components.Audio(type="filepath")
16
 
17
 
18
- iface = gr.Interface(fn=get_text, inputs=vd, outputs=label)
19
  iface.launch()
20
 
21
 
 
1
+ import os
 
2
  import gradio as gr
3
+ from utils import get_text_from_audio, get_video_from_yt
4
+ from logger import logging
5
+ import config
6
 
7
 
8
+ <<<<<<< HEAD
9
  def get_text(audio_path):
10
  if not audio_path:
11
  return "No audio file chosen..."
12
  model = whisper.load_model(name='base', download_root=config.MODEL_DIR)
13
  results = model.transcribe(audio_path)
14
  return results['text']
15
+ =======
16
+ def get_results(video_url :str) -> str:
17
+
18
+ logging.info(f">>>Getting predictions for : {video_url}")
19
+
20
+ try :
21
+ video_path = get_video_from_yt(video_url=video_url, save_file_dir=config.AUDIO_FILES_DIR)
22
+
23
+ if not video_path:
24
+ return "Problem while downloading the video. Please check the logs."
25
+
26
+ text = get_text_from_audio(video_path)
27
+
28
+ if not text:
29
+ return "Problem generating the text. Please check the logs."
30
+
31
+ return text
32
 
33
+ except Exception as e:
34
+ logging.exception(e)
35
+ return str(e)
36
+ >>>>>>> 0740c74 (input through youtube added)
37
 
 
 
38
 
39
 
40
+ iface = gr.Interface(fn=get_results, inputs="text", outputs="text")
41
  iface.launch()
42
 
43
 
config.py CHANGED
@@ -1,2 +1,3 @@
1
  import os
2
  MODEL_DIR = os.path.join(os.getcwd(), 'models', 'model.h5')
 
 
1
  import os
2
  MODEL_DIR = os.path.join(os.getcwd(), 'models', 'model.h5')
3
+ AUDIO_FILES_DIR = os.path.join(os.getcwd(), 'audio_files')
logger.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ import logging
2
+
3
+ logging.basicConfig(
4
+ format="[ %(asctime)s ] %(lineno)d %(name)s - %(levelname)s %(message)s",
5
+ level=logging.INFO
6
+ )
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
  openai-whisper
2
  gradio
3
- ffmpeg-python
 
 
1
  openai-whisper
2
  gradio
3
+ ffmpeg-python
4
+ yt-dlp
utils.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from pathlib import Path
3
+ from logger import logging
4
+ from typing import Optional
5
+ import whisper
6
+ import config
7
+
8
+
9
+ def get_video_from_yt(video_url : str, save_file_dir : str, video_name : str="yt_audio") -> Optional[Path]:
10
+ """
11
+ Download YouTube video as an audio file in .wav . Returns the path of the
12
+ downloaded file as string
13
+ """
14
+
15
+ logging.info(f"Attempting youtube video download : \nURL : {video_url}")
16
+
17
+ try :
18
+ # create directory if not exists
19
+ os.makedirs(save_file_dir, exist_ok=True)
20
+
21
+ filepath = f"{save_file_dir}/{video_name}"
22
+
23
+ # download the file
24
+ os.system(f'yt-dlp --quiet -o {filepath} -x --audio-format "wav" {video_url}')
25
+
26
+ logging.info(f"Download successful. \nAudio file path : {filepath}")
27
+
28
+ return f"{filepath}.wav"
29
+ except Exception as e:
30
+ logging.info("Download unsuccessful.")
31
+ logging.exception(e)
32
+ return None
33
+
34
+
35
+ def get_text_from_audio(audio_path : str) -> Optional[str]:
36
+ """
37
+ Extracts text from audio file.
38
+ """
39
+
40
+ logging.info(f"Attempting to extract text from : {audio_path}")
41
+
42
+ try :
43
+ model = whisper.load_model(name='base', download_root=config.MODEL_DIR)
44
+ results = model.transcribe(audio_path)
45
+
46
+ logging.info("Extraction successful.")
47
+ return results['text']
48
+ except Exception as e:
49
+ logging.info("Extraction failed.")
50
+ logging.exception(e)
51
+ return None
52
+
53
+
54
+
55
+ #URL = "https://www.youtube.com/watch?v=iO5LjrQaN9s"
56
+ #save_file_dir = "audio_files"
57
+ #video_path = get_video_from_yt(URL, save_file_dir)
58
+ #if video_path:
59
+ # print(get_text_from_audio(video_path))