Edward Nagy commited on
Commit
c286cd9
·
unverified ·
1 Parent(s): 376807a

Refactor transcribe function to handle both video

Browse files
Files changed (1) hide show
  1. app.py +23 -21
app.py CHANGED
@@ -10,35 +10,37 @@ import os
10
  def transcribe_audio(audio_file):
11
  text = "Test text"
12
  # text = pipe(audio_file)["text"]
13
- os.remove(audio_file) # Remove temporary audio file
14
  return text
15
 
16
 
17
- def transcribe(input_data):
18
- if input_data["audio"]:
19
- return transcribe_audio(input_data["audio"].name)
20
- elif input_data["video_url"]:
21
- video_url = input_data["video_url"]
22
- # Download the video from the URL
23
- video_filename = "temp_video.mp4"
24
- with open(video_filename, "wb") as f:
25
- response = requests.get(video_url)
26
- f.write(response.content)
27
 
28
- # Load the video using moviepy
29
- video = VideoFileClip(video_filename)
30
- audio = video.audio
31
 
32
- audio_file = "temp_audio.wav"
33
- audio.write_audiofile(audio_file, codec="pcm_s16le")
34
 
35
- text = transcribe_audio(audio_file)
36
 
37
- # Remove temporary files
38
- os.remove(video_filename)
39
- os.remove(audio_file)
40
 
41
- return text
 
 
 
 
 
 
 
42
 
43
 
44
  iface = gr.Interface(
 
10
  def transcribe_audio(audio_file):
11
  text = "Test text"
12
  # text = pipe(audio_file)["text"]
 
13
  return text
14
 
15
 
16
+ def transcribe_video(video_url):
17
+ # Download the video from the URL
18
+ video_filename = "temp_video.mp4"
19
+ with open(video_filename, "wb") as f:
20
+ response = requests.get(video_url)
21
+ f.write(response.content)
 
 
 
 
22
 
23
+ # Load the video using moviepy
24
+ video = VideoFileClip(video_filename)
25
+ audio = video.audio
26
 
27
+ audio_file = "temp_audio.wav"
28
+ audio.write_audiofile(audio_file, codec="pcm_s16le")
29
 
30
+ text = transcribe_audio(audio_file)
31
 
32
+ # Remove temporary files
33
+ os.remove(video_filename)
34
+ os.remove(audio_file)
35
 
36
+ return text
37
+
38
+
39
+ def transcribe(video_url="", audio=None):
40
+ if audio:
41
+ return transcribe_audio(audio)
42
+ elif video_url:
43
+ return transcribe_video(video_url)
44
 
45
 
46
  iface = gr.Interface(