Spaces:
Running
Running
eberhenriquez94
commited on
Commit
•
f7876dc
1
Parent(s):
33dafb5
fg
Browse files
app.py
CHANGED
@@ -189,111 +189,57 @@ Una lista detallada de cada cambio realizado, indicando:
|
|
189 |
""".strip()
|
190 |
}
|
191 |
|
192 |
-
## Función para llamar al modelo de Google (con manejo de errores mejorado)
|
193 |
async def google_response(borrador):
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
except genai.errors.TimeoutError as e:
|
200 |
-
return f"Error en Google Generative AI (Timeout): {e.message}"
|
201 |
-
except Exception as e:
|
202 |
-
return f"Error inesperado en Google Generative AI: {e}"
|
203 |
-
|
204 |
-
|
205 |
|
206 |
async def nvidia_response(borrador):
|
207 |
try:
|
208 |
-
messages = [ {"role": "user", "content": borrador}]
|
209 |
completion = await asyncio.to_thread(
|
210 |
nvidia_client.chat.completions.create,
|
211 |
-
model="meta/llama-3.1-405b-instruct",
|
212 |
messages=messages,
|
213 |
temperature=0.5,
|
214 |
top_p=0.7,
|
215 |
max_tokens=4000
|
216 |
)
|
217 |
return completion.choices[0].message.content
|
218 |
-
except openai.error.APIError as e:
|
219 |
-
return f"Error en NVIDIA API (API Error): {e.message}"
|
220 |
-
except openai.error.Timeout as e:
|
221 |
-
return f"Error en NVIDIA API (Timeout): {e}"
|
222 |
except Exception as e:
|
223 |
-
return f"Error
|
224 |
|
|
|
|
|
|
|
|
|
225 |
|
226 |
-
|
227 |
-
|
228 |
|
229 |
-
google_task = asyncio.create_task(google_response(borrador))
|
230 |
-
nvidia_task = asyncio.create_task(nvidia_response(borrador))
|
231 |
|
232 |
-
yield
|
233 |
google_result = await google_task
|
234 |
|
235 |
|
236 |
-
yield
|
237 |
nvidia_result = await nvidia_task
|
238 |
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
yield chat_historial, "Listo", google_result, nvidia_result
|
243 |
|
|
|
244 |
|
245 |
async def predict(borrador):
|
246 |
-
chat_historial = []
|
247 |
results = []
|
248 |
-
async for
|
249 |
-
results = [
|
250 |
-
|
251 |
return results
|
252 |
|
253 |
-
# Configuración de la interfaz de usuario
|
254 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
255 |
-
|
256 |
-
with gr.Row():
|
257 |
-
gr.Markdown("### Prueba dos modelos IA para redactar resoluciones judiciales")
|
258 |
-
|
259 |
-
with gr.Row():
|
260 |
-
with gr.Column():
|
261 |
-
google_output = gr.Textbox(label="Texto Revisado por Google", lines=4)
|
262 |
-
with gr.Column():
|
263 |
-
nvidia_output = gr.Textbox(label="Texto Revisado por NVIDIA",lines=4)
|
264 |
-
|
265 |
-
|
266 |
-
chatbox = gr.Chatbot(label="Respuestas de los Modelos", type="messages")
|
267 |
-
input_text = gr.Textbox(
|
268 |
-
label="Borrador de Resolución",
|
269 |
-
placeholder="Introduce el texto que deseas perfeccionar...",
|
270 |
-
lines=6
|
271 |
-
)
|
272 |
-
submit_button = gr.Button("Enviar")
|
273 |
-
clear_button = gr.Button("Limpiar")
|
274 |
-
|
275 |
-
cargando = gr.Textbox(label="Estado", value="Listo", interactive=False)
|
276 |
-
chat_historial = gr.State([])
|
277 |
-
|
278 |
-
submit_button.click(
|
279 |
-
predict,
|
280 |
-
inputs=input_text,
|
281 |
-
outputs=[chatbox, cargando, google_output, nvidia_output]
|
282 |
-
)
|
283 |
-
clear_button.click(
|
284 |
-
lambda: ([], "Listo", None, None),
|
285 |
-
inputs=None,
|
286 |
-
outputs=[chatbox, cargando, google_output, nvidia_output]
|
287 |
-
)
|
288 |
-
|
289 |
-
gr.Examples(
|
290 |
-
examples=[
|
291 |
-
["El juez decide que el padre puede ver a su hijo los fines de semana."],
|
292 |
-
["Se ordena pagar una pensión a la madre para los gastos del niño."],
|
293 |
-
["El padre debe pagar la mitad de los gastos del colegio."],
|
294 |
-
],
|
295 |
-
inputs=input_text
|
296 |
-
)
|
297 |
|
298 |
if __name__ == "__main__":
|
299 |
-
demo.
|
|
|
189 |
""".strip()
|
190 |
}
|
191 |
|
|
|
192 |
async def google_response(borrador):
|
193 |
+
try:
|
194 |
+
response = await asyncio.to_thread(google_model.generate_content, borrador)
|
195 |
+
return response.text
|
196 |
+
except Exception as e:
|
197 |
+
return f"Error en Google: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
198 |
|
199 |
async def nvidia_response(borrador):
|
200 |
try:
|
201 |
+
messages = [nvidia_system_message, {"role": "user", "content": borrador}]
|
202 |
completion = await asyncio.to_thread(
|
203 |
nvidia_client.chat.completions.create,
|
204 |
+
model="meta/llama-3.1-405b-instruct", #<-REVISA EL MODELO
|
205 |
messages=messages,
|
206 |
temperature=0.5,
|
207 |
top_p=0.7,
|
208 |
max_tokens=4000
|
209 |
)
|
210 |
return completion.choices[0].message.content
|
|
|
|
|
|
|
|
|
211 |
except Exception as e:
|
212 |
+
return f"Error en NVIDIA: {e}"
|
213 |
|
214 |
+
async def procesar_respuestas(borrador):
|
215 |
+
temp_historial = []
|
216 |
+
temp_historial.append({"role": "user", "content": borrador})
|
217 |
+
yield temp_historial, "Llamando a las APIs...", None, None
|
218 |
|
219 |
+
google_task, nvidia_task = asyncio.gather(
|
220 |
+
google_response(borrador), nvidia_response(borrador))
|
221 |
|
|
|
|
|
222 |
|
223 |
+
yield temp_historial, "Esperando respuesta de Google...", None, None
|
224 |
google_result = await google_task
|
225 |
|
226 |
|
227 |
+
yield temp_historial, "Esperando respuesta de NVIDIA...", None, None
|
228 |
nvidia_result = await nvidia_task
|
229 |
|
230 |
+
temp_historial.append({"role": "assistant", "content": f"**Google:**\n{google_result}"})
|
231 |
+
temp_historial.append({"role": "assistant", "content": f"**NVIDIA:**\n{nvidia_result}"})
|
|
|
|
|
232 |
|
233 |
+
yield temp_historial, "Listo", google_result, nvidia_result
|
234 |
|
235 |
async def predict(borrador):
|
|
|
236 |
results = []
|
237 |
+
async for hist, status, google_res, nvidia_res in procesar_respuestas(borrador):
|
238 |
+
results = [hist, status, google_res, nvidia_res]
|
|
|
239 |
return results
|
240 |
|
|
|
241 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
242 |
+
# ... (Código de la interfaz de usuario SIN CAMBIOS) ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
243 |
|
244 |
if __name__ == "__main__":
|
245 |
+
demo.launch()
|