Alejo760 commited on
Commit
3998ea6
verified
1 Parent(s): 57d8133

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -77
app.py CHANGED
@@ -12,25 +12,16 @@ client = Groq(
12
  model_name = "llama-3.1-70b-versatile"
13
  chat_groq = ChatGroq(model=model_name)
14
 
15
- def transcribe_audio(audio_filepath):
16
- transcription_text = ""
17
- if audio_filepath is None:
18
- print("No se proporcion贸 audio.")
19
- return transcription_text
20
- try:
21
- print(f"Transcribiendo audio desde: {audio_filepath}")
22
- with open(audio_filepath, "rb") as file:
23
- transcription = client.audio.transcriptions.create(
24
- file=file,
25
- model="whisper-large-v3",
26
- response_format="json",
27
- temperature=0.0
28
- )
29
- print(f"Respuesta de transcripci贸n: {transcription}")
30
- transcription_text = transcription.text
31
- except Exception as e:
32
- print(f"Error en transcripci贸n de audio: {e}")
33
- return transcription_text
34
 
35
 
36
  def extract_text_from_pdf(pdf_path):
@@ -79,7 +70,7 @@ def organize_clinical_record(current_text, transcription_text, pdf_text):
79
  {clinical_record_template}
80
 
81
  Borrador Actual del Registro Cl铆nico:
82
- {current_text}
83
 
84
  Nueva Informaci贸n de Audio:
85
  {transcription_text}
@@ -91,52 +82,16 @@ def organize_clinical_record(current_text, transcription_text, pdf_text):
91
  """
92
 
93
  organized_text = chat_groq.invoke(prompt)
94
- return organized_text.content
95
-
96
- def process_input(audio_filepath, pdfs, current_text):
97
- transcription_text = transcribe_audio(audio_filepath)
98
- # Definimos el l铆mite m谩ximo de palabras
99
- max_words_per_prompt = 4500
100
- debug_info = ""
101
 
102
- # Preparamos la lista de textos de PDFs
103
- pdf_texts = []
 
104
  if pdfs:
105
- for pdf in pdfs:
106
- pdf_content = extract_text_from_pdf(pdf)
107
- pdf_texts.append((os.path.basename(pdf), pdf_content))
108
- debug_info += f"Le铆do PDF: {pdf}\n"
109
- else:
110
- debug_info += "No se proporcionaron PDFs.\n"
111
-
112
- # Combinamos los textos y contamos las palabras
113
- updated_text = current_text
114
- combined_texts = []
115
- total_words = len(updated_text.split()) + len(transcription_text.split())
116
-
117
- for pdf_name, pdf_content in pdf_texts:
118
- pdf_words = len(pdf_content.split())
119
- if total_words + pdf_words > max_words_per_prompt:
120
- # Procesamos los textos actuales
121
- pdf_text_combined = "\n".join([f"Contenido del PDF ({name}):\n{content}" for name, content in combined_texts])
122
- updated_text = organize_clinical_record(updated_text, transcription_text, pdf_text_combined)
123
- debug_info += f"Procesado lote de PDFs: {[name for name, _ in combined_texts]}\n"
124
- # Reiniciamos los textos
125
- combined_texts = [(pdf_name, pdf_content)]
126
- total_words = len(updated_text.split()) + len(transcription_text.split()) + pdf_words
127
- else:
128
- combined_texts.append((pdf_name, pdf_content))
129
- total_words += pdf_words
130
-
131
- # Procesamos el 煤ltimo lote si hay PDFs pendientes
132
- if combined_texts:
133
- pdf_text_combined = "\n".join([f"Contenido del PDF ({name}):\n{content}" for name, content in combined_texts])
134
- updated_text = organize_clinical_record(updated_text, transcription_text, pdf_text_combined)
135
- debug_info += f"Procesado lote de PDFs: {[name for name, _ in combined_texts]}\n"
136
-
137
- debug_info += f"Transcripci贸n de Audio: {transcription_text}\n"
138
-
139
- return updated_text, debug_info
140
 
141
  theme = gr.themes.Base(
142
  primary_hue=gr.themes.Color(
@@ -149,15 +104,7 @@ theme = gr.themes.Base(
149
  neutral_hue="neutral",
150
  )
151
 
152
- current_state = gr.State()
153
-
154
-
155
- with gr.Blocks(theme=theme) as iface:
156
- gr.Markdown("# Aplicaci贸n de Procesamiento de Audio y PDFs")
157
-
158
- iterative_output = gr.Textbox(
159
- label="Registro Cl铆nico Organizado",
160
- value="""
161
  MOTIVO DE CONSULTA:
162
 
163
  ENFERMEDAD ACTUAL:
@@ -178,13 +125,19 @@ with gr.Blocks(theme=theme) as iface:
178
  ** Medicamentos:
179
 
180
  AYUDAS DIAGNOSTICAS:
181
- """,
 
 
 
 
 
 
 
182
  lines=20,
183
  )
184
  # Move the State inside the Blocks context
185
- current_state = gr.State(value=iterative_output.value)
186
-
187
- audio_filepath = gr.Audio(sources=["microphone"], type="filepath", label="Entrada de Audio")
188
  pdf_files = gr.File(file_types=[".pdf"], label="Subir PDFs (puedes subir m煤ltiples archivos)", file_count="multiple")
189
  debug_output = gr.Textbox(label="Informaci贸n de Depuraci贸n", lines=10)
190
 
 
12
  model_name = "llama-3.1-70b-versatile"
13
  chat_groq = ChatGroq(model=model_name)
14
 
15
+ def transcribe_audio(audio):
16
+ filename = audio
17
+ with open(filename, "rb") as file:
18
+ transcription = client.audio.transcriptions.create(
19
+ file=(filename, file.read()),
20
+ model="whisper-large-v3",
21
+ response_format="json",
22
+ temperature=0.0
23
+ )
24
+ return transcription.text
 
 
 
 
 
 
 
 
 
25
 
26
 
27
  def extract_text_from_pdf(pdf_path):
 
70
  {clinical_record_template}
71
 
72
  Borrador Actual del Registro Cl铆nico:
73
+ {iterative_output}
74
 
75
  Nueva Informaci贸n de Audio:
76
  {transcription_text}
 
82
  """
83
 
84
  organized_text = chat_groq.invoke(prompt)
85
+ return organized_text
 
 
 
 
 
 
86
 
87
+ def process_input(audio, pdfs):
88
+ transcription_text = transcribe_audio(audio)
89
+ pdf_text = ''
90
  if pdfs:
91
+ pdf_text = extract_texts_from_pdfs(pdfs)
92
+ combined_text = transcription_text + "\n" + pdf_text
93
+ organized_record = organize_clinical_record(combined_text)
94
+ return organized_record.content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
  theme = gr.themes.Base(
97
  primary_hue=gr.themes.Color(
 
104
  neutral_hue="neutral",
105
  )
106
 
107
+ iterative_output = """
 
 
 
 
 
 
 
 
108
  MOTIVO DE CONSULTA:
109
 
110
  ENFERMEDAD ACTUAL:
 
125
  ** Medicamentos:
126
 
127
  AYUDAS DIAGNOSTICAS:
128
+ """
129
+
130
+ with gr.Blocks(theme=theme) as iface:
131
+ gr.Markdown("# Aplicaci贸n de Procesamiento de Audio y PDFs")
132
+
133
+ iterative_output = gr.Textbox(
134
+ label="Registro Cl铆nico Organizado",
135
+ value= iterative_output,
136
  lines=20,
137
  )
138
  # Move the State inside the Blocks context
139
+ current_state = gr.State(value=iterative_output)
140
+ audio = gr.Audio(sources=["microphone"], type="filepath", label="Entrada de Audio")
 
141
  pdf_files = gr.File(file_types=[".pdf"], label="Subir PDFs (puedes subir m煤ltiples archivos)", file_count="multiple")
142
  debug_output = gr.Textbox(label="Informaci贸n de Depuraci贸n", lines=10)
143