Spaces:
Sleeping
Sleeping
fajkhfdj
Browse files
app.py
CHANGED
|
@@ -68,7 +68,7 @@ def majority_vote_with_steps(question, num_iterations=10):
|
|
| 68 |
steps_list.append(steps)
|
| 69 |
|
| 70 |
majority_voted_ans = get_majority_vote(all_answers)
|
| 71 |
-
if
|
| 72 |
print(type_check(majority_voted_ans))
|
| 73 |
if type_check(expression) == "Polynomial":
|
| 74 |
plotfile = draw_polynomial_plot(expression)
|
|
|
|
| 68 |
steps_list.append(steps)
|
| 69 |
|
| 70 |
majority_voted_ans = get_majority_vote(all_answers)
|
| 71 |
+
if sucess:
|
| 72 |
print(type_check(majority_voted_ans))
|
| 73 |
if type_check(expression) == "Polynomial":
|
| 74 |
plotfile = draw_polynomial_plot(expression)
|
temp.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 get_majority_vote,type_check,postprocess_completion
|
| 6 |
|
| 7 |
|
| 8 |
import re
|
|
@@ -60,16 +60,23 @@ def majority_vote_with_steps(question, num_iterations=10):
|
|
| 60 |
all_predictions.append(prediction)
|
| 61 |
all_answers.append(answer)
|
| 62 |
steps_list.append(prediction)
|
| 63 |
-
|
| 64 |
else:
|
| 65 |
answer, steps = parse_prediction(prediction)
|
| 66 |
all_predictions.append(prediction)
|
| 67 |
all_answers.append(answer)
|
| 68 |
steps_list.append(steps)
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
|
| 75 |
# Get the majority voted answer
|
|
@@ -90,10 +97,96 @@ def majority_vote_with_steps(question, num_iterations=10):
|
|
| 90 |
|
| 91 |
def gradio_interface(question, correct_answer):
|
| 92 |
final_answer, steps_solution,plotfile = majority_vote_with_steps(question, iterations)
|
| 93 |
-
return question, final_answer, steps_solution, correct_answer,
|
| 94 |
|
| 95 |
# Custom CSS for enhanced design (unchanged)
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
# Define the directory path
|
| 99 |
flagging_dir = "./flagged_data"
|
|
|
|
| 2 |
import ctranslate2
|
| 3 |
from transformers import AutoTokenizer
|
| 4 |
from huggingface_hub import snapshot_download
|
| 5 |
+
from codeexecutor import get_majority_vote,type_check,postprocess_completion
|
| 6 |
|
| 7 |
|
| 8 |
import re
|
|
|
|
| 60 |
all_predictions.append(prediction)
|
| 61 |
all_answers.append(answer)
|
| 62 |
steps_list.append(prediction)
|
| 63 |
+
|
| 64 |
else:
|
| 65 |
answer, steps = parse_prediction(prediction)
|
| 66 |
all_predictions.append(prediction)
|
| 67 |
all_answers.append(answer)
|
| 68 |
steps_list.append(steps)
|
| 69 |
+
|
| 70 |
+
majority_voted_ans = get_majority_vote(all_answers)
|
| 71 |
+
if success:
|
| 72 |
+
print(type_check(majority_voted_ans))
|
| 73 |
+
if type_check(expression) == "Polynomial":
|
| 74 |
+
plotfile = draw_polynomial_plot(expression)
|
| 75 |
+
else:
|
| 76 |
+
if os.path.exists("thankyou.png"):
|
| 77 |
+
plotfile = "thankyou.png"
|
| 78 |
+
else:
|
| 79 |
+
plotfile = None
|
| 80 |
|
| 81 |
|
| 82 |
# Get the majority voted answer
|
|
|
|
| 97 |
|
| 98 |
def gradio_interface(question, correct_answer):
|
| 99 |
final_answer, steps_solution,plotfile = majority_vote_with_steps(question, iterations)
|
| 100 |
+
return question, final_answer, steps_solution, correct_answer,plotfile
|
| 101 |
|
| 102 |
# Custom CSS for enhanced design (unchanged)
|
| 103 |
+
custom_css = """
|
| 104 |
+
body {
|
| 105 |
+
background-color: #fafafa;
|
| 106 |
+
font-family: 'Open Sans', sans-serif;
|
| 107 |
+
}
|
| 108 |
+
.gradio-container {
|
| 109 |
+
background-color: #ffffff;
|
| 110 |
+
border: 3px solid #007acc;
|
| 111 |
+
border-radius: 15px;
|
| 112 |
+
padding: 20px;
|
| 113 |
+
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
|
| 114 |
+
max-width: 800px;
|
| 115 |
+
margin: 50px auto;
|
| 116 |
+
}
|
| 117 |
+
h1 {
|
| 118 |
+
font-family: 'Poppins', sans-serif;
|
| 119 |
+
color: #007acc;
|
| 120 |
+
font-weight: bold;
|
| 121 |
+
font-size: 32px;
|
| 122 |
+
text-align: center;
|
| 123 |
+
margin-bottom: 20px;
|
| 124 |
+
}
|
| 125 |
+
p {
|
| 126 |
+
font-family: 'Roboto', sans-serif;
|
| 127 |
+
font-size: 18px;
|
| 128 |
+
color: #333;
|
| 129 |
+
text-align: center;
|
| 130 |
+
margin-bottom: 15px;
|
| 131 |
+
}
|
| 132 |
+
input, textarea {
|
| 133 |
+
font-family: 'Montserrat', sans-serif;
|
| 134 |
+
font-size: 16px;
|
| 135 |
+
padding: 10px;
|
| 136 |
+
border: 2px solid #007acc;
|
| 137 |
+
border-radius: 10px;
|
| 138 |
+
background-color: #f1f8ff;
|
| 139 |
+
margin-bottom: 15px;
|
| 140 |
+
}
|
| 141 |
+
#math_question, #correct_answer {
|
| 142 |
+
font-size: 20px;
|
| 143 |
+
font-family: 'Poppins', sans-serif;
|
| 144 |
+
font-weight: 500px;
|
| 145 |
+
color: #007acc;
|
| 146 |
+
margin-bottom: 5px;
|
| 147 |
+
display: inline-block;
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
textarea {
|
| 151 |
+
min-height: 150px;
|
| 152 |
+
}
|
| 153 |
+
.gr-button-primary {
|
| 154 |
+
background-color: #007acc !important;
|
| 155 |
+
color: white !important;
|
| 156 |
+
border-radius: 10px !important;
|
| 157 |
+
font-size: 18px !important;
|
| 158 |
+
font-weight: bold !important;
|
| 159 |
+
padding: 10px 20px !important;
|
| 160 |
+
font-family: 'Montserrat', sans-serif !important;
|
| 161 |
+
transition: background-color 0.3s ease !important;
|
| 162 |
+
}
|
| 163 |
+
.gr-button-primary:hover {
|
| 164 |
+
background-color: #005f99 !important;
|
| 165 |
+
}
|
| 166 |
+
.gr-button-secondary {
|
| 167 |
+
background-color: #f44336 !important;
|
| 168 |
+
color: white !important;
|
| 169 |
+
border-radius: 10px !important;
|
| 170 |
+
font-size: 18px !important;
|
| 171 |
+
font-weight: bold !important;
|
| 172 |
+
padding: 10px 20px !important;
|
| 173 |
+
font-family: 'Montserrat', sans-serif !important;
|
| 174 |
+
transition: background-color 0.3s ease !important;
|
| 175 |
+
}
|
| 176 |
+
.gr-button-secondary:hover {
|
| 177 |
+
background-color: #c62828 !important;
|
| 178 |
+
}
|
| 179 |
+
.gr-output {
|
| 180 |
+
background-color: #e0f7fa;
|
| 181 |
+
border: 2px solid #007acc;
|
| 182 |
+
border-radius: 10px;
|
| 183 |
+
padding: 15px;
|
| 184 |
+
font-size: 16px;
|
| 185 |
+
font-family: 'Roboto', sans-serif;
|
| 186 |
+
font-weight: bold;
|
| 187 |
+
color: #00796b;
|
| 188 |
+
}
|
| 189 |
+
"""
|
| 190 |
|
| 191 |
# Define the directory path
|
| 192 |
flagging_dir = "./flagged_data"
|
temp2.py
CHANGED
|
@@ -1,34 +1,10 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
# import ctranslate2
|
| 3 |
-
# from transformers import AutoTokenizer
|
| 4 |
-
# from huggingface_hub import snapshot_download
|
| 5 |
from codeexecutor import get_majority_vote, type_check, postprocess_completion, draw_polynomial_plot
|
| 6 |
import re
|
| 7 |
-
import os
|
| 8 |
-
|
| 9 |
-
# Define the model and tokenizer loading
|
| 10 |
-
model_prompt = "Explain and solve the following mathematical problem step by step, showing all work: "
|
| 11 |
-
# tokenizer = AutoTokenizer.from_pretrained("AI-MO/NuminaMath-7B-TIR")
|
| 12 |
-
# model_path = snapshot_download(repo_id="Makima57/deepseek-math-Numina")
|
| 13 |
-
# generator = ctranslate2.Generator(model_path, device="cpu", compute_type="int8")
|
| 14 |
iterations = 4
|
| 15 |
-
|
| 16 |
-
# # Function to generate predictions using the model
|
| 17 |
-
# def get_prediction(question):
|
| 18 |
-
# input_text = model_prompt + question
|
| 19 |
-
# input_tokens = tokenizer.tokenize(input_text)
|
| 20 |
-
# results = generator.generate_batch(
|
| 21 |
-
# [input_tokens],
|
| 22 |
-
# max_length=512,
|
| 23 |
-
# sampling_temperature=0.7,
|
| 24 |
-
# sampling_topk=40,
|
| 25 |
-
# )
|
| 26 |
-
# output_tokens = results[0].sequences[0]
|
| 27 |
-
# predicted_answer = tokenizer.convert_tokens_to_string(output_tokens)
|
| 28 |
-
# return predicted_answer
|
| 29 |
-
|
| 30 |
def get_prediction(question):
|
| 31 |
-
return "Solve the following mathematical problem: what is
|
| 32 |
|
| 33 |
# Function to parse the prediction to extract the answer and steps
|
| 34 |
def parse_prediction(prediction):
|
|
@@ -36,27 +12,24 @@ def parse_prediction(prediction):
|
|
| 36 |
answer = None
|
| 37 |
steps = []
|
| 38 |
for line in lines:
|
| 39 |
-
# Check for "Answer:" or "answer:"
|
| 40 |
match = re.match(r'^\s*(?:Answer|answer)\s*[:=]\s*(.*)', line)
|
| 41 |
if match:
|
| 42 |
answer = match.group(1).strip()
|
| 43 |
else:
|
| 44 |
steps.append(line)
|
| 45 |
if answer is None:
|
| 46 |
-
# If no "Answer:" found, assume last line is the answer
|
| 47 |
answer = lines[-1].strip()
|
| 48 |
steps = lines
|
| 49 |
steps_text = '\n'.join(steps).strip()
|
| 50 |
return answer, steps_text
|
| 51 |
|
|
|
|
| 52 |
def extract_boxed_answer(text):
|
| 53 |
-
# Regular expression to find the content inside \\boxed{}
|
| 54 |
match = re.search(r'\\boxed\{(.*?)\}', text)
|
| 55 |
if match:
|
| 56 |
-
return match.group(1)
|
| 57 |
return None
|
| 58 |
|
| 59 |
-
|
| 60 |
# Function to perform majority voting and get steps
|
| 61 |
def majority_vote_with_steps(question, num_iterations=10):
|
| 62 |
all_predictions = []
|
|
@@ -66,32 +39,25 @@ def majority_vote_with_steps(question, num_iterations=10):
|
|
| 66 |
for _ in range(num_iterations):
|
| 67 |
prediction = get_prediction(question)
|
| 68 |
answer, success = postprocess_completion(prediction, return_status=True, last_code_block=True)
|
| 69 |
-
|
| 70 |
if success:
|
| 71 |
all_predictions.append(prediction)
|
| 72 |
all_answers.append(answer)
|
| 73 |
steps_list.append(prediction)
|
| 74 |
-
|
| 75 |
-
|
| 76 |
else:
|
| 77 |
answer, steps = parse_prediction(prediction)
|
| 78 |
all_predictions.append(prediction)
|
| 79 |
all_answers.append(answer)
|
| 80 |
steps_list.append(steps)
|
| 81 |
-
|
| 82 |
if success:
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
plotfile = draw_polynomial_plot(expression)
|
| 88 |
else:
|
| 89 |
plotfile = None
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
# Draw plot of polynomial
|
| 94 |
-
|
| 95 |
# Find the steps corresponding to the majority voted answer
|
| 96 |
for i, ans in enumerate(all_answers):
|
| 97 |
if ans == majority_voted_ans:
|
|
@@ -106,33 +72,21 @@ def majority_vote_with_steps(question, num_iterations=10):
|
|
| 106 |
|
| 107 |
# Function to handle chat-like interaction
|
| 108 |
def chat_interface(history, question):
|
| 109 |
-
# Get the answer and steps from the majority voting method
|
| 110 |
final_answer, steps_solution, plotfile = majority_vote_with_steps(question, iterations)
|
| 111 |
-
|
| 112 |
-
# Append the question and answer to the chat history
|
| 113 |
history.append(("User", question))
|
| 114 |
history.append(("MathBot", f"Answer: {final_answer}\nSteps:\n{steps_solution}"))
|
| 115 |
-
|
| 116 |
return history, plotfile
|
| 117 |
|
| 118 |
-
# Gradio app setup
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
gr.
|
| 123 |
-
gr.
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
],
|
| 129 |
-
title="🔢 Math Question Solver - Chat Mode",
|
| 130 |
-
description="Chat with MathBot and ask any math-related question. It will explain the solution step by step and provide a majority-voted answer.",
|
| 131 |
-
allow_flagging="auto",
|
| 132 |
-
flagging_dir="./flagged_data",
|
| 133 |
-
)
|
| 134 |
|
| 135 |
if __name__ == "__main__":
|
| 136 |
interface.launch()
|
| 137 |
-
# history, plotfile=chat_interface(["hello"], ["what is the sum of 2x+3 and 3x"])
|
| 138 |
-
# print(history, plotfile)
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
from codeexecutor import get_majority_vote, type_check, postprocess_completion, draw_polynomial_plot
|
| 3 |
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
iterations = 4
|
| 5 |
+
# Function to generate mock predictions (as the model isn't loaded)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
def get_prediction(question):
|
| 7 |
+
return "Solve the following mathematical problem: what is the sum of polynomial 2x+3 and 3x?\n### Solution: To solve the problem of summing the polynomials \\(2x + 3\\) and \\(3x\\), we can follow these steps:\n\n1. Define the polynomials.\n2. Sum the polynomials.\n3. Simplify the resulting polynomial expression.\n\nThe sum of the polynomials \\(2x + 3\\) and \\(3x\\) is \\(\\boxed{5x + 3}\\).\n"
|
| 8 |
|
| 9 |
# Function to parse the prediction to extract the answer and steps
|
| 10 |
def parse_prediction(prediction):
|
|
|
|
| 12 |
answer = None
|
| 13 |
steps = []
|
| 14 |
for line in lines:
|
|
|
|
| 15 |
match = re.match(r'^\s*(?:Answer|answer)\s*[:=]\s*(.*)', line)
|
| 16 |
if match:
|
| 17 |
answer = match.group(1).strip()
|
| 18 |
else:
|
| 19 |
steps.append(line)
|
| 20 |
if answer is None:
|
|
|
|
| 21 |
answer = lines[-1].strip()
|
| 22 |
steps = lines
|
| 23 |
steps_text = '\n'.join(steps).strip()
|
| 24 |
return answer, steps_text
|
| 25 |
|
| 26 |
+
# Function to extract boxed answers
|
| 27 |
def extract_boxed_answer(text):
|
|
|
|
| 28 |
match = re.search(r'\\boxed\{(.*?)\}', text)
|
| 29 |
if match:
|
| 30 |
+
return match.group(1)
|
| 31 |
return None
|
| 32 |
|
|
|
|
| 33 |
# Function to perform majority voting and get steps
|
| 34 |
def majority_vote_with_steps(question, num_iterations=10):
|
| 35 |
all_predictions = []
|
|
|
|
| 39 |
for _ in range(num_iterations):
|
| 40 |
prediction = get_prediction(question)
|
| 41 |
answer, success = postprocess_completion(prediction, return_status=True, last_code_block=True)
|
| 42 |
+
|
| 43 |
if success:
|
| 44 |
all_predictions.append(prediction)
|
| 45 |
all_answers.append(answer)
|
| 46 |
steps_list.append(prediction)
|
|
|
|
|
|
|
| 47 |
else:
|
| 48 |
answer, steps = parse_prediction(prediction)
|
| 49 |
all_predictions.append(prediction)
|
| 50 |
all_answers.append(answer)
|
| 51 |
steps_list.append(steps)
|
| 52 |
+
|
| 53 |
if success:
|
| 54 |
+
majority_voted_ans = get_majority_vote(all_answers)
|
| 55 |
+
expression = majority_voted_ans
|
| 56 |
+
if type_check(expression) == "Polynomial":
|
| 57 |
+
plotfile = draw_polynomial_plot(expression)
|
|
|
|
| 58 |
else:
|
| 59 |
plotfile = None
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
# Find the steps corresponding to the majority voted answer
|
| 62 |
for i, ans in enumerate(all_answers):
|
| 63 |
if ans == majority_voted_ans:
|
|
|
|
| 72 |
|
| 73 |
# Function to handle chat-like interaction
|
| 74 |
def chat_interface(history, question):
|
|
|
|
| 75 |
final_answer, steps_solution, plotfile = majority_vote_with_steps(question, iterations)
|
|
|
|
|
|
|
| 76 |
history.append(("User", question))
|
| 77 |
history.append(("MathBot", f"Answer: {final_answer}\nSteps:\n{steps_solution}"))
|
|
|
|
| 78 |
return history, plotfile
|
| 79 |
|
| 80 |
+
# Gradio app setup using Blocks for layout management
|
| 81 |
+
with gr.Blocks() as interface:
|
| 82 |
+
with gr.Column():
|
| 83 |
+
chat_history = gr.Chatbot(label="Chat with MathBot", elem_id="chat_history")
|
| 84 |
+
math_question = gr.Textbox(label="Your Question", placeholder="Ask a math question...", elem_id="math_question")
|
| 85 |
+
chatbot_output = gr.Chatbot(label="Chat History")
|
| 86 |
+
polynomial_plot = gr.Image(label="Polynomial Plot")
|
| 87 |
+
|
| 88 |
+
math_question.submit(chat_interface, inputs=[chat_history, math_question], outputs=[chatbot_output, polynomial_plot])
|
| 89 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
if __name__ == "__main__":
|
| 92 |
interface.launch()
|
|
|
|
|
|
temp3.py
CHANGED
|
@@ -1,17 +1,94 @@
|
|
| 1 |
-
import
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
#
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from codeexecutor import get_majority_vote, type_check, postprocess_completion, draw_polynomial_plot
|
| 3 |
+
import re
|
| 4 |
+
iterations=4
|
| 5 |
|
| 6 |
+
# Mock function for generating predictions
|
| 7 |
+
def get_prediction(question):
|
| 8 |
+
return "Solve the following mathematical problem: what is the sum of polynomial 2x+3 and 3x?\n### Solution: To solve the problem of summing the polynomials \\(2x + 3\\) and \\(3x\\), we can follow these steps:\n\n1. Define the polynomials.\n2. Sum the polynomials.\n3. Simplify the resulting polynomial expression.\n\nThe sum of the polynomials \\(2x + 3\\) and \\(3x\\) is \\(\\boxed{5x + 3}\\).\n"
|
| 9 |
|
| 10 |
+
# Function to parse the prediction to extract the answer and steps
|
| 11 |
+
def parse_prediction(prediction):
|
| 12 |
+
lines = prediction.strip().split('\n')
|
| 13 |
+
answer = None
|
| 14 |
+
steps = []
|
| 15 |
+
for line in lines:
|
| 16 |
+
match = re.match(r'^\s*(?:Answer|answer)\s*[:=]\s*(.*)', line)
|
| 17 |
+
if match:
|
| 18 |
+
answer = match.group(1).strip()
|
| 19 |
+
else:
|
| 20 |
+
steps.append(line)
|
| 21 |
+
if answer is None:
|
| 22 |
+
answer = lines[-1].strip()
|
| 23 |
+
steps = lines
|
| 24 |
+
steps_text = '\n'.join(steps).strip()
|
| 25 |
+
return answer, steps_text
|
| 26 |
|
| 27 |
+
# Function to extract boxed answers
|
| 28 |
+
def extract_boxed_answer(text):
|
| 29 |
+
match = re.search(r'\\boxed\{(.*?)\}', text)
|
| 30 |
+
if match:
|
| 31 |
+
return match.group(1)
|
| 32 |
+
return None
|
| 33 |
|
| 34 |
+
# Function to perform majority voting and get steps
|
| 35 |
+
def majority_vote_with_steps(question, num_iterations=10):
|
| 36 |
+
all_predictions = []
|
| 37 |
+
all_answers = []
|
| 38 |
+
steps_list = []
|
| 39 |
|
| 40 |
+
for _ in range(num_iterations):
|
| 41 |
+
prediction = get_prediction(question)
|
| 42 |
+
answer, success = postprocess_completion(prediction, return_status=True, last_code_block=True)
|
| 43 |
+
print(answer,success)
|
| 44 |
+
|
| 45 |
+
if success:
|
| 46 |
+
all_predictions.append(prediction)
|
| 47 |
+
all_answers.append(answer)
|
| 48 |
+
steps_list.append(prediction)
|
| 49 |
+
else:
|
| 50 |
+
answer, steps = parse_prediction(prediction)
|
| 51 |
+
all_predictions.append(prediction)
|
| 52 |
+
all_answers.append(answer)
|
| 53 |
+
steps_list.append(steps)
|
| 54 |
+
majority_voted_ans = get_majority_vote(all_answers)
|
| 55 |
+
if success:
|
| 56 |
+
|
| 57 |
+
expression = majority_voted_ans
|
| 58 |
+
if type_check(expression) == "Polynomial":
|
| 59 |
+
plotfile = draw_polynomial_plot(expression)
|
| 60 |
+
else:
|
| 61 |
+
plotfile = None
|
| 62 |
+
|
| 63 |
+
# Find the steps corresponding to the majority voted answer
|
| 64 |
+
for i, ans in enumerate(all_answers):
|
| 65 |
+
if ans == majority_voted_ans:
|
| 66 |
+
steps_solution = steps_list[i]
|
| 67 |
+
answer = parse_prediction(steps_solution)
|
| 68 |
+
break
|
| 69 |
+
else:
|
| 70 |
+
answer = majority_voted_ans
|
| 71 |
+
steps_solution = "No steps found"
|
| 72 |
+
|
| 73 |
+
return answer, steps_solution, plotfile
|
| 74 |
+
|
| 75 |
+
# Function to handle chat-like interaction and merge plot into chat history
|
| 76 |
+
def chat_interface(history, question):
|
| 77 |
+
final_answer, steps_solution, plotfile = majority_vote_with_steps(question, iterations)
|
| 78 |
+
|
| 79 |
+
# Convert the plot image to base64 for embedding in chat (if plot exists)
|
| 80 |
+
if plotfile:
|
| 81 |
+
history.append(("MathBot", f"Answer: \n{steps_solution}\n"))
|
| 82 |
+
else:
|
| 83 |
+
history.append(("MathBot", f"Answer: \n{steps_solution}"))
|
| 84 |
+
|
| 85 |
+
return history # Return None for the image output as it's embedded in the chat
|
| 86 |
+
|
| 87 |
+
# Gradio app setup using Blocks
|
| 88 |
+
with gr.Blocks() as interface:
|
| 89 |
+
chatbot = gr.Chatbot(label="Chat with MathBot", elem_id="chat_history")
|
| 90 |
+
math_question = gr.Textbox(label="Your Question", placeholder="Ask a math question...", elem_id="math_question")
|
| 91 |
+
|
| 92 |
+
math_question.submit(chat_interface, inputs=[chatbot, math_question], outputs=[chatbot])
|
| 93 |
+
|
| 94 |
+
interface.launch()
|