Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -42,13 +42,7 @@ def extraer_texto(pdf_path: str) -> str:
|
|
| 42 |
return texto_total
|
| 43 |
|
| 44 |
def parsear_con_llm(texto_pdf: str, model: GenerativeModel) -> dict:
|
| 45 |
-
"""
|
| 46 |
-
Usa el LLM para extraer preguntas y respuestas.
|
| 47 |
-
Devuelve un dict {"Pregunta X": "Respuesta X", ...}.
|
| 48 |
-
"""
|
| 49 |
# Instrucciones para parsear:
|
| 50 |
-
# - Buscar variaciones de "Pregunta" y "Respuesta" (mayúsculas, minúsculas, plural...)
|
| 51 |
-
# - Devolver un JSON limpio, sin texto extra.
|
| 52 |
prompt = f"""
|
| 53 |
Eres un parser de texto.
|
| 54 |
A continuación tienes el contenido de un PDF con un examen (o respuestas).
|
|
@@ -60,8 +54,7 @@ def parsear_con_llm(texto_pdf: str, model: GenerativeModel) -> dict:
|
|
| 60 |
|
| 61 |
{{
|
| 62 |
"Pregunta 1": "Texto de la respuesta",
|
| 63 |
-
"Pregunta 2": "Texto de la respuesta"
|
| 64 |
-
...
|
| 65 |
}}
|
| 66 |
|
| 67 |
Si hay preguntas sin respuesta, pon la respuesta como cadena vacía.
|
|
@@ -70,7 +63,8 @@ def parsear_con_llm(texto_pdf: str, model: GenerativeModel) -> dict:
|
|
| 70 |
Texto PDF:
|
| 71 |
{texto_pdf}
|
| 72 |
"""
|
| 73 |
-
part_text = Part(
|
|
|
|
| 74 |
response = model.generate_content(
|
| 75 |
[part_text],
|
| 76 |
generation_config=generation_config,
|
|
@@ -84,23 +78,14 @@ def parsear_con_llm(texto_pdf: str, model: GenerativeModel) -> dict:
|
|
| 84 |
else:
|
| 85 |
return {}
|
| 86 |
except:
|
| 87 |
-
# Si no se pudo parsear como JSON, devolvemos dict vacío
|
| 88 |
return {}
|
| 89 |
|
| 90 |
-
def comparar_preguntas_respuestas(
|
| 91 |
-
dict_docente: dict, dict_alumno: dict
|
| 92 |
-
) -> str:
|
| 93 |
-
"""
|
| 94 |
-
Recorre las preguntas del dict_docente y
|
| 95 |
-
compara con las respuestas del dict_alumno.
|
| 96 |
-
"""
|
| 97 |
retroalimentacion = []
|
| 98 |
for pregunta, resp_correcta in dict_docente.items():
|
| 99 |
resp_alumno = dict_alumno.get(pregunta, None)
|
| 100 |
if resp_alumno is None:
|
| 101 |
-
retroalimentacion.append(
|
| 102 |
-
f"**{pregunta}**\nNo fue asignada al alumno.\n"
|
| 103 |
-
)
|
| 104 |
else:
|
| 105 |
retroalimentacion.append(
|
| 106 |
f"**{pregunta}**\n"
|
|
@@ -111,52 +96,45 @@ def comparar_preguntas_respuestas(
|
|
| 111 |
|
| 112 |
def revisar_examen(json_cred, pdf_docente, pdf_alumno):
|
| 113 |
try:
|
| 114 |
-
# 1. Configurar credenciales
|
| 115 |
configurar_credenciales(json_cred.name)
|
| 116 |
-
|
| 117 |
-
# 2. Inicializar Vertex AI
|
| 118 |
vertexai.init(project="deploygpt", location="us-central1")
|
| 119 |
|
| 120 |
-
# 3. Extraer texto de PDFs
|
| 121 |
texto_docente = extraer_texto(pdf_docente.name)
|
| 122 |
texto_alumno = extraer_texto(pdf_alumno.name)
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
| 126 |
dict_docente = parsear_con_llm(texto_docente, model)
|
| 127 |
dict_alumno = parsear_con_llm(texto_alumno, model)
|
| 128 |
|
| 129 |
-
# 5. Comparar y generar retroalimentación
|
| 130 |
feedback = comparar_preguntas_respuestas(dict_docente, dict_alumno)
|
| 131 |
|
| 132 |
-
# 6. Generar un summary final con LLM (opcional)
|
| 133 |
-
# Queda a tu criterio si lo deseas:
|
| 134 |
if len(feedback.strip()) < 5:
|
| 135 |
-
return "No se encontraron preguntas
|
| 136 |
|
| 137 |
-
# Llamada final al modelo para un summary:
|
| 138 |
summary_prompt = f"""
|
| 139 |
Eres un profesor experto. Te muestro la comparación de preguntas y respuestas:
|
| 140 |
{feedback}
|
| 141 |
Por favor, genera un breve resumen del desempeño del alumno
|
| 142 |
sin inventar preguntas adicionales.
|
| 143 |
"""
|
| 144 |
-
summary_part = Part(
|
| 145 |
summary_resp = model.generate_content(
|
| 146 |
[summary_part],
|
| 147 |
generation_config=generation_config,
|
| 148 |
safety_settings=safety_settings,
|
| 149 |
stream=False
|
| 150 |
)
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
return f"{feedback}\n\n**Resumen**\n{summary_text}"
|
| 154 |
|
| 155 |
except Exception as e:
|
| 156 |
return f"Error al procesar: {str(e)}"
|
| 157 |
|
|
|
|
| 158 |
|
| 159 |
-
# Interfaz Gradio
|
| 160 |
interface = gr.Interface(
|
| 161 |
fn=revisar_examen,
|
| 162 |
inputs=[
|
|
@@ -170,3 +148,4 @@ interface = gr.Interface(
|
|
| 170 |
)
|
| 171 |
|
| 172 |
interface.launch(debug=True)
|
|
|
|
|
|
| 42 |
return texto_total
|
| 43 |
|
| 44 |
def parsear_con_llm(texto_pdf: str, model: GenerativeModel) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
# Instrucciones para parsear:
|
|
|
|
|
|
|
| 46 |
prompt = f"""
|
| 47 |
Eres un parser de texto.
|
| 48 |
A continuación tienes el contenido de un PDF con un examen (o respuestas).
|
|
|
|
| 54 |
|
| 55 |
{{
|
| 56 |
"Pregunta 1": "Texto de la respuesta",
|
| 57 |
+
"Pregunta 2": "Texto de la respuesta"
|
|
|
|
| 58 |
}}
|
| 59 |
|
| 60 |
Si hay preguntas sin respuesta, pon la respuesta como cadena vacía.
|
|
|
|
| 63 |
Texto PDF:
|
| 64 |
{texto_pdf}
|
| 65 |
"""
|
| 66 |
+
part_text = Part.from_text(prompt)
|
| 67 |
+
|
| 68 |
response = model.generate_content(
|
| 69 |
[part_text],
|
| 70 |
generation_config=generation_config,
|
|
|
|
| 78 |
else:
|
| 79 |
return {}
|
| 80 |
except:
|
|
|
|
| 81 |
return {}
|
| 82 |
|
| 83 |
+
def comparar_preguntas_respuestas(dict_docente: dict, dict_alumno: dict) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
retroalimentacion = []
|
| 85 |
for pregunta, resp_correcta in dict_docente.items():
|
| 86 |
resp_alumno = dict_alumno.get(pregunta, None)
|
| 87 |
if resp_alumno is None:
|
| 88 |
+
retroalimentacion.append(f"**{pregunta}**\nNo fue asignada al alumno.\n")
|
|
|
|
|
|
|
| 89 |
else:
|
| 90 |
retroalimentacion.append(
|
| 91 |
f"**{pregunta}**\n"
|
|
|
|
| 96 |
|
| 97 |
def revisar_examen(json_cred, pdf_docente, pdf_alumno):
|
| 98 |
try:
|
|
|
|
| 99 |
configurar_credenciales(json_cred.name)
|
|
|
|
|
|
|
| 100 |
vertexai.init(project="deploygpt", location="us-central1")
|
| 101 |
|
|
|
|
| 102 |
texto_docente = extraer_texto(pdf_docente.name)
|
| 103 |
texto_alumno = extraer_texto(pdf_alumno.name)
|
| 104 |
|
| 105 |
+
model = GenerativeModel(
|
| 106 |
+
"gemini-1.5-pro-001",
|
| 107 |
+
system_instruction=["Eres un parser estricto."]
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
dict_docente = parsear_con_llm(texto_docente, model)
|
| 111 |
dict_alumno = parsear_con_llm(texto_alumno, model)
|
| 112 |
|
|
|
|
| 113 |
feedback = comparar_preguntas_respuestas(dict_docente, dict_alumno)
|
| 114 |
|
|
|
|
|
|
|
| 115 |
if len(feedback.strip()) < 5:
|
| 116 |
+
return "No se encontraron preguntas o respuestas válidas."
|
| 117 |
|
|
|
|
| 118 |
summary_prompt = f"""
|
| 119 |
Eres un profesor experto. Te muestro la comparación de preguntas y respuestas:
|
| 120 |
{feedback}
|
| 121 |
Por favor, genera un breve resumen del desempeño del alumno
|
| 122 |
sin inventar preguntas adicionales.
|
| 123 |
"""
|
| 124 |
+
summary_part = Part.from_text(summary_prompt)
|
| 125 |
summary_resp = model.generate_content(
|
| 126 |
[summary_part],
|
| 127 |
generation_config=generation_config,
|
| 128 |
safety_settings=safety_settings,
|
| 129 |
stream=False
|
| 130 |
)
|
| 131 |
+
return f"{feedback}\n\n**Resumen**\n{summary_resp.text.strip()}"
|
|
|
|
|
|
|
| 132 |
|
| 133 |
except Exception as e:
|
| 134 |
return f"Error al procesar: {str(e)}"
|
| 135 |
|
| 136 |
+
import gradio as gr
|
| 137 |
|
|
|
|
| 138 |
interface = gr.Interface(
|
| 139 |
fn=revisar_examen,
|
| 140 |
inputs=[
|
|
|
|
| 148 |
)
|
| 149 |
|
| 150 |
interface.launch(debug=True)
|
| 151 |
+
|