Upload app.py with huggingface_hub
Browse files
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import streamlit as st
|
3 |
+
import ctranslate2
|
4 |
+
from transformers import AutoTokenizer
|
5 |
+
from huggingface_hub import snapshot_download
|
6 |
+
from codeexecutor import postprocess_completion,get_majority_vote
|
7 |
+
|
8 |
+
# Define the model and tokenizer loading
|
9 |
+
model_prompt = "Solve the following mathematical problem: "
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained("AI-MO/NuminaMath-7B-TIR")
|
11 |
+
model_path = snapshot_download(repo_id="Makima57/deepseek-math-Numina")
|
12 |
+
generator = ctranslate2.Generator(model_path, device="cpu", compute_type="int8")
|
13 |
+
iterations=10
|
14 |
+
|
15 |
+
# Function to generate predictions using the model
|
16 |
+
def get_prediction(question):
|
17 |
+
input_text = model_prompt + question
|
18 |
+
input_tokens = tokenizer.tokenize(input_text)
|
19 |
+
results = generator.generate_batch([input_tokens])
|
20 |
+
output_tokens = results[0].sequences[0]
|
21 |
+
predicted_answer = tokenizer.convert_tokens_to_string(output_tokens)
|
22 |
+
return predicted_answer
|
23 |
+
|
24 |
+
# Function to perform majority voting across multiple predictions
|
25 |
+
def majority_vote(question, num_iterations=10):
|
26 |
+
all_predictions = []
|
27 |
+
all_answer=[]
|
28 |
+
for _ in range(num_iterations):
|
29 |
+
prediction = get_prediction(question)
|
30 |
+
answer=postprocess_completion(prediction,True,True)
|
31 |
+
all_predictions.append(prediction)
|
32 |
+
all_answer.append(answer)
|
33 |
+
majority_voted_pred = max(set(all_predictions), key=all_predictions.count)
|
34 |
+
majority_voted_ans=get_majority_vote(all_answer)
|
35 |
+
return majority_voted_pred, all_predictions,majority_voted_ans
|
36 |
+
|
37 |
+
# Streamlit app UI
|
38 |
+
st.title("Math Question Solver")
|
39 |
+
st.write("Enter a math question to get the model prediction and see all generated answers.")
|
40 |
+
|
41 |
+
# Input field for math question
|
42 |
+
question = st.text_input("Math Question", placeholder="Enter your math question here...")
|
43 |
+
|
44 |
+
# Input field for correct answer
|
45 |
+
correct_answer = st.text_input("Correct Answer", placeholder="Enter the correct answer here...")
|
46 |
+
|
47 |
+
# Button to trigger prediction
|
48 |
+
if st.button("Get Prediction"):
|
49 |
+
if question and correct_answer:
|
50 |
+
final_prediction, all_predictions,final_answer = majority_vote(question, iterations)
|
51 |
+
st.write("Question: ", question)
|
52 |
+
st.write("Generated Answers (10 iterations): ", all_predictions)
|
53 |
+
st.write("Majority-Voted Prediction: ", final_prediction)
|
54 |
+
st.write("Correct solution: ", correct_answer)
|
55 |
+
st.write("Majority answer: ", final_answer)
|
56 |
+
else:
|
57 |
+
st.error("Please enter both math question and correct answer")
|
58 |
+
|
59 |
+
|