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

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()