vmasmitja
commited on
Commit
路
fd6fb97
1
Parent(s):
ab01055
Fix app initialization issue
Browse files- app.py +122 -18
- requirements.txt +4 -2
app.py
CHANGED
@@ -1,30 +1,134 @@
|
|
1 |
-
import
|
2 |
import numpy as np
|
3 |
import librosa
|
|
|
|
|
4 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
#
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
#
|
10 |
-
def
|
11 |
-
|
12 |
-
raise ValueError("No se ha proporcionado un archivo de audio.")
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
# Crear la interfaz Gradio
|
20 |
demo = gr.Interface(
|
21 |
-
fn=
|
22 |
-
inputs=
|
23 |
outputs="text",
|
24 |
-
title="
|
25 |
-
description="
|
26 |
)
|
27 |
|
28 |
-
# Lanzar la aplicaci贸n
|
29 |
if __name__ == "__main__":
|
30 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import ffmpeg
|
2 |
import numpy as np
|
3 |
import librosa
|
4 |
+
import os
|
5 |
+
import time
|
6 |
from transformers import pipeline
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
# Modelos de Hugging Face para espa帽ol
|
10 |
+
transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-small", language="es")
|
11 |
+
summarizer = pipeline("summarization", model="mrm8488/bert2bert_shared-spanish-finetuned-summarization")
|
12 |
+
|
13 |
+
# Variable global para estados y transcripciones
|
14 |
+
state = {"status": "Esperando transmisi贸n...", "transcriptions": [], "summary": ""}
|
15 |
+
|
16 |
+
# Funci贸n para esperar inicio de transmisi贸n RTMP
|
17 |
+
def wait_for_stream(rtmp_url):
|
18 |
+
state["status"] = "Esperando transmisi贸n..."
|
19 |
+
print(state["status"])
|
20 |
+
while True:
|
21 |
+
try:
|
22 |
+
probe = ffmpeg.probe(rtmp_url, format='flv')
|
23 |
+
if probe:
|
24 |
+
state["status"] = "隆Transmisi贸n detectada!"
|
25 |
+
print(state["status"])
|
26 |
+
break
|
27 |
+
except ffmpeg.Error:
|
28 |
+
time.sleep(5)
|
29 |
+
|
30 |
+
# Procesar transmisi贸n RTMP en tiempo real
|
31 |
+
def process_rtmp(rtmp_url):
|
32 |
+
audio_output = "stream_audio.wav"
|
33 |
+
transcription = []
|
34 |
+
|
35 |
+
state["status"] = "Transcribiendo en tiempo real..."
|
36 |
+
print(state["status"])
|
37 |
+
|
38 |
+
# Iniciar FFmpeg para extraer audio en tiempo real
|
39 |
+
process = (
|
40 |
+
ffmpeg
|
41 |
+
.input(rtmp_url, format='flv')
|
42 |
+
.output(audio_output, format='wav', acodec='pcm_s16le', ac=1, ar=16000)
|
43 |
+
.overwrite_output()
|
44 |
+
.run_async(pipe_stdout=True, pipe_stderr=True)
|
45 |
+
)
|
46 |
+
|
47 |
+
try:
|
48 |
+
while True:
|
49 |
+
if os.path.exists(audio_output):
|
50 |
+
audio_data, _ = librosa.load(audio_output, sr=16000)
|
51 |
+
if len(audio_data) > 0:
|
52 |
+
text = transcriber(np.array(audio_data))["text"]
|
53 |
+
transcription.append(text)
|
54 |
+
state["transcriptions"].append(text)
|
55 |
+
print(f"Transcripci贸n: {text}")
|
56 |
+
time.sleep(2) # Procesar cada 2 segundos
|
57 |
+
except KeyboardInterrupt:
|
58 |
+
process.terminate()
|
59 |
+
|
60 |
+
state["status"] = "Transmisi贸n finalizada"
|
61 |
+
print(state["status"])
|
62 |
+
return " ".join(transcription)
|
63 |
|
64 |
+
# Generar resumen
|
65 |
+
def finalize_summary(transcription):
|
66 |
+
state["status"] = "Generando resumen..."
|
67 |
+
print(state["status"])
|
68 |
+
summary = summarizer(transcription, max_length=100, min_length=30, do_sample=False)[0]["summary_text"]
|
69 |
+
state["summary"] = summary
|
70 |
+
state["status"] = "Resumen listo"
|
71 |
+
print(state["status"])
|
72 |
+
return summary
|
73 |
|
74 |
+
# Flujo principal
|
75 |
+
def process_and_finalize():
|
76 |
+
rtmp_url = "rtmp://37.27.213.138/live/stream"
|
|
|
77 |
|
78 |
+
# Esperar inicio de transmisi贸n
|
79 |
+
wait_for_stream(rtmp_url)
|
80 |
+
|
81 |
+
# Procesar transmisi贸n y transcribir en tiempo real
|
82 |
+
transcription = process_rtmp(rtmp_url)
|
83 |
+
|
84 |
+
# Generar resumen
|
85 |
+
summary = finalize_summary(transcription)
|
86 |
+
|
87 |
+
return summary
|
88 |
+
|
89 |
+
# Interfaz Gradio
|
90 |
+
def display_status():
|
91 |
+
# Mostrar estados y transcripciones en tiempo real
|
92 |
+
return f"Estado: {state['status']}\n\nTranscripciones:\n" + "\n".join(state["transcriptions"]) + f"\n\nResumen final:\n{state['summary']}"
|
93 |
|
|
|
94 |
demo = gr.Interface(
|
95 |
+
fn=display_status,
|
96 |
+
inputs=None,
|
97 |
outputs="text",
|
98 |
+
title="Estado de Transmisi贸n y Resumen",
|
99 |
+
description="Muestra el estado de la transmisi贸n, transcripciones en tiempo real y el resumen generado."
|
100 |
)
|
101 |
|
|
|
102 |
if __name__ == "__main__":
|
103 |
+
demo.launch()
|
104 |
+
|
105 |
+
# import gradio as gr
|
106 |
+
# import numpy as np
|
107 |
+
# import librosa
|
108 |
+
# from transformers import pipeline
|
109 |
+
|
110 |
+
# # Cargar el modelo de transcripci贸n Whisper
|
111 |
+
# transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-small")
|
112 |
+
|
113 |
+
# # Funci贸n para procesar y transcribir el audio
|
114 |
+
# def transcribe(audio):
|
115 |
+
# if audio is None:
|
116 |
+
# raise ValueError("No se ha proporcionado un archivo de audio.")
|
117 |
+
|
118 |
+
# # Cargar el archivo de audio como un array NumPy
|
119 |
+
# audio_data, _ = librosa.load(audio, sr=16000) # Resample a 16 kHz
|
120 |
+
# result = transcriber(np.array(audio_data))
|
121 |
+
# return result["text"]
|
122 |
+
|
123 |
+
# # Crear la interfaz Gradio
|
124 |
+
# demo = gr.Interface(
|
125 |
+
# fn=transcribe,
|
126 |
+
# inputs=gr.Audio(type="filepath"), # Subida de archivos de audio
|
127 |
+
# outputs="text",
|
128 |
+
# title="Transcripci贸n de Audio en Vivo",
|
129 |
+
# description="Sube un archivo de audio para transcribir su contenido autom谩ticamente."
|
130 |
+
# )
|
131 |
+
|
132 |
+
# # Lanzar la aplicaci贸n
|
133 |
+
# if __name__ == "__main__":
|
134 |
+
# demo.launch()
|
requirements.txt
CHANGED
@@ -1,4 +1,6 @@
|
|
|
|
|
|
|
|
1 |
transformers
|
2 |
gradio
|
3 |
-
torch
|
4 |
-
librosa
|
|
|
1 |
+
ffmpeg-python
|
2 |
+
numpy
|
3 |
+
librosa
|
4 |
transformers
|
5 |
gradio
|
6 |
+
torch
|
|