alexanderander30 commited on
Commit
8c92dca
1 Parent(s): 19963f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -46
app.py CHANGED
@@ -1,47 +1,49 @@
1
- import os
2
- import gradio as gr
3
- from faster_whisper import WhisperModel
4
- from pytube import YouTube
5
-
6
- # Inicializar el modelo Whisper
7
- model = WhisperModel("base", device="cuda" if gr.device.is_available("cuda") else "cpu", compute_type="float16")
8
-
9
- def transcribe_audio(audio_path):
10
- segments, _ = model.transcribe(audio_path, beam_size=5)
11
- return " ".join([segment.text for segment in segments])
12
-
13
- def process_youtube(youtube_url):
14
- try:
15
- yt = YouTube(youtube_url)
16
- audio_stream = yt.streams.filter(only_audio=True).first()
17
-
18
- if not os.path.exists("temp"):
19
- os.makedirs("temp")
20
-
21
- output_path = audio_stream.download(output_path="temp")
22
- return transcribe_audio(output_path)
23
- except Exception as e:
24
- return f"Error processing YouTube URL: {str(e)}"
25
-
26
- def transcribe(audio_file, youtube_url):
27
- if audio_file:
28
- return transcribe_audio(audio_file)
29
- elif youtube_url:
30
- return process_youtube(youtube_url)
31
- else:
32
- return "Please provide either an audio file or a YouTube URL."
33
-
34
- # Definir la interfaz de Gradio
35
- iface = gr.Interface(
36
- fn=transcribe,
37
- inputs=[
38
- gr.Audio(type="filepath", label="Upload Audio File"),
39
- gr.Textbox(label="Or Enter YouTube URL")
40
- ],
41
- outputs="text",
42
- title="Whisper Transcription App",
43
- description="Upload an audio file or provide a YouTube URL to transcribe."
44
- )
45
-
46
- # Lanzar la aplicación
 
 
47
  iface.launch()
 
1
+ import os
2
+ import gradio as gr
3
+ from faster_whisper import WhisperModel
4
+ from pytube import YouTube
5
+ import torch
6
+
7
+ # Inicializar el modelo Whisper
8
+ device = "cuda" if torch.cuda.is_available() else "cpu"
9
+ model = WhisperModel("base", device=device, compute_type="float16")
10
+
11
+ def transcribe_audio(audio_path):
12
+ segments, _ = model.transcribe(audio_path, beam_size=5)
13
+ return " ".join([segment.text for segment in segments])
14
+
15
+ def process_youtube(youtube_url):
16
+ try:
17
+ yt = YouTube(youtube_url)
18
+ audio_stream = yt.streams.filter(only_audio=True).first()
19
+
20
+ if not os.path.exists("temp"):
21
+ os.makedirs("temp")
22
+
23
+ output_path = audio_stream.download(output_path="temp")
24
+ return transcribe_audio(output_path)
25
+ except Exception as e:
26
+ return f"Error processing YouTube URL: {str(e)}"
27
+
28
+ def transcribe(audio_file, youtube_url):
29
+ if audio_file:
30
+ return transcribe_audio(audio_file)
31
+ elif youtube_url:
32
+ return process_youtube(youtube_url)
33
+ else:
34
+ return "Please provide either an audio file or a YouTube URL."
35
+
36
+ # Definir la interfaz de Gradio
37
+ iface = gr.Interface(
38
+ fn=transcribe,
39
+ inputs=[
40
+ gr.Audio(type="filepath", label="Upload Audio File"),
41
+ gr.Textbox(label="Or Enter YouTube URL")
42
+ ],
43
+ outputs="text",
44
+ title="Whisper Transcription App",
45
+ description="Upload an audio file or provide a YouTube URL to transcribe."
46
+ )
47
+
48
+ # Lanzar la aplicación
49
  iface.launch()