Rajor78 commited on
Commit
4220be5
verified
1 Parent(s): bcf4d82

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -2
app.py CHANGED
@@ -1,39 +1,57 @@
1
  import gradio as gr
2
  import subprocess
3
  import os
 
4
  from transformers import WhisperProcessor, WhisperForConditionalGeneration
5
  import language_tool_python
6
  from pydub import AudioSegment
7
  from docx import Document
8
 
 
9
  def extract_audio(video_path, audio_path):
10
  command = f"ffmpeg -i '{video_path}' -ar 16000 -ac 1 -c:a pcm_s16le '{audio_path}' -y"
11
  subprocess.run(command, shell=True, check=True)
12
  return audio_path
13
 
 
14
  def transcribe_audio(audio_path):
 
15
  processor = WhisperProcessor.from_pretrained("openai/whisper-base")
16
  model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-base")
17
 
18
- audio_input = processor(audio_path, return_tensors="pt", sampling_rate=16000)
19
- result = model.generate(**audio_input)
 
 
 
 
 
 
20
  transcription = processor.decode(result[0], skip_special_tokens=True)
21
 
22
  return transcription
23
 
 
24
  def correct_text(text):
25
  tool = language_tool_python.LanguageTool('es')
26
  matches = tool.check(text)
27
  return language_tool_python.utils.correct(text, matches)
28
 
 
29
  def process_video(video_file):
30
  video_path = video_file.name
31
  audio_path = os.path.splitext(video_path)[0] + '.wav'
32
 
 
33
  extract_audio(video_path, audio_path)
 
 
34
  transcribed_text = transcribe_audio(audio_path)
 
 
35
  corrected_text = correct_text(transcribed_text)
36
 
 
37
  doc = Document()
38
  doc.add_paragraph(corrected_text)
39
  doc_path = "transcription.docx"
@@ -41,6 +59,7 @@ def process_video(video_file):
41
 
42
  return corrected_text, doc_path
43
 
 
44
  demo = gr.Interface(
45
  fn=process_video,
46
  inputs=gr.File(label="Sube un archivo de video"),
 
1
  import gradio as gr
2
  import subprocess
3
  import os
4
+ import librosa
5
  from transformers import WhisperProcessor, WhisperForConditionalGeneration
6
  import language_tool_python
7
  from pydub import AudioSegment
8
  from docx import Document
9
 
10
+ # Funci贸n para extraer audio de video
11
  def extract_audio(video_path, audio_path):
12
  command = f"ffmpeg -i '{video_path}' -ar 16000 -ac 1 -c:a pcm_s16le '{audio_path}' -y"
13
  subprocess.run(command, shell=True, check=True)
14
  return audio_path
15
 
16
+ # Funci贸n para transcribir el audio usando Whisper
17
  def transcribe_audio(audio_path):
18
+ # Cargar el procesador y modelo de Whisper
19
  processor = WhisperProcessor.from_pretrained("openai/whisper-base")
20
  model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-base")
21
 
22
+ # Cargar el archivo de audio usando librosa
23
+ audio_input, _ = librosa.load(audio_path, sr=16000)
24
+
25
+ # Preprocesar el audio para el modelo
26
+ inputs = processor(audio_input, return_tensors="pt", sampling_rate=16000)
27
+
28
+ # Realizar la transcripci贸n
29
+ result = model.generate(**inputs)
30
  transcription = processor.decode(result[0], skip_special_tokens=True)
31
 
32
  return transcription
33
 
34
+ # Funci贸n para corregir el texto transcrito con LanguageTool
35
  def correct_text(text):
36
  tool = language_tool_python.LanguageTool('es')
37
  matches = tool.check(text)
38
  return language_tool_python.utils.correct(text, matches)
39
 
40
+ # Funci贸n principal que procesa el video
41
  def process_video(video_file):
42
  video_path = video_file.name
43
  audio_path = os.path.splitext(video_path)[0] + '.wav'
44
 
45
+ # Extraer el audio del video
46
  extract_audio(video_path, audio_path)
47
+
48
+ # Transcribir el audio
49
  transcribed_text = transcribe_audio(audio_path)
50
+
51
+ # Corregir la transcripci贸n
52
  corrected_text = correct_text(transcribed_text)
53
 
54
+ # Crear un documento Word con la transcripci贸n corregida
55
  doc = Document()
56
  doc.add_paragraph(corrected_text)
57
  doc_path = "transcription.docx"
 
59
 
60
  return corrected_text, doc_path
61
 
62
+ # Interfaz de Gradio
63
  demo = gr.Interface(
64
  fn=process_video,
65
  inputs=gr.File(label="Sube un archivo de video"),