File size: 4,039 Bytes
7cba34a
 
 
 
e341366
7cba34a
d35d511
7cba34a
 
61e86f1
 
7cba34a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e341366
 
 
4d995a1
61e86f1
 
 
 
 
 
 
 
 
 
 
 
 
 
e341366
7cba34a
 
e341366
 
 
 
 
d35d511
897c746
 
 
 
 
 
7cba34a
61e86f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7cba34a
 
 
 
 
 
 
 
 
e341366
7cba34a
 
 
 
 
e341366
7cba34a
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import gradio as gr
import base64
import vertexai
from vertexai.generative_models import GenerativeModel, Part, SafetySetting
import os

# Configuraci贸n del modelo y par谩metros globales
generation_config = {
    "max_output_tokens": 8192,
    "temperature": 0.75,
    "top_p": 0.95,
}

safety_settings = [
    SafetySetting(
        category=SafetySetting.HarmCategory.HARM_CATEGORY_HATE_SPEECH,
        threshold=SafetySetting.HarmBlockThreshold.OFF
    ),
    SafetySetting(
        category=SafetySetting.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
        threshold=SafetySetting.HarmBlockThreshold.OFF
    ),
    SafetySetting(
        category=SafetySetting.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
        threshold=SafetySetting.HarmBlockThreshold.OFF
    ),
    SafetySetting(
        category=SafetySetting.HarmCategory.HARM_CATEGORY_HARASSMENT,
        threshold=SafetySetting.HarmBlockThreshold.OFF
    ),
]

def configurar_credenciales(json_path):
    """Configura las credenciales de Google Cloud usando un archivo JSON."""
    os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = json_path

def extraer_respuestas(pdf_data):
    """Simula la extracci贸n de preguntas y respuestas desde un PDF."""
    # Aqu铆 puedes usar bibliotecas como PyPDF2 para procesar el contenido del PDF
    # Por simplicidad, asumiremos texto plano (esto es un ejemplo)
    texto_extraido = base64.b64decode(pdf_data).decode("utf-8", errors="ignore")
    preguntas_respuestas = {}
    for linea in texto_extraido.split("\n"):
        if "Pregunta" in linea:
            pregunta = linea
            preguntas_respuestas[pregunta] = ""
        elif "Respuesta" in linea:
            preguntas_respuestas[pregunta] = linea.split(":")[-1].strip()
    return preguntas_respuestas

def revisar_examen(json_path, pdf_docente, pdf_alumno):
    """Funci贸n principal para comparar los PDFs del docente y del alumno."""
    try:
        # Configurar las credenciales
        configurar_credenciales(json_path)

        # Inicializar Vertex AI
        vertexai.init(project="deploygpt", location="us-central1")

        # Leer los datos de los PDFs desde la ruta proporcionada por Gradio
        with open(pdf_docente.name, "rb") as docente_file:
            docente_data = docente_file.read()
        
        with open(pdf_alumno.name, "rb") as alumno_file:
            alumno_data = alumno_file.read()

        # Extraer preguntas y respuestas del docente y del alumno
        preguntas_docente = extraer_respuestas(docente_data)
        respuestas_alumno = extraer_respuestas(alumno_data)

        # Construir la retroalimentaci贸n manualmente para evitar "alucinaciones"
        retroalimentacion = []
        for pregunta, respuesta_correcta in preguntas_docente.items():
            respuesta_alumno = respuestas_alumno.get(pregunta, "No respondida")
            if respuesta_alumno == "No respondida":
                retroalimentacion.append(f"Pregunta: {pregunta}\nNo fue asignada al alumno.\n")
            else:
                retroalimentacion.append(
                    f"Pregunta: {pregunta}\n"
                    f"Respuesta del alumno: {respuesta_alumno}\n"
                    f"Respuesta correcta: {respuesta_correcta}\n"
                )

        # Unir la retroalimentaci贸n en un solo texto
        feedback = "\n".join(retroalimentacion)
        return feedback

    except Exception as e:
        return f"Error al procesar: {str(e)}"

# Crear la interfaz con Gradio
interface = gr.Interface(
    fn=revisar_examen,
    inputs=[
        gr.File(label="Archivo de Credenciales JSON"),
        gr.File(label="PDF del Docente (Preguntas y Respuestas)"),
        gr.File(label="PDF del Alumno (Respuestas)")
    ],
    outputs=gr.Textbox(label="Retroalimentaci贸n del Examen"),
    title="Revisi贸n Autom谩tica de Ex谩menes",
    description="Sube el archivo de credenciales JSON de Google Cloud, el PDF del docente y el PDF del alumno para recibir una evaluaci贸n detallada."
)

# Lanzar la interfaz
interface.launch(debug=True)