Spaces:
Running
Running
Update app.py
Browse filesapp majority n steps
app.py
CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
|
|
2 |
import ctranslate2
|
3 |
from transformers import AutoTokenizer
|
4 |
from huggingface_hub import snapshot_download
|
5 |
-
from codeexecutor import postprocess_completion, get_majority_vote
|
6 |
|
7 |
# Define the model and tokenizer loading
|
8 |
model_prompt = "Solve the following mathematical problem: "
|
@@ -20,38 +20,39 @@ def get_prediction(question):
|
|
20 |
predicted_answer = tokenizer.convert_tokens_to_string(output_tokens)
|
21 |
return predicted_answer
|
22 |
|
23 |
-
# Function to perform majority voting
|
24 |
-
def
|
25 |
all_predictions = []
|
26 |
all_answer = []
|
|
|
|
|
27 |
for _ in range(num_iterations):
|
28 |
prediction = get_prediction(question)
|
29 |
-
|
|
|
30 |
all_predictions.append(prediction)
|
31 |
all_answer.append(answer)
|
32 |
-
|
|
|
|
|
33 |
majority_voted_ans = get_majority_vote(all_answer)
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
steps = get_solution_steps(question) # You need to implement this function based on how steps are generated
|
40 |
-
return steps
|
41 |
|
42 |
# Gradio interface for user input and output
|
43 |
def gradio_interface(question, correct_answer):
|
44 |
-
|
45 |
-
solution_steps = get_solution_steps(question) # Fetch the steps to solve the problem
|
46 |
return {
|
47 |
"Question": question,
|
48 |
-
"Majority-Voted
|
49 |
-
"
|
50 |
-
"
|
51 |
-
"Solution Steps": solution_steps
|
52 |
}
|
53 |
|
54 |
-
# Custom CSS for enhanced design
|
55 |
custom_css = """
|
56 |
body {
|
57 |
background-color: #fafafa;
|
@@ -93,7 +94,7 @@ custom_css = """
|
|
93 |
#math_question, #correct_answer {
|
94 |
font-size: 20px;
|
95 |
font-family: 'Poppins', sans-serif;
|
96 |
-
font-weight: 500px;
|
97 |
color: #007acc;
|
98 |
margin-bottom: 5px;
|
99 |
display: inline-block;
|
@@ -151,7 +152,7 @@ interface = gr.Interface(
|
|
151 |
gr.JSON(label="π Results"), # Display the results in a JSON format
|
152 |
],
|
153 |
title="π’ Math Question Solver",
|
154 |
-
description="Enter a math question to get the majority-voted
|
155 |
css=custom_css # Apply custom CSS
|
156 |
)
|
157 |
|
|
|
2 |
import ctranslate2
|
3 |
from transformers import AutoTokenizer
|
4 |
from huggingface_hub import snapshot_download
|
5 |
+
from codeexecutor import postprocess_completion, get_majority_vote
|
6 |
|
7 |
# Define the model and tokenizer loading
|
8 |
model_prompt = "Solve the following mathematical problem: "
|
|
|
20 |
predicted_answer = tokenizer.convert_tokens_to_string(output_tokens)
|
21 |
return predicted_answer
|
22 |
|
23 |
+
# Function to perform majority voting and solve the problem with steps
|
24 |
+
def majority_vote_with_steps(question, num_iterations=10):
|
25 |
all_predictions = []
|
26 |
all_answer = []
|
27 |
+
steps_to_solve = []
|
28 |
+
|
29 |
for _ in range(num_iterations):
|
30 |
prediction = get_prediction(question)
|
31 |
+
# Process prediction to get steps and answer
|
32 |
+
answer, success = postprocess_completion(prediction, True, True)
|
33 |
all_predictions.append(prediction)
|
34 |
all_answer.append(answer)
|
35 |
+
if success:
|
36 |
+
steps_to_solve.append(answer) # Add the steps if code executes successfully
|
37 |
+
|
38 |
majority_voted_ans = get_majority_vote(all_answer)
|
39 |
+
|
40 |
+
# If steps to solve exist, return them, else fallback to "No steps found"
|
41 |
+
steps_solution = steps_to_solve[0] if steps_to_solve else "No steps found"
|
42 |
+
|
43 |
+
return majority_voted_ans, steps_solution
|
|
|
|
|
44 |
|
45 |
# Gradio interface for user input and output
|
46 |
def gradio_interface(question, correct_answer):
|
47 |
+
final_answer, steps_solution = majority_vote_with_steps(question, iterations)
|
|
|
48 |
return {
|
49 |
"Question": question,
|
50 |
+
"Majority-Voted Answer": final_answer,
|
51 |
+
"Steps to Solve": steps_solution,
|
52 |
+
"Correct Solution": correct_answer
|
|
|
53 |
}
|
54 |
|
55 |
+
# Custom CSS for enhanced design
|
56 |
custom_css = """
|
57 |
body {
|
58 |
background-color: #fafafa;
|
|
|
94 |
#math_question, #correct_answer {
|
95 |
font-size: 20px;
|
96 |
font-family: 'Poppins', sans-serif;
|
97 |
+
font-weight: 500px; /* Apply bold */
|
98 |
color: #007acc;
|
99 |
margin-bottom: 5px;
|
100 |
display: inline-block;
|
|
|
152 |
gr.JSON(label="π Results"), # Display the results in a JSON format
|
153 |
],
|
154 |
title="π’ Math Question Solver",
|
155 |
+
description="Enter a math question to get the model's majority-voted answer and steps to solve the problem.",
|
156 |
css=custom_css # Apply custom CSS
|
157 |
)
|
158 |
|