Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 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 |
+
# Gradio interface for user input and output
|
| 38 |
+
def gradio_interface(question, correct_answer):
|
| 39 |
+
final_prediction, all_predictions,final_answer = majority_vote(question, iterations)
|
| 40 |
+
return {
|
| 41 |
+
"Question": question,
|
| 42 |
+
"Generated Answers (10 iterations)": all_predictions,
|
| 43 |
+
"Majority-Voted Prediction": final_prediction,
|
| 44 |
+
"Correct solution": correct_answer,
|
| 45 |
+
"Majority answer": final_answer
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
# Gradio app setup
|
| 49 |
+
interface = gr.Interface(
|
| 50 |
+
fn=gradio_interface,
|
| 51 |
+
inputs=[
|
| 52 |
+
gr.Textbox(label="Math Question"),
|
| 53 |
+
gr.Textbox(label="Correct Answer"),
|
| 54 |
+
],
|
| 55 |
+
outputs=[
|
| 56 |
+
gr.JSON(label="Results"), # Display the results in a JSON format
|
| 57 |
+
],
|
| 58 |
+
title="Math Question Solver",
|
| 59 |
+
description="Enter a math question to get the model prediction and see all generated answers.",
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
if __name__ == "__main__":
|
| 63 |
+
|
| 64 |
+
|