|
|
|
import streamlit as st |
|
import ctranslate2 |
|
from transformers import AutoTokenizer |
|
from huggingface_hub import snapshot_download |
|
from codeexecutor import postprocess_completion,get_majority_vote |
|
|
|
|
|
model_prompt = "Solve the following mathematical problem: " |
|
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=10 |
|
|
|
|
|
def get_prediction(question): |
|
input_text = model_prompt + question |
|
input_tokens = tokenizer.tokenize(input_text) |
|
results = generator.generate_batch([input_tokens]) |
|
output_tokens = results[0].sequences[0] |
|
predicted_answer = tokenizer.convert_tokens_to_string(output_tokens) |
|
return predicted_answer |
|
|
|
|
|
def majority_vote(question, num_iterations=10): |
|
all_predictions = [] |
|
all_answer=[] |
|
for _ in range(num_iterations): |
|
prediction = get_prediction(question) |
|
answer=postprocess_completion(prediction,True,True) |
|
all_predictions.append(prediction) |
|
all_answer.append(answer) |
|
majority_voted_pred = max(set(all_predictions), key=all_predictions.count) |
|
majority_voted_ans=get_majority_vote(all_answer) |
|
return majority_voted_pred, all_predictions,majority_voted_ans |
|
|
|
|
|
st.title("Math Question Solver") |
|
st.write("Enter a math question to get the model prediction and see all generated answers.") |
|
|
|
|
|
question = st.text_input("Math Question", placeholder="Enter your math question here...") |
|
|
|
|
|
correct_answer = st.text_input("Correct Answer", placeholder="Enter the correct answer here...") |
|
|
|
|
|
if st.button("Get Prediction"): |
|
if question and correct_answer: |
|
final_prediction, all_predictions,final_answer = majority_vote(question, iterations) |
|
st.write("Question: ", question) |
|
st.write("Generated Answers (10 iterations): ", all_predictions) |
|
st.write("Majority-Voted Prediction: ", final_prediction) |
|
st.write("Correct solution: ", correct_answer) |
|
st.write("Majority answer: ", final_answer) |
|
else: |
|
st.error("Please enter both math question and correct answer") |
|
|
|
|