JymNils commited on
Commit
c5c600c
verified
1 Parent(s): 72b68fc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -32
app.py CHANGED
@@ -2,12 +2,12 @@ import torch
2
  from transformers import pipeline
3
  import gradio as gr
4
 
5
- # Configuraci贸n
6
  MODEL_NAME = "openai/whisper-small"
7
  BATCH_SIZE = 8
8
- device = 0 if torch.cuda.is_available() else "cpu"
9
 
10
- # Inicializar el pipeline
 
 
11
  pipe = pipeline(
12
  task="automatic-speech-recognition",
13
  model=MODEL_NAME,
@@ -30,57 +30,48 @@ def format_timestamp(seconds: float, always_include_hours: bool = False, decimal
30
 
31
  def transcribe(file, task, return_timestamps):
32
  if file is None:
33
- return "Por favor, sube un archivo o usa el micr贸fono."
34
-
35
- outputs = pipe(
36
- file,
37
- batch_size=BATCH_SIZE,
38
- generate_kwargs={"task": task},
39
- return_timestamps=return_timestamps
40
- )
41
-
42
  text = outputs["text"]
 
43
  if return_timestamps:
44
- chunks = outputs["chunks"]
45
- timestamps_text = [
46
  f"[{format_timestamp(chunk['timestamp'][0])} -> {format_timestamp(chunk['timestamp'][1])}] {chunk['text']}"
47
- for chunk in chunks
48
  ]
49
- text = "\n".join(timestamps_text)
50
  return text
51
 
52
- # Interfaz de Micr贸fono
53
  mic_transcribe = gr.Interface(
54
  fn=transcribe,
55
  inputs=[
56
- gr.Audio(sources="microphone", type="filepath", label="Micr贸fono"),
57
- gr.Radio(["transcribe", "translate"], label="Tarea", value="transcribe"),
58
- gr.Checkbox(value=False, label="Incluir marcas de tiempo"),
59
  ],
60
  outputs="text",
61
- title="Whisper Demo: Transcribir Micr贸fono",
62
- description=f"Demo usando el modelo [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}).",
63
- allow_flagging="never",
64
  )
65
 
66
- # Interfaz de Archivo
67
  file_transcribe = gr.Interface(
68
  fn=transcribe,
69
  inputs=[
70
- gr.Audio(sources="upload", type="filepath", label="Subir Archivo"),
71
- gr.Radio(["transcribe", "translate"], label="Tarea", value="transcribe"),
72
- gr.Checkbox(value=False, label="Incluir marcas de tiempo"),
73
  ],
74
  outputs="text",
75
- title="Whisper Demo: Transcribir Archivo",
76
- description=f"Sube un archivo de audio para transcribirlo con Whisper.",
77
- allow_flagging="never",
78
  )
79
 
80
- # Crear la aplicaci贸n con pesta帽as
81
  demo = gr.TabbedInterface(
82
  [mic_transcribe, file_transcribe],
83
- ["Transcribir Micr贸fono", "Transcribir Archivo Audio"]
84
  )
85
 
86
  if __name__ == "__main__":
 
2
  from transformers import pipeline
3
  import gradio as gr
4
 
 
5
  MODEL_NAME = "openai/whisper-small"
6
  BATCH_SIZE = 8
 
7
 
8
+ # Forzar uso de CPU
9
+ device = -1
10
+
11
  pipe = pipeline(
12
  task="automatic-speech-recognition",
13
  model=MODEL_NAME,
 
30
 
31
  def transcribe(file, task, return_timestamps):
32
  if file is None:
33
+ return "Error: No se proporcion贸 archivo de audio."
34
+
35
+ outputs = pipe(file, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=return_timestamps)
 
 
 
 
 
 
36
  text = outputs["text"]
37
+
38
  if return_timestamps:
39
+ timestamps = outputs["chunks"]
40
+ timestamps = [
41
  f"[{format_timestamp(chunk['timestamp'][0])} -> {format_timestamp(chunk['timestamp'][1])}] {chunk['text']}"
42
+ for chunk in timestamps
43
  ]
44
+ text = "\n".join(timestamps)
45
  return text
46
 
47
+ # Configuraci贸n de interfaces con sintaxis moderna (Gradio 4+)
48
  mic_transcribe = gr.Interface(
49
  fn=transcribe,
50
  inputs=[
51
+ gr.Audio(sources="microphone", type="filepath"),
52
+ gr.Radio(["transcribe", "translate"], label="Task", value="transcribe"),
53
+ gr.Checkbox(label="Return timestamps", value=False),
54
  ],
55
  outputs="text",
56
+ title="Whisper Demo: Transcribe Microphone",
57
+ flagging_mode="never", # 'allow_flagging' ahora es 'flagging_mode'
 
58
  )
59
 
 
60
  file_transcribe = gr.Interface(
61
  fn=transcribe,
62
  inputs=[
63
+ gr.Audio(sources="upload", type="filepath", label="Audio file"),
64
+ gr.Radio(["transcribe", "translate"], label="Task", value="transcribe"),
65
+ gr.Checkbox(label="Return timestamps", value=False),
66
  ],
67
  outputs="text",
68
+ title="Whisper Demo: Transcribe Audio File",
69
+ flagging_mode="never",
 
70
  )
71
 
 
72
  demo = gr.TabbedInterface(
73
  [mic_transcribe, file_transcribe],
74
+ ["Transcribe Microphone", "Transcribe Audio File"]
75
  )
76
 
77
  if __name__ == "__main__":