Spaces:
Running
Running
File size: 4,276 Bytes
ef78f90 879248b fa5a727 879248b e8c0a63 ef78f90 879248b ef78f90 e8c0a63 ef78f90 e8c0a63 ef78f90 e8c0a63 ef78f90 e8c0a63 ef78f90 fa5a727 ef78f90 fa5a727 ef78f90 fa5a727 879248b fa5a727 ef78f90 879248b ef78f90 879248b fa5a727 ef78f90 |
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 107 108 109 110 111 112 113 114 115 116 117 |
import gradio as gr
from codeexecutor import get_majority_vote, type_check, postprocess_completion, draw_polynomial_plot
import re
import base64
from io import BytesIO
# from PIL import Image
import time
iterations=4
# Mock function for generating predictions
def get_prediction(question):
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"
# Function to parse the prediction to extract the answer and steps
def parse_prediction(prediction):
lines = prediction.strip().split('\n')
answer = None
steps = []
for line in lines:
match = re.match(r'^\s*(?:Answer|answer)\s*[:=]\s*(.*)', line)
if match:
answer = match.group(1).strip()
else:
steps.append(line)
if answer is None:
answer = lines[-1].strip()
steps = lines
steps_text = '\n'.join(steps).strip()
return answer, steps_text
# Function to extract boxed answers
def extract_boxed_answer(text):
match = re.search(r'\\boxed\{(.*?)\}', text)
if match:
return match.group(1)
return None
# Function to perform majority voting and get steps
def majority_vote_with_steps(question, num_iterations=10):
all_predictions = []
all_answers = []
steps_list = []
for _ in range(num_iterations):
prediction = get_prediction(question)
answer, success = postprocess_completion(prediction, return_status=True, last_code_block=True)
print(answer,success)
if success:
all_predictions.append(prediction)
all_answers.append(answer)
steps_list.append(prediction)
else:
answer, steps = parse_prediction(prediction)
all_predictions.append(prediction)
all_answers.append(answer)
steps_list.append(steps)
majority_voted_ans = get_majority_vote(all_answers)
if success:
expression = majority_voted_ans
if type_check(expression) == "Polynomial":
plotfile = draw_polynomial_plot(expression)
else:
plotfile = "polynomial_plot.png"
# Find the steps corresponding to the majority voted answer
for i, ans in enumerate(all_answers):
if ans == majority_voted_ans:
steps_solution = steps_list[i]
answer = parse_prediction(steps_solution)
break
else:
answer = majority_voted_ans
steps_solution = "No steps found"
return answer, steps_solution, plotfile
# Function to handle chat-like interaction and merge plot into chat history
def chat_interface(history, question):
final_answer, steps_solution, plotfile = majority_vote_with_steps(question, iterations)
# Convert the plot image to base64 for embedding in chat (if plot exists)
if plotfile:
history.append(("what is the sum of polynomial 2x+3 and 3x?", f"Answer: \n{steps_solution}"))
with open(plotfile, "rb") as image_file:
image_bytes = image_file.read()
base64_image = base64.b64encode(image_bytes).decode("utf-8")
image_data = f'<img src="data:image/png;base64,{base64_image}" width="300"/>'
history.append(("", image_data))
else:
history.append(("MathBot", f"Answer: \n{steps_solution}"))
return history
custom_css = """
#math_question label {
font-size: 20px; /* Increase label font size */
font-weight: bold; /* Optional: make the label bold */
}
#math_question textarea {
font-size: 20px; /* Increase font size */
}
"""
# Gradio app setup using Blocks
with gr.Blocks(css=custom_css) as interface:
chatbot = gr.Chatbot(label="Chat with MathBot", elem_id="chat_history",height="70vh")
math_question = gr.Textbox(label="Your Question", placeholder="Ask a math question...", elem_id="math_question")
math_question.submit(chat_interface, inputs=[chatbot, math_question], outputs=[chatbot])
interface.launch()
|