Spaces:
Sleeping
Sleeping
alexanderander30
commited on
Commit
•
8c92dca
1
Parent(s):
19963f0
Update app.py
Browse files
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 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
return
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
return
|
31 |
-
|
32 |
-
return
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
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()
|