Alejo760 commited on
Commit
9184b27
verified
1 Parent(s): 6906b0c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -22
app.py CHANGED
@@ -23,7 +23,6 @@ def transcribe_audio(audio_filepath):
23
  )
24
  return transcription.text
25
 
26
-
27
  def extract_text_from_pdf(pdf_path):
28
  text = ''
29
  with fitz.open(pdf_path) as doc:
@@ -31,8 +30,14 @@ def extract_text_from_pdf(pdf_path):
31
  text += page.get_text()
32
  return text
33
 
 
 
 
 
 
 
 
34
  def organize_clinical_record(current_text, transcription_text, pdf_text):
35
- # No cambiamos el clinical_record_template
36
  clinical_record_template = """
37
  MOTIVO DE CONSULTA: usa una frase en palabras del paciente entre comillas
38
 
@@ -70,7 +75,7 @@ def organize_clinical_record(current_text, transcription_text, pdf_text):
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}
@@ -84,14 +89,13 @@ def organize_clinical_record(current_text, transcription_text, pdf_text):
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,7 +108,7 @@ theme = gr.themes.Base(
104
  neutral_hue="neutral",
105
  )
106
 
107
- iterative_output = """
108
  MOTIVO DE CONSULTA:
109
 
110
  ENFERMEDAD ACTUAL:
@@ -132,25 +136,21 @@ with gr.Blocks(theme=theme) as iface:
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_filepath = 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
 
144
  def on_audio_change(audio_filepath, pdfs, current_text):
145
  print(f"on_audio_change: audio_filepath = {audio_filepath}")
146
- # Use the current_text passed in (which is current_state)
147
  if not current_text or not current_text.strip():
148
- current_text = iterative_output.value # Initial value if current_text is empty
149
  updated_text, debug_info = process_input(audio_filepath, pdfs, current_text)
150
- # Return updated_text to update both iterative_output and current_state
151
- return updated_text, debug_info, updated_text # Note: Return updated_text for current_state
152
-
153
- # Update outputs to include current_state
154
  audio_filepath.change(
155
  fn=on_audio_change,
156
  inputs=[audio_filepath, pdf_files, current_state],
@@ -160,14 +160,14 @@ with gr.Blocks(theme=theme) as iface:
160
  def on_pdfs_change(audio_filepath, pdfs, current_text):
161
  print(f"on_pdfs_change: pdfs = {pdfs}")
162
  if not current_text or not current_text.strip():
163
- current_text = iterative_output.value
164
  updated_text, debug_info = process_input(audio_filepath, pdfs, current_text)
165
- return updated_text, debug_info, updated_text # Return updated_text for current_state
166
-
167
  pdf_files.change(
168
  fn=on_pdfs_change,
169
  inputs=[audio_filepath, pdf_files, current_state],
170
  outputs=[iterative_output, debug_output, current_state]
171
  )
172
 
173
- iface.launch(auth=[("her", "her")])
 
23
  )
24
  return transcription.text
25
 
 
26
  def extract_text_from_pdf(pdf_path):
27
  text = ''
28
  with fitz.open(pdf_path) as doc:
 
30
  text += page.get_text()
31
  return text
32
 
33
+ def extract_texts_from_pdfs(pdfs):
34
+ text = ''
35
+ for pdf in pdfs:
36
+ pdf_text = extract_text_from_pdf(pdf.name)
37
+ text += pdf_text + "\n"
38
+ return text
39
+
40
  def organize_clinical_record(current_text, transcription_text, pdf_text):
 
41
  clinical_record_template = """
42
  MOTIVO DE CONSULTA: usa una frase en palabras del paciente entre comillas
43
 
 
75
  {clinical_record_template}
76
 
77
  Borrador Actual del Registro Cl铆nico:
78
+ {current_text}
79
 
80
  Nueva Informaci贸n de Audio:
81
  {transcription_text}
 
89
  organized_text = chat_groq.invoke(prompt)
90
  return organized_text
91
 
92
+ def process_input(audio, pdfs, current_text):
93
  transcription_text = transcribe_audio(audio)
94
  pdf_text = ''
95
  if pdfs:
96
  pdf_text = extract_texts_from_pdfs(pdfs)
97
+ organized_record = organize_clinical_record(current_text, transcription_text, pdf_text)
98
+ return organized_record.content, "Informaci贸n de depuraci贸n"
 
99
 
100
  theme = gr.themes.Base(
101
  primary_hue=gr.themes.Color(
 
108
  neutral_hue="neutral",
109
  )
110
 
111
+ initial_text = """
112
  MOTIVO DE CONSULTA:
113
 
114
  ENFERMEDAD ACTUAL:
 
136
 
137
  iterative_output = gr.Textbox(
138
  label="Registro Cl铆nico Organizado",
139
+ value=initial_text,
140
  lines=20,
141
+ )
142
+ current_state = gr.State(value=initial_text)
 
143
  audio_filepath = gr.Audio(sources=["microphone"], type="filepath", label="Entrada de Audio")
144
  pdf_files = gr.File(file_types=[".pdf"], label="Subir PDFs (puedes subir m煤ltiples archivos)", file_count="multiple")
145
  debug_output = gr.Textbox(label="Informaci贸n de Depuraci贸n", lines=10)
146
 
147
  def on_audio_change(audio_filepath, pdfs, current_text):
148
  print(f"on_audio_change: audio_filepath = {audio_filepath}")
 
149
  if not current_text or not current_text.strip():
150
+ current_text = initial_text
151
  updated_text, debug_info = process_input(audio_filepath, pdfs, current_text)
152
+ return updated_text, debug_info, updated_text
153
+
 
 
154
  audio_filepath.change(
155
  fn=on_audio_change,
156
  inputs=[audio_filepath, pdf_files, current_state],
 
160
  def on_pdfs_change(audio_filepath, pdfs, current_text):
161
  print(f"on_pdfs_change: pdfs = {pdfs}")
162
  if not current_text or not current_text.strip():
163
+ current_text = initial_text
164
  updated_text, debug_info = process_input(audio_filepath, pdfs, current_text)
165
+ return updated_text, debug_info, updated_text
166
+
167
  pdf_files.change(
168
  fn=on_pdfs_change,
169
  inputs=[audio_filepath, pdf_files, current_state],
170
  outputs=[iterative_output, debug_output, current_state]
171
  )
172
 
173
+ iface.launch(auth=[("her", "her")])