riabayonaor commited on
Commit
2ab855f
1 Parent(s): 6d9da6e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -38
app.py CHANGED
@@ -1,40 +1,52 @@
1
 
2
  import gradio as gr
3
- import random
4
- from transformers import pipeline, set_seed
5
-
6
- set_seed(42) # Para consistencia en la generación
7
-
8
- # Inicializar el pipeline con tu modelo y especificar el dispositivo
9
- # Use a pipeline as a high-level helper
10
- from transformers import pipeline
11
-
12
- pipe = pipeline("text-generation", model="datificate/gpt2-small-spanish")
13
-
14
- def generate_problem(topic, max_length=50):
15
- problem_prompt = f"Genera un problema de matemáticas sobre {topic}."
16
- # Generar texto con longitud máxima limitada
17
- problem_output = pipe(problem_prompt, max_length=max_length, num_return_sequences=1)[0]['generated_text']
18
- problem, solution = problem_output.split('La solución es')
19
- return problem.strip(), solution.strip()
20
-
21
- def generate_fake_answers(real_solution, num_fakes=3):
22
- fake_answers = [str(int(real_solution) + i) for i in range(1, num_fakes + 1)]
23
- return fake_answers
24
-
25
- def math_problem_solver(topic):
26
- problem, solution = generate_problem(topic)
27
- correct_answer = solution
28
- fake_answers = generate_fake_answers(solution)
29
- all_answers = fake_answers + [correct_answer]
30
- random.shuffle(all_answers)
31
- return problem, all_answers, correct_answer
32
-
33
- iface = gr.Interface(
34
- fn=math_problem_solver,
35
- inputs=gr.Dropdown(choices=["Problemas de Preálgebra", "Problemas de Funciones"], label="Selecciona el tema"),
36
- outputs=[gr.Textbox(label="Problema"), gr.Radio(label="Opciones de respuesta"), gr.Textbox(label="Respuesta correcta")],
37
- examples=[["Problemas de Preálgebra"], ["Problemas de Funciones"]],
38
- )
39
-
40
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
1
 
2
  import gradio as gr
3
+ from inference import load_model, generate
4
+ # Imaginemos que 'generate' es una función adaptada en 'inference.py' que ya maneja la generación de problemas matemáticos.
5
+
6
+ # Cargar modelo
7
+ model_name = "deepseek-ai/deepseek-vl-7b-chat" # Ejemplo de nombre de modelo
8
+ tokenizer, model, _ = load_model(model_name)
9
+
10
+ def math_problem_generator(theme):
11
+ """
12
+ Simula la generación de un problema matemático basado en el tema seleccionado.
13
+ Devuelve un problema y cuatro posibles soluciones, donde solo una es correcta.
14
+ """
15
+ # Este es un ejemplo simplificado. Aquí deberías generar el problema y las soluciones basándote en el tema.
16
+ if theme == 'Álgebra':
17
+ problem = "¿Cuál es el resultado de x en la ecuación 2x + 3 = 7?"
18
+ options = ["x = 1", "x = 2", "x = 3", "x = 4"]
19
+ correct_answer = "x = 2"
20
+ elif theme == 'Geometría':
21
+ problem = "¿Cuál es el área de un círculo con radio de 4 unidades?"
22
+ options = ["16π unidades cuadradas", "8π unidades cuadradas", "4π unidades cuadradas", "2π unidades cuadradas"]
23
+ correct_answer = "16π unidades cuadradas"
24
+ # Agrega más temas y problemas aquí según sea necesario.
25
+
26
+ return problem, options, correct_answer
27
+
28
+ def solve_math_problem(theme):
29
+ problem, options, correct_answer = math_problem_generator(theme)
30
+ return problem, options, correct_answer
31
+
32
+ themes = ["Álgebra", "Geometría"] # Agrega más temas según sea necesario
33
+ theme_dropdown = gr.Dropdown(choices=themes, label="Selecciona un tema matemático")
34
+
35
+ problem_text = gr.Textbox(label="Problema")
36
+ options_radio = gr.Radio(label="Opciones")
37
+ correct_answer_text = gr.Textbox(label="Respuesta Correcta", visible=False)
38
+
39
+ def update_response(theme):
40
+ problem, options, correct_answer = solve_math_problem(theme)
41
+ problem_text.update(value=problem)
42
+ options_radio.update(choices=options, value=None)
43
+ correct_answer_text.update(value=correct_answer, visible=False)
44
+ return problem, options
45
+
46
+ theme_dropdown.change(update_response, inputs=[theme_dropdown], outputs=[problem_text, options_radio])
47
+
48
+ def check_answer(user_choice, correct_answer):
49
+ if user_choice == correct_answer:
50
+ return "¡Correcto!"
51
+ else:
52
+ correct_answer_text.update(visible=True)