Update app.py
Browse files
app.py
CHANGED
@@ -1,62 +1,62 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from transformers import pipeline
|
3 |
-
import torch
|
4 |
-
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor
|
5 |
-
import os
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
# Disponibilidad de GPU
|
10 |
-
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
11 |
-
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
12 |
-
|
13 |
-
# ID del modelo
|
14 |
-
model_id = "openai/whisper-large-v3-turbo"
|
15 |
-
|
16 |
-
model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
17 |
-
model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
|
18 |
-
)
|
19 |
-
model.to(device)
|
20 |
-
|
21 |
-
processor = AutoProcessor.from_pretrained(model_id)
|
22 |
-
|
23 |
-
|
24 |
-
pipe = pipeline(
|
25 |
-
"automatic-speech-recognition",
|
26 |
-
model=model,
|
27 |
-
tokenizer=processor.tokenizer,
|
28 |
-
feature_extractor=processor.feature_extractor,
|
29 |
-
chunk_length_s=30,
|
30 |
-
batch_size=16, # Para ajustar dependiendo de la capacidad del sistema
|
31 |
-
torch_dtype=torch_dtype,
|
32 |
-
device=device,
|
33 |
-
)
|
34 |
-
|
35 |
-
|
36 |
-
def transcribe(audio):
|
37 |
-
if audio is None or not os.path.exists(audio):
|
38 |
-
return "Input de audio inv谩lido. Por favor, sube un archivo de audio v谩lido."
|
39 |
-
|
40 |
-
|
41 |
-
try:
|
42 |
-
result = pipe(audio)
|
43 |
-
return result["text"]
|
44 |
-
except Exception as e:
|
45 |
-
return f"Ocurri贸 un error durante la transcripci贸n: {e}"
|
46 |
-
|
47 |
-
|
48 |
-
iface = gr.Interface(
|
49 |
-
fn=transcribe,
|
50 |
-
inputs=gr.Audio(type="filepath", label="Subir archivo de audio"),
|
51 |
-
outputs=gr.Textbox(label="Resultado de la transcripci贸n"),
|
52 |
-
title="Transcripci贸n por reconocimiento de audio",
|
53 |
-
description="Sube tu audio para transcribir a texto.\n\
|
54 |
-
examples=[
|
55 |
-
["example_audio_1.wav"], # Ojo a帽adir el camino de los archivo
|
56 |
-
["example_audio_2.wav"],
|
57 |
-
],
|
58 |
-
live=True,
|
59 |
-
theme="default",
|
60 |
-
)
|
61 |
-
|
62 |
iface.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import torch
|
4 |
+
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor
|
5 |
+
import os
|
6 |
+
|
7 |
+
|
8 |
+
|
9 |
+
# Disponibilidad de GPU
|
10 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
11 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
12 |
+
|
13 |
+
# ID del modelo
|
14 |
+
model_id = "openai/whisper-large-v3-turbo"
|
15 |
+
|
16 |
+
model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
17 |
+
model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
|
18 |
+
)
|
19 |
+
model.to(device)
|
20 |
+
|
21 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
22 |
+
|
23 |
+
|
24 |
+
pipe = pipeline(
|
25 |
+
"automatic-speech-recognition",
|
26 |
+
model=model,
|
27 |
+
tokenizer=processor.tokenizer,
|
28 |
+
feature_extractor=processor.feature_extractor,
|
29 |
+
chunk_length_s=30,
|
30 |
+
batch_size=16, # Para ajustar dependiendo de la capacidad del sistema
|
31 |
+
torch_dtype=torch_dtype,
|
32 |
+
device=device,
|
33 |
+
)
|
34 |
+
|
35 |
+
|
36 |
+
def transcribe(audio):
|
37 |
+
if audio is None or not os.path.exists(audio):
|
38 |
+
return "Input de audio inv谩lido. Por favor, sube un archivo de audio v谩lido."
|
39 |
+
|
40 |
+
|
41 |
+
try:
|
42 |
+
result = pipe(audio)
|
43 |
+
return result["text"]
|
44 |
+
except Exception as e:
|
45 |
+
return f"Ocurri贸 un error durante la transcripci贸n: {e}"
|
46 |
+
|
47 |
+
|
48 |
+
iface = gr.Interface(
|
49 |
+
fn=transcribe,
|
50 |
+
inputs=gr.Audio(type="filepath", label="Subir archivo de audio"),
|
51 |
+
outputs=gr.Textbox(label="Resultado de la transcripci贸n"),
|
52 |
+
title="Transcripci贸n por reconocimiento de audio",
|
53 |
+
description="Sube tu audio para transcribir a texto.\n\nEV 2024",
|
54 |
+
examples=[
|
55 |
+
["example_audio_1.wav"], # Ojo a帽adir el camino de los archivo
|
56 |
+
["example_audio_2.wav"],
|
57 |
+
],
|
58 |
+
live=True,
|
59 |
+
theme="default",
|
60 |
+
)
|
61 |
+
|
62 |
iface.launch()
|