Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,10 +7,11 @@ from vertexai.generative_models import GenerativeModel, Part, SafetySetting
|
|
7 |
|
8 |
# Configuración global
|
9 |
generation_config = {
|
10 |
-
"max_output_tokens":
|
11 |
"temperature": 0,
|
12 |
"top_p": 0.8,
|
13 |
}
|
|
|
14 |
safety_settings = [
|
15 |
SafetySetting(
|
16 |
category=SafetySetting.HarmCategory.HARM_CATEGORY_HATE_SPEECH,
|
@@ -52,7 +53,6 @@ def parsear_con_llm_con_enumeraciones(texto_pdf: str, model: GenerativeModel) ->
|
|
52 |
...
|
53 |
}
|
54 |
"""
|
55 |
-
# Prompt que describe el formato “Preguntas” y “RESPUESTAS”
|
56 |
prompt = f"""
|
57 |
Eres un parser de texto que recibe el contenido de un PDF con dos secciones:
|
58 |
'Preguntas' y 'RESPUESTAS', cada una enumerada como '1.', '2)', etc.
|
@@ -73,7 +73,6 @@ Texto PDF:
|
|
73 |
Devuelve solo el JSON, sin explicaciones adicionales.
|
74 |
"""
|
75 |
part_text = Part.from_text(prompt)
|
76 |
-
|
77 |
response = model.generate_content(
|
78 |
[part_text],
|
79 |
generation_config=generation_config,
|
@@ -105,31 +104,47 @@ def comparar_preguntas_respuestas(dict_docente: dict, dict_alumno: dict) -> str:
|
|
105 |
return "\n".join(retroalimentacion)
|
106 |
|
107 |
def revisar_examen(json_cred, pdf_docente, pdf_alumno):
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
try:
|
109 |
configurar_credenciales(json_cred.name)
|
|
|
|
|
|
|
110 |
vertexai.init(project="deploygpt", location="us-central1")
|
111 |
|
112 |
-
# Leer texto
|
|
|
113 |
texto_docente = extraer_texto(pdf_docente.name)
|
|
|
|
|
114 |
texto_alumno = extraer_texto(pdf_alumno.name)
|
115 |
|
116 |
-
#
|
|
|
117 |
model = GenerativeModel(
|
118 |
"gemini-1.5-pro-001",
|
119 |
system_instruction=["Eres un parser estricto."]
|
120 |
)
|
121 |
-
|
122 |
-
# Parsear 'Preguntas' y 'RESPUESTAS' para docente y alumno
|
123 |
dict_docente = parsear_con_llm_con_enumeraciones(texto_docente, model)
|
|
|
|
|
124 |
dict_alumno = parsear_con_llm_con_enumeraciones(texto_alumno, model)
|
125 |
|
126 |
-
#
|
|
|
127 |
feedback = comparar_preguntas_respuestas(dict_docente, dict_alumno)
|
128 |
|
129 |
if len(feedback.strip()) < 5:
|
130 |
-
|
|
|
131 |
|
132 |
-
#
|
|
|
133 |
summary_prompt = f"""
|
134 |
Eres un profesor experto de bioquímica. Te muestro la comparación de preguntas y respuestas:
|
135 |
{feedback}
|
@@ -143,13 +158,14 @@ def revisar_examen(json_cred, pdf_docente, pdf_alumno):
|
|
143 |
safety_settings=safety_settings,
|
144 |
stream=False
|
145 |
)
|
146 |
-
|
147 |
|
148 |
-
|
149 |
-
return f"Error al procesar: {str(e)}"
|
150 |
|
151 |
-
|
|
|
152 |
|
|
|
153 |
interface = gr.Interface(
|
154 |
fn=revisar_examen,
|
155 |
inputs=[
|
@@ -157,13 +173,15 @@ interface = gr.Interface(
|
|
157 |
gr.File(label="PDF del Docente"),
|
158 |
gr.File(label="PDF Alumno")
|
159 |
],
|
160 |
-
outputs=gr.
|
161 |
-
|
|
|
162 |
description=(
|
163 |
"Sube tus credenciales, el PDF del docente y del alumno. El LLM "
|
164 |
"detectará enumeraciones (1., 2), etc.) en 'Preguntas' y 'RESPUESTAS' "
|
165 |
-
"
|
166 |
)
|
167 |
)
|
168 |
|
169 |
interface.launch(debug=True)
|
|
|
|
7 |
|
8 |
# Configuración global
|
9 |
generation_config = {
|
10 |
+
"max_output_tokens": 8192,
|
11 |
"temperature": 0,
|
12 |
"top_p": 0.8,
|
13 |
}
|
14 |
+
|
15 |
safety_settings = [
|
16 |
SafetySetting(
|
17 |
category=SafetySetting.HarmCategory.HARM_CATEGORY_HATE_SPEECH,
|
|
|
53 |
...
|
54 |
}
|
55 |
"""
|
|
|
56 |
prompt = f"""
|
57 |
Eres un parser de texto que recibe el contenido de un PDF con dos secciones:
|
58 |
'Preguntas' y 'RESPUESTAS', cada una enumerada como '1.', '2)', etc.
|
|
|
73 |
Devuelve solo el JSON, sin explicaciones adicionales.
|
74 |
"""
|
75 |
part_text = Part.from_text(prompt)
|
|
|
76 |
response = model.generate_content(
|
77 |
[part_text],
|
78 |
generation_config=generation_config,
|
|
|
104 |
return "\n".join(retroalimentacion)
|
105 |
|
106 |
def revisar_examen(json_cred, pdf_docente, pdf_alumno):
|
107 |
+
"""
|
108 |
+
Función convertida en generador para mostrar 'progreso' en Gradio:
|
109 |
+
en lugar de return final, se hace yield en varios pasos.
|
110 |
+
"""
|
111 |
+
# Paso 1: Configurar credenciales
|
112 |
+
yield "Cargando credenciales..."
|
113 |
try:
|
114 |
configurar_credenciales(json_cred.name)
|
115 |
+
|
116 |
+
# Paso 2: Inicializar Vertex AI
|
117 |
+
yield "Inicializando Vertex AI..."
|
118 |
vertexai.init(project="deploygpt", location="us-central1")
|
119 |
|
120 |
+
# Paso 3: Leer texto PDF
|
121 |
+
yield "Extrayendo texto del PDF del docente..."
|
122 |
texto_docente = extraer_texto(pdf_docente.name)
|
123 |
+
|
124 |
+
yield "Extrayendo texto del PDF del alumno..."
|
125 |
texto_alumno = extraer_texto(pdf_alumno.name)
|
126 |
|
127 |
+
# Paso 4: Invocar modelo
|
128 |
+
yield "Parseando preguntas y respuestas (Docente)..."
|
129 |
model = GenerativeModel(
|
130 |
"gemini-1.5-pro-001",
|
131 |
system_instruction=["Eres un parser estricto."]
|
132 |
)
|
|
|
|
|
133 |
dict_docente = parsear_con_llm_con_enumeraciones(texto_docente, model)
|
134 |
+
|
135 |
+
yield "Parseando preguntas y respuestas (Alumno)..."
|
136 |
dict_alumno = parsear_con_llm_con_enumeraciones(texto_alumno, model)
|
137 |
|
138 |
+
# Paso 5: Comparar
|
139 |
+
yield "Comparando respuestas..."
|
140 |
feedback = comparar_preguntas_respuestas(dict_docente, dict_alumno)
|
141 |
|
142 |
if len(feedback.strip()) < 5:
|
143 |
+
yield "No se encontraron preguntas o respuestas válidas."
|
144 |
+
return # terminamos la función
|
145 |
|
146 |
+
# Paso 6: Resumen final
|
147 |
+
yield "Generando resumen final..."
|
148 |
summary_prompt = f"""
|
149 |
Eres un profesor experto de bioquímica. Te muestro la comparación de preguntas y respuestas:
|
150 |
{feedback}
|
|
|
158 |
safety_settings=safety_settings,
|
159 |
stream=False
|
160 |
)
|
161 |
+
final_result = f"{feedback}\n\n**Resumen**\n{summary_resp.text.strip()}"
|
162 |
|
163 |
+
yield final_result
|
|
|
164 |
|
165 |
+
except Exception as e:
|
166 |
+
yield f"Error al procesar: {str(e)}"
|
167 |
|
168 |
+
# Interfaz Gradio
|
169 |
interface = gr.Interface(
|
170 |
fn=revisar_examen,
|
171 |
inputs=[
|
|
|
173 |
gr.File(label="PDF del Docente"),
|
174 |
gr.File(label="PDF Alumno")
|
175 |
],
|
176 |
+
# Cuando la función es un generador, definimos outputs="text" (o gr.Textbox()).
|
177 |
+
outputs="text",
|
178 |
+
title="Revisión de Exámenes con Enumeraciones (Progreso)",
|
179 |
description=(
|
180 |
"Sube tus credenciales, el PDF del docente y del alumno. El LLM "
|
181 |
"detectará enumeraciones (1., 2), etc.) en 'Preguntas' y 'RESPUESTAS' "
|
182 |
+
"y mostrará progreso a medida que avanza."
|
183 |
)
|
184 |
)
|
185 |
|
186 |
interface.launch(debug=True)
|
187 |
+
|