Spaces:
Running
Running
app.py
CHANGED
|
@@ -1,13 +1,16 @@
|
|
| 1 |
import os
|
| 2 |
import asyncio
|
| 3 |
-
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
# Configuración de la clave de API
|
| 7 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 8 |
if not GEMINI_API_KEY:
|
| 9 |
raise ValueError("La clave GEMINI_API_KEY no está configurada correctamente.")
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Instrucciones del sistema por defecto
|
| 13 |
instruction = """
|
|
@@ -72,64 +75,69 @@ La versión revisada del borrador de la resolución judicial, aplicando las corr
|
|
| 72 |
* La estructura original del borrador, los hechos probados y la fundamentación jurídica son intocables.
|
| 73 |
* Emplee el estilo jurídico imperativo y el estilo de redacción descrito.
|
| 74 |
"""
|
| 75 |
-
# Configuración del modelo de Google gemini-2.0-flash-exp
|
| 76 |
-
google_flash_exp_model = genai.GenerativeModel(
|
| 77 |
-
"gemini-2.0-pro-exp-02-05",
|
| 78 |
-
system_instruction=instruction,
|
| 79 |
-
generation_config={
|
| 80 |
-
"temperature": 0.3,
|
| 81 |
-
"top_p": 0.9,
|
| 82 |
-
"top_k": 40,
|
| 83 |
-
"max_output_tokens": 8000,
|
| 84 |
-
"response_mime_type": "text/plain",
|
| 85 |
-
},
|
| 86 |
-
)
|
| 87 |
|
| 88 |
-
# Configuración
|
| 89 |
-
|
| 90 |
-
"gemini-2.0-flash-thinking-exp-01-21",
|
| 91 |
system_instruction=instruction,
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
"response_mime_type": "text/plain",
|
| 98 |
-
},
|
| 99 |
)
|
| 100 |
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
try:
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
| 106 |
return response.text
|
| 107 |
except Exception as e:
|
| 108 |
return f"Error en {model_name}: {str(e)}"
|
| 109 |
|
|
|
|
| 110 |
async def combine_responses(borrador):
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
| 116 |
)
|
| 117 |
-
google_flash_exp_result = await google_flash_exp_task
|
| 118 |
-
google_gemini_ex_result = await google_gemini_ex_task
|
| 119 |
-
combined_result = f"**Google Gemini flash-exp:**\n{google_flash_exp_result}\n\n**Google gemini-exp-1206:**\n{google_gemini_ex_result}"
|
| 120 |
return combined_result
|
| 121 |
|
|
|
|
| 122 |
async def predict(borrador):
|
| 123 |
return await combine_responses(borrador)
|
| 124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
# Interfaz Gradio
|
| 126 |
with gr.Blocks() as demo:
|
| 127 |
gr.Markdown("### Mejorador de resoluciones judiciales - Derecho de Familia en Chile")
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
submit_btn = gr.Button("Enviar")
|
| 131 |
-
|
| 132 |
-
submit_btn.click(fn=
|
| 133 |
|
| 134 |
if __name__ == "__main__":
|
| 135 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import asyncio
|
| 3 |
+
from google import genai
|
| 4 |
+
from google.genai import types
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
# Configuración de la clave de API
|
| 8 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 9 |
if not GEMINI_API_KEY:
|
| 10 |
raise ValueError("La clave GEMINI_API_KEY no está configurada correctamente.")
|
| 11 |
+
|
| 12 |
+
# Se crea el cliente de la SDK para Gemini Developer API
|
| 13 |
+
client = genai.Client(api_key=GEMINI_API_KEY)
|
| 14 |
|
| 15 |
# Instrucciones del sistema por defecto
|
| 16 |
instruction = """
|
|
|
|
| 75 |
* La estructura original del borrador, los hechos probados y la fundamentación jurídica son intocables.
|
| 76 |
* Emplee el estilo jurídico imperativo y el estilo de redacción descrito.
|
| 77 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
# Configuración común de generación para ambos modelos
|
| 80 |
+
generation_config = types.GenerateContentConfig(
|
|
|
|
| 81 |
system_instruction=instruction,
|
| 82 |
+
temperature=0.3,
|
| 83 |
+
top_p=0.9,
|
| 84 |
+
top_k=40,
|
| 85 |
+
max_output_tokens=8000,
|
| 86 |
+
response_mime_type="text/plain"
|
|
|
|
|
|
|
| 87 |
)
|
| 88 |
|
| 89 |
+
# Definición de los nombres de modelo a utilizar
|
| 90 |
+
model_flash_exp = "gemini-2.0-pro-exp-02-05"
|
| 91 |
+
model_gemini_ex = "gemini-2.0-flash-thinking-exp-01-21"
|
| 92 |
+
|
| 93 |
+
# Función asíncrona para generar contenido usando el modelo indicado
|
| 94 |
+
async def generate_content(model_name, borrador):
|
| 95 |
try:
|
| 96 |
+
response = await client.aio.models.generate_content(
|
| 97 |
+
model=model_name,
|
| 98 |
+
contents=borrador,
|
| 99 |
+
config=generation_config
|
| 100 |
+
)
|
| 101 |
return response.text
|
| 102 |
except Exception as e:
|
| 103 |
return f"Error en {model_name}: {str(e)}"
|
| 104 |
|
| 105 |
+
# Función que combina las respuestas de ambos modelos
|
| 106 |
async def combine_responses(borrador):
|
| 107 |
+
flash_task = asyncio.create_task(generate_content(model_flash_exp, borrador))
|
| 108 |
+
gemini_ex_task = asyncio.create_task(generate_content(model_gemini_ex, borrador))
|
| 109 |
+
flash_result = await flash_task
|
| 110 |
+
gemini_ex_result = await gemini_ex_task
|
| 111 |
+
combined_result = (
|
| 112 |
+
f"**Google Gemini flash-exp:**\n{flash_result}\n\n"
|
| 113 |
+
f"**Google gemini-exp-1206:**\n{gemini_ex_result}"
|
| 114 |
)
|
|
|
|
|
|
|
|
|
|
| 115 |
return combined_result
|
| 116 |
|
| 117 |
+
# Función asíncrona principal
|
| 118 |
async def predict(borrador):
|
| 119 |
return await combine_responses(borrador)
|
| 120 |
|
| 121 |
+
# Función sincrónica que envuelve la llamada asíncrona (para Gradio)
|
| 122 |
+
def predict_sync(borrador):
|
| 123 |
+
return asyncio.run(predict(borrador))
|
| 124 |
+
|
| 125 |
# Interfaz Gradio
|
| 126 |
with gr.Blocks() as demo:
|
| 127 |
gr.Markdown("### Mejorador de resoluciones judiciales - Derecho de Familia en Chile")
|
| 128 |
+
borrador_input = gr.Textbox(
|
| 129 |
+
label="Borrador judicial",
|
| 130 |
+
placeholder="Escribe o pega el texto aquí...",
|
| 131 |
+
lines=10
|
| 132 |
+
)
|
| 133 |
+
output = gr.Textbox(
|
| 134 |
+
label="Resultado mejorado",
|
| 135 |
+
placeholder="El resultado aparecerá aquí...",
|
| 136 |
+
lines=10
|
| 137 |
+
)
|
| 138 |
submit_btn = gr.Button("Enviar")
|
| 139 |
+
|
| 140 |
+
submit_btn.click(fn=predict_sync, inputs=borrador_input, outputs=output)
|
| 141 |
|
| 142 |
if __name__ == "__main__":
|
| 143 |
demo.launch()
|