Spaces:
Running
Running
app.py
CHANGED
|
@@ -5,6 +5,8 @@ import gradio as gr
|
|
| 5 |
|
| 6 |
# Configuración de claves de API
|
| 7 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
|
|
|
|
|
|
| 8 |
genai.configure(api_key=GEMINI_API_KEY)
|
| 9 |
|
| 10 |
# Instrucciones del sistema por defecto
|
|
@@ -14,31 +16,10 @@ Como Ministro de la Corte Suprema de Chile, especializado en Derecho de Familia,
|
|
| 14 |
El objetivo es elevar el texto a un estándar de excelencia en redacción jurídica, asegurando la máxima claridad, precisión, concisión y formalidad. **No debes modificar la estructura del borrador, tampoco agregar fundamentación o hechos. La mejora solo es gramatical, redaccional y estética lingüística jurídica.**
|
| 15 |
"""
|
| 16 |
|
| 17 |
-
# Configuración
|
| 18 |
-
google_gemini_model = genai.GenerativeModel(
|
| 19 |
-
|
| 20 |
-
generation_config={
|
| 21 |
-
"temperature": 0.5,
|
| 22 |
-
"top_p": 0.9,
|
| 23 |
-
"top_k": 40,
|
| 24 |
-
"max_output_tokens": 5000,
|
| 25 |
-
"response_mime_type": "text/plain",
|
| 26 |
-
},
|
| 27 |
-
)
|
| 28 |
|
| 29 |
-
# Configuración del modelo de Google LearnLM
|
| 30 |
-
google_learnlm_model = genai.GenerativeModel(
|
| 31 |
-
"learnlm-1.5-pro-experimental",
|
| 32 |
-
generation_config={
|
| 33 |
-
"temperature": 0.5,
|
| 34 |
-
"top_p": 0.9,
|
| 35 |
-
"top_k": 40,
|
| 36 |
-
"max_output_tokens": 5000,
|
| 37 |
-
"response_mime_type": "text/plain",
|
| 38 |
-
},
|
| 39 |
-
)
|
| 40 |
-
|
| 41 |
-
# Función genérica para generar contenido
|
| 42 |
async def generate_content(client, model_name, system_instruction, borrador):
|
| 43 |
try:
|
| 44 |
response = await asyncio.to_thread(client.generate_content, [system_instruction, borrador])
|
|
@@ -46,46 +27,30 @@ async def generate_content(client, model_name, system_instruction, borrador):
|
|
| 46 |
except Exception as e:
|
| 47 |
return f"Error en {model_name}: {str(e)}"
|
| 48 |
|
| 49 |
-
# Combina las respuestas de ambos modelos de Google
|
| 50 |
async def combine_responses(borrador):
|
| 51 |
system_instruction = default_system_instruction
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
combined_result = f"**Google Gemini:**\n{google_gemini_result}\n\n**Google LearnLM:**\n{google_learnlm_result}"
|
| 61 |
return combined_result
|
| 62 |
|
| 63 |
-
# Función de predicción
|
| 64 |
async def predict(borrador):
|
| 65 |
-
|
| 66 |
-
return result
|
| 67 |
|
| 68 |
-
# Interfaz Gradio
|
| 69 |
with gr.Blocks() as demo:
|
| 70 |
gr.Markdown("### Mejorador de resoluciones judiciales - Derecho de Familia en Chile")
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
borrador =
|
| 73 |
-
label="Borrador judicial",
|
| 74 |
-
placeholder="Escribe o pega el texto aquí...",
|
| 75 |
-
lines=10
|
| 76 |
-
)
|
| 77 |
-
output = gr.Textbox(
|
| 78 |
-
label="Resultado mejorado",
|
| 79 |
-
placeholder="El resultado aparecerá aquí...",
|
| 80 |
-
lines=10
|
| 81 |
-
)
|
| 82 |
-
submit_btn = gr.Button("Enviar") # Botón funcional para enviar el texto
|
| 83 |
-
|
| 84 |
-
submit_btn.click(
|
| 85 |
-
fn=lambda texto: asyncio.run(predict(texto)),
|
| 86 |
-
inputs=borrador,
|
| 87 |
-
outputs=output
|
| 88 |
-
)
|
| 89 |
|
| 90 |
if __name__ == "__main__":
|
| 91 |
demo.launch()
|
|
|
|
| 5 |
|
| 6 |
# Configuración de claves de API
|
| 7 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 8 |
+
if not GEMINI_API_KEY:
|
| 9 |
+
raise ValueError("La clave de API GEMINI_API_KEY no está configurada correctamente.")
|
| 10 |
genai.configure(api_key=GEMINI_API_KEY)
|
| 11 |
|
| 12 |
# Instrucciones del sistema por defecto
|
|
|
|
| 16 |
El objetivo es elevar el texto a un estándar de excelencia en redacción jurídica, asegurando la máxima claridad, precisión, concisión y formalidad. **No debes modificar la estructura del borrador, tampoco agregar fundamentación o hechos. La mejora solo es gramatical, redaccional y estética lingüística jurídica.**
|
| 17 |
"""
|
| 18 |
|
| 19 |
+
# Configuración de modelos de Google
|
| 20 |
+
google_gemini_model = genai.GenerativeModel("gemini-exp-1121", generation_config={...})
|
| 21 |
+
google_learnlm_model = genai.GenerativeModel("learnlm-1.5-pro-experimental", generation_config={...})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
async def generate_content(client, model_name, system_instruction, borrador):
|
| 24 |
try:
|
| 25 |
response = await asyncio.to_thread(client.generate_content, [system_instruction, borrador])
|
|
|
|
| 27 |
except Exception as e:
|
| 28 |
return f"Error en {model_name}: {str(e)}"
|
| 29 |
|
|
|
|
| 30 |
async def combine_responses(borrador):
|
| 31 |
system_instruction = default_system_instruction
|
| 32 |
+
google_gemini_task = asyncio.create_task(
|
| 33 |
+
generate_content(google_gemini_model, "Google Gemini", system_instruction, borrador)
|
| 34 |
+
)
|
| 35 |
+
google_learnlm_task = asyncio.create_task(
|
| 36 |
+
generate_content(google_learnlm_model, "Google LearnLM", system_instruction, borrador)
|
| 37 |
+
)
|
| 38 |
+
google_gemini_result = await google_gemini_task
|
| 39 |
+
google_learnlm_result = await google_learnlm_task
|
| 40 |
combined_result = f"**Google Gemini:**\n{google_gemini_result}\n\n**Google LearnLM:**\n{google_learnlm_result}"
|
| 41 |
return combined_result
|
| 42 |
|
|
|
|
| 43 |
async def predict(borrador):
|
| 44 |
+
return await combine_responses(borrador)
|
|
|
|
| 45 |
|
| 46 |
+
# Interfaz Gradio
|
| 47 |
with gr.Blocks() as demo:
|
| 48 |
gr.Markdown("### Mejorador de resoluciones judiciales - Derecho de Familia en Chile")
|
| 49 |
+
borrador = gr.Textbox(label="Borrador judicial", placeholder="Escribe o pega el texto aquí...", lines=10)
|
| 50 |
+
output = gr.Textbox(label="Resultado mejorado", placeholder="El resultado aparecerá aquí...", lines=10)
|
| 51 |
+
submit_btn = gr.Button("Enviar")
|
| 52 |
|
| 53 |
+
submit_btn.click(fn=predict, inputs=borrador, outputs=output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
if __name__ == "__main__":
|
| 56 |
demo.launch()
|