deepseekmath / temp.py
Pra-tham's picture
added draawubg feautyre
e8c0a63
raw
history blame
4.49 kB
import gradio as gr
import ctranslate2
from transformers import AutoTokenizer
from huggingface_hub import snapshot_download
from codeexecutor import get_majority_vote,type_check,postprocess_completion,draw_polynomial_plot
import re
import os
# Define the model and tokenizer loading
model_prompt = "Explain and solve the following mathematical problem step by step, showing all work: "
tokenizer = AutoTokenizer.from_pretrained("AI-MO/NuminaMath-7B-TIR")
model_path = snapshot_download(repo_id="Makima57/deepseek-math-Numina")
generator = ctranslate2.Generator(model_path, device="cpu", compute_type="int8")
iterations = 4
# Function to generate predictions using the model
def get_prediction(question):
input_text = model_prompt + question
input_tokens = tokenizer.tokenize(input_text)
results = generator.generate_batch(
[input_tokens],
max_length=512,
sampling_temperature=0.7,
sampling_topk=40,
)
output_tokens = results[0].sequences[0]
predicted_answer = tokenizer.convert_tokens_to_string(output_tokens)
return predicted_answer
# 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:
# Check for "Answer:" or "answer:"
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:
# If no "Answer:" found, assume last line is the answer
answer = lines[-1].strip()
steps = lines
steps_text = '\n'.join(steps).strip()
return answer, steps_text
# 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,sucess= postprocess_completion(prediction, return_status=True, last_code_block=True)
if sucess:
all_predictions.append(prediction)
all_answers.append(answer)
steps_list.append(prediction)
majority_voted_ans = get_majority_vote(all_answers)
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 type_check(majority_voted_ans)=="Polynomial":
plotfile=draw_polynomial_plot(majority_voted_ans)
#draw plot of polynomial
# Get the majority voted answer
# 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
def gradio_interface(question, correct_answer):
final_answer, steps_solution,plotfile = majority_vote_with_steps(question, iterations)
return question, final_answer, steps_solution, correct_answer,
# Custom CSS for enhanced design (unchanged)
# Define the directory path
flagging_dir = "./flagged_data"
# Create the directory if it doesn't exist
if not os.path.exists(flagging_dir):
os.makedirs(flagging_dir)
# Gradio app setup with flagging
interface = gr.Interface(
fn=gradio_interface,
inputs=[
gr.Textbox(label="🧠 Math Question", placeholder="Enter your math question here...", elem_id="math_question"),
],
outputs=[
gr.Textbox(label="Question", interactive=False), # Non-editable
gr.Textbox(label="Answer", interactive=False), # Non-editable
gr.Textbox(label="Solution", interactive=True), # Editable textbox for correct solution
gr.Image(label="Polynomial Plot")
],
title="πŸ”’ Math Question Solver",
description="Enter a math question to get the model's majority-voted answer and steps to solve the problem.",
css=custom_css, # Apply custom CSS
flagging_dir=flagging_dir, # Directory to save flagged data
allow_flagging="auto" # Allow users to auto flag data
)
if __name__ == "__main__":
interface.launch()