Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import fitz # PyMuPDF
|
| 4 |
-
import threading
|
| 5 |
-
import time
|
| 6 |
from groq import Groq
|
| 7 |
from langchain_groq import ChatGroq
|
| 8 |
|
|
@@ -15,15 +13,17 @@ model_name = "llama-3.1-70b-versatile"
|
|
| 15 |
chat_groq = ChatGroq(model=model_name)
|
| 16 |
|
| 17 |
def transcribe_audio(audio):
|
|
|
|
| 18 |
transcription_text = ""
|
| 19 |
try:
|
| 20 |
-
with open(
|
| 21 |
transcription = client.audio.transcriptions.create(
|
| 22 |
file=file,
|
| 23 |
model="whisper-large-v3",
|
| 24 |
response_format="json",
|
| 25 |
temperature=0.0
|
| 26 |
)
|
|
|
|
| 27 |
transcription_text = transcription.text
|
| 28 |
except Exception as e:
|
| 29 |
print(f"Error en transcripci贸n de audio: {e}")
|
|
@@ -61,7 +61,7 @@ def organize_clinical_record(current_text, transcription_text, pdf_text):
|
|
| 61 |
** Medicamentos:
|
| 62 |
|
| 63 |
AYUDAS DIAGNOSTICAS:
|
| 64 |
-
(ordenar todas las ayudas diagn贸sticas por fecha de forma que sea simple y sencillo leer los resultados para el m茅dico,
|
| 65 |
separa cada examen con una coma, usa minusculas y organiza por fechas.
|
| 66 |
por ejemplo:
|
| 67 |
11/10/2024: resultado 1 , resultado 2, ...
|
|
@@ -89,8 +89,10 @@ def organize_clinical_record(current_text, transcription_text, pdf_text):
|
|
| 89 |
organized_text = chat_groq.invoke(prompt)
|
| 90 |
return organized_text
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
| 94 |
MOTIVO DE CONSULTA:
|
| 95 |
|
| 96 |
ENFERMEDAD ACTUAL:
|
|
@@ -113,39 +115,66 @@ ANTECEDENTES:
|
|
| 113 |
AYUDAS DIAGNOSTICAS:
|
| 114 |
"""
|
| 115 |
|
| 116 |
-
|
| 117 |
-
def continuous_audio_processing():
|
| 118 |
-
global iterative_text
|
| 119 |
-
while True:
|
| 120 |
-
if audio_input.value:
|
| 121 |
-
audio_data = audio_input.value
|
| 122 |
-
transcription_text = transcribe_audio(audio_data)
|
| 123 |
-
iterative_text = organize_clinical_record(iterative_text, transcription_text, "")
|
| 124 |
-
# Actualizar el campo de texto en la interfaz
|
| 125 |
-
iterative_output.value = iterative_text
|
| 126 |
-
# Limpiar el audio
|
| 127 |
-
audio_input.clear()
|
| 128 |
-
time.sleep(60) # Esperar un minuto antes de la pr贸xima transcripci贸n
|
| 129 |
-
|
| 130 |
-
# Funci贸n para procesar el PDF inmediatamente despu茅s de subirlo
|
| 131 |
-
def on_pdf_upload(pdfs):
|
| 132 |
-
global iterative_text
|
| 133 |
debug_info = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
if pdfs:
|
| 135 |
-
pdf_text_combined = ""
|
| 136 |
for pdf in pdfs:
|
| 137 |
-
pdf_content = extract_text_from_pdf(pdf
|
| 138 |
-
|
| 139 |
-
debug_info += f"
|
| 140 |
-
iterative_text = organize_clinical_record(iterative_text, "", pdf_text_combined)
|
| 141 |
-
# Actualizar el campo de texto en la interfaz
|
| 142 |
-
iterative_output.value = iterative_text
|
| 143 |
else:
|
| 144 |
debug_info += "No se proporcionaron PDFs.\n"
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
theme = gr.themes.Base(
|
| 151 |
primary_hue=gr.themes.Color(
|
|
@@ -160,36 +189,50 @@ theme = gr.themes.Base(
|
|
| 160 |
|
| 161 |
with gr.Blocks(theme=theme) as iface:
|
| 162 |
gr.Markdown("# Aplicaci贸n de Procesamiento de Audio y PDFs")
|
| 163 |
-
|
| 164 |
# Campo de texto para mostrar y editar el registro cl铆nico
|
| 165 |
iterative_output = gr.Textbox(
|
| 166 |
label="Registro Cl铆nico Organizado",
|
| 167 |
-
value=
|
| 168 |
-
|
| 169 |
-
)
|
| 170 |
|
| 171 |
-
|
| 172 |
-
start_audio_button = gr.Button("Iniciar Grabaci贸n de Audio")
|
| 173 |
|
| 174 |
-
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
-
|
| 178 |
-
|
|
|
|
|
|
|
| 179 |
|
|
|
|
|
|
|
|
|
|
| 180 |
debug_output = gr.Textbox(label="Informaci贸n de Depuraci贸n", lines=10)
|
| 181 |
|
| 182 |
-
#
|
| 183 |
-
def
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
start_audio_button.visible = False
|
| 188 |
-
start_audio_button.update(visible=False)
|
| 189 |
-
|
| 190 |
-
start_audio_button.click(fn=start_recording)
|
| 191 |
|
| 192 |
-
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
iface.launch(auth=[("her", "her")])
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import fitz # PyMuPDF
|
|
|
|
|
|
|
| 4 |
from groq import Groq
|
| 5 |
from langchain_groq import ChatGroq
|
| 6 |
|
|
|
|
| 13 |
chat_groq = ChatGroq(model=model_name)
|
| 14 |
|
| 15 |
def transcribe_audio(audio):
|
| 16 |
+
filename = audio
|
| 17 |
transcription_text = ""
|
| 18 |
try:
|
| 19 |
+
with open(filename, "rb") as file:
|
| 20 |
transcription = client.audio.transcriptions.create(
|
| 21 |
file=file,
|
| 22 |
model="whisper-large-v3",
|
| 23 |
response_format="json",
|
| 24 |
temperature=0.0
|
| 25 |
)
|
| 26 |
+
# Acceder al atributo 'text' directamente
|
| 27 |
transcription_text = transcription.text
|
| 28 |
except Exception as e:
|
| 29 |
print(f"Error en transcripci贸n de audio: {e}")
|
|
|
|
| 61 |
** Medicamentos:
|
| 62 |
|
| 63 |
AYUDAS DIAGNOSTICAS:
|
| 64 |
+
(ordenar todas las ayudas diagn贸sticas por fecha de forma que sea simple y sencillo leer los resultados para el m茅dico,cuando se requiera presenta los resultados en miles, asegurate que no te falte ninguna ayuda, y no interpretes, solo pon los valores sin rango de normalidad en prosa
|
| 65 |
separa cada examen con una coma, usa minusculas y organiza por fechas.
|
| 66 |
por ejemplo:
|
| 67 |
11/10/2024: resultado 1 , resultado 2, ...
|
|
|
|
| 89 |
organized_text = chat_groq.invoke(prompt)
|
| 90 |
return organized_text
|
| 91 |
|
| 92 |
+
def process_input(audio, pdfs, current_text):
|
| 93 |
+
# Si no hay texto actual, utilizamos el texto inicial proporcionado
|
| 94 |
+
if not current_text.strip():
|
| 95 |
+
current_text = """
|
| 96 |
MOTIVO DE CONSULTA:
|
| 97 |
|
| 98 |
ENFERMEDAD ACTUAL:
|
|
|
|
| 115 |
AYUDAS DIAGNOSTICAS:
|
| 116 |
"""
|
| 117 |
|
| 118 |
+
transcription_text = transcribe_audio(audio) if audio else ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
debug_info = ""
|
| 120 |
+
|
| 121 |
+
# Definimos el l铆mite m谩ximo de palabras
|
| 122 |
+
max_words_per_prompt = 4500
|
| 123 |
+
|
| 124 |
+
# Preparamos la lista de textos de PDFs
|
| 125 |
+
pdf_texts = []
|
| 126 |
if pdfs:
|
|
|
|
| 127 |
for pdf in pdfs:
|
| 128 |
+
pdf_content = extract_text_from_pdf(pdf)
|
| 129 |
+
pdf_texts.append((os.path.basename(pdf), pdf_content))
|
| 130 |
+
debug_info += f"Le铆do PDF: {pdf}\n"
|
|
|
|
|
|
|
|
|
|
| 131 |
else:
|
| 132 |
debug_info += "No se proporcionaron PDFs.\n"
|
| 133 |
+
|
| 134 |
+
# Combinamos los textos y contamos las palabras
|
| 135 |
+
updated_text = current_text
|
| 136 |
+
combined_texts = []
|
| 137 |
+
total_words = len(updated_text.split()) + len(transcription_text.split())
|
| 138 |
+
|
| 139 |
+
for pdf_name, pdf_content in pdf_texts:
|
| 140 |
+
pdf_words = len(pdf_content.split())
|
| 141 |
+
if total_words + pdf_words > max_words_per_prompt:
|
| 142 |
+
# Procesamos los textos actuales
|
| 143 |
+
pdf_text_combined = "\n".join([f"Contenido del PDF ({name}):\n{content}" for name, content in combined_texts])
|
| 144 |
+
updated_text = organize_clinical_record(updated_text, transcription_text, pdf_text_combined)
|
| 145 |
+
debug_info += f"Procesado lote de PDFs: {[name for name, _ in combined_texts]}\n"
|
| 146 |
+
# Reiniciamos los textos
|
| 147 |
+
combined_texts = [(pdf_name, pdf_content)]
|
| 148 |
+
total_words = len(updated_text.split()) + len(transcription_text.split()) + pdf_words
|
| 149 |
+
else:
|
| 150 |
+
combined_texts.append((pdf_name, pdf_content))
|
| 151 |
+
total_words += pdf_words
|
| 152 |
+
|
| 153 |
+
# Procesamos el 煤ltimo lote si hay PDFs pendientes
|
| 154 |
+
if combined_texts:
|
| 155 |
+
pdf_text_combined = "\n".join([f"Contenido del PDF ({name}):\n{content}" for name, content in combined_texts])
|
| 156 |
+
updated_text = organize_clinical_record(updated_text, transcription_text, pdf_text_combined)
|
| 157 |
+
debug_info += f"Procesado lote de PDFs: {[name for name, _ in combined_texts]}\n"
|
| 158 |
+
|
| 159 |
+
debug_info += f"Transcripci贸n de Audio: {transcription_text}\n"
|
| 160 |
+
|
| 161 |
+
# Eliminar archivo de audio
|
| 162 |
+
if audio and os.path.exists(audio):
|
| 163 |
+
try:
|
| 164 |
+
os.remove(audio)
|
| 165 |
+
except Exception as e:
|
| 166 |
+
debug_info += f"Error al eliminar archivo de audio: {e}\n"
|
| 167 |
+
|
| 168 |
+
# Eliminar archivos PDF
|
| 169 |
+
if pdfs:
|
| 170 |
+
for pdf in pdfs:
|
| 171 |
+
if os.path.exists(pdf):
|
| 172 |
+
try:
|
| 173 |
+
os.remove(pdf)
|
| 174 |
+
except Exception as e:
|
| 175 |
+
debug_info += f"Error al eliminar PDF {pdf}: {e}\n"
|
| 176 |
+
|
| 177 |
+
return updated_text, debug_info
|
| 178 |
|
| 179 |
theme = gr.themes.Base(
|
| 180 |
primary_hue=gr.themes.Color(
|
|
|
|
| 189 |
|
| 190 |
with gr.Blocks(theme=theme) as iface:
|
| 191 |
gr.Markdown("# Aplicaci贸n de Procesamiento de Audio y PDFs")
|
| 192 |
+
|
| 193 |
# Campo de texto para mostrar y editar el registro cl铆nico
|
| 194 |
iterative_output = gr.Textbox(
|
| 195 |
label="Registro Cl铆nico Organizado",
|
| 196 |
+
value="""
|
| 197 |
+
MOTIVO DE CONSULTA:
|
|
|
|
| 198 |
|
| 199 |
+
ENFERMEDAD ACTUAL:
|
|
|
|
| 200 |
|
| 201 |
+
REVISI脫N POR SISTEMAS:
|
| 202 |
+
|
| 203 |
+
ANTECEDENTES:
|
| 204 |
+
**Patol贸gicos:
|
| 205 |
+
** Al茅rgicos:
|
| 206 |
+
** T贸xicos:
|
| 207 |
+
** Familiares:
|
| 208 |
+
** Transfusionales:
|
| 209 |
+
** Traum谩ticos:
|
| 210 |
+
** Ginecol贸gicos:
|
| 211 |
+
** Quir煤rgicos:
|
| 212 |
+
** Estado de vacunaci贸n:
|
| 213 |
+
** Hospitalizaciones previas:
|
| 214 |
+
** Medicamentos:
|
| 215 |
|
| 216 |
+
AYUDAS DIAGNOSTICAS:
|
| 217 |
+
""",
|
| 218 |
+
lines=20,
|
| 219 |
+
)
|
| 220 |
|
| 221 |
+
audio_input = gr.Audio(sources=["microphone"], type="filepath", label="Entrada de Audio")
|
| 222 |
+
pdf_files = gr.File(file_types=[".pdf"], label="Subir PDFs (puedes subir m煤ltiples archivos)", file_count="multiple", type="filepath")
|
| 223 |
+
process_button = gr.Button("Iniciar Procesamiento")
|
| 224 |
debug_output = gr.Textbox(label="Informaci贸n de Depuraci贸n", lines=10)
|
| 225 |
|
| 226 |
+
# Funci贸n para manejar el clic del bot贸n y actualizar el texto
|
| 227 |
+
def on_process_click(audio, pdfs, current_text):
|
| 228 |
+
updated_text, debug_info = process_input(audio, pdfs, current_text)
|
| 229 |
+
# Devolver None para resetear audio_input y pdf_files
|
| 230 |
+
return updated_text, debug_info, None, None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
|
| 232 |
+
process_button.click(
|
| 233 |
+
fn=on_process_click,
|
| 234 |
+
inputs=[audio_input, pdf_files, iterative_output],
|
| 235 |
+
outputs=[iterative_output, debug_output, audio_input, pdf_files]
|
| 236 |
+
)
|
| 237 |
|
| 238 |
iface.launch(auth=[("her", "her")])
|