ChiBenevisamPas commited on
Commit
c46f600
·
verified ·
1 Parent(s): f269663

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -18
app.py CHANGED
@@ -1,27 +1,40 @@
1
- from transformers import WhisperProcessor, WhisperForConditionalGeneration
2
  import gradio as gr
3
 
4
  # Load the Whisper model
5
- model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-base")
6
- processor = WhisperProcessor.from_pretrained("openai/whisper-base")
7
 
8
  def transcribe_video(video_file):
9
- audio_input = processor(audio_file=video_file.name, return_tensors="pt", sampling_rate=16000)
10
- # Perform the transcription
11
- with torch.no_grad():
12
- predicted_ids = model.generate(**audio_input)
13
- transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
14
-
15
- return transcription
16
-
17
- # Define Gradio interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  iface = gr.Interface(
19
  fn=transcribe_video,
20
- inputs=gr.Video(label="Upload Video"),
21
- outputs="text",
22
- title="Video Transcription",
23
- description="Upload a video file, and this tool will transcribe it."
24
  )
25
 
26
- # Launch the Gradio app
27
- iface.launch()
 
1
+ import whisper
2
  import gradio as gr
3
 
4
  # Load the Whisper model
5
+ model = whisper.load_model("base") # Choose 'tiny', 'base', 'small', 'medium', or 'large'
 
6
 
7
  def transcribe_video(video_file):
8
+ # Transcribe the video to generate subtitles
9
+ result = model.transcribe(video_file.name)
10
+
11
+ # Prepare the subtitles in SRT format
12
+ srt_file = "generated_subtitles.srt"
13
+
14
+ def write_srt(transcription, output_file):
15
+ with open(output_file, "w") as f:
16
+ for i, segment in enumerate(transcription['segments']):
17
+ start = segment['start']
18
+ end = segment['end']
19
+ text = segment['text']
20
+ # Format timestamps for SRT
21
+ start_time = whisper.utils.format_timestamp(start)
22
+ end_time = whisper.utils.format_timestamp(end)
23
+ f.write(f"{i + 1}\n")
24
+ f.write(f"{start_time} --> {end_time}\n")
25
+ f.write(f"{text.strip()}\n\n")
26
+
27
+ # Write the transcription as subtitles
28
+ write_srt(result, srt_file)
29
+
30
+ return srt_file # Return the generated SRT file
31
+
32
+ # Create a Gradio interface
33
  iface = gr.Interface(
34
  fn=transcribe_video,
35
+ inputs=gr.inputs.File(label="Upload Video"),
36
+ outputs=gr.outputs.File(label="Download Subtitles")
 
 
37
  )
38
 
39
+ # Launch the interface
40
+ iface.launch(share=True)