Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import ctranslate2 | |
| from transformers import AutoTokenizer | |
| from huggingface_hub import snapshot_download | |
| from codeexecutor import postprocess_completion, get_majority_vote | |
| # Define the model and tokenizer loading | |
| 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 | |
| # 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]) | |
| output_tokens = results[0].sequences[0] | |
| predicted_answer = tokenizer.convert_tokens_to_string(output_tokens) | |
| return predicted_answer | |
| # Function to perform majority voting and solve the problem with steps | |
| def majority_vote_with_steps(question, num_iterations=10): | |
| all_predictions = [] | |
| all_answer = [] | |
| steps_to_solve = [] | |
| for _ in range(num_iterations): | |
| prediction = get_prediction(question) | |
| # Process prediction to get steps and answer | |
| answer, success = postprocess_completion(prediction, True, True) | |
| all_predictions.append(prediction) | |
| all_answer.append(answer) | |
| if success: | |
| steps_to_solve.append(answer) # Add the steps if code executes successfully | |
| majority_voted_ans = get_majority_vote(all_answer) | |
| # If steps to solve exist, return them, else fallback to "No steps found" | |
| steps_solution = steps_to_solve[0] if steps_to_solve else "No steps found" | |
| return majority_voted_ans, steps_solution | |
| # Gradio interface for user input and output | |
| def gradio_interface(question, correct_answer): | |
| final_answer, steps_solution = majority_vote_with_steps(question, iterations) | |
| return { | |
| "Question": question, | |
| "Majority-Voted Answer": final_answer, | |
| "Steps to Solve": steps_solution, | |
| "Correct Solution": correct_answer | |
| } | |
| # Custom CSS for enhanced design | |
| custom_css = """ | |
| body { | |
| background-color: #fafafa; | |
| font-family: 'Open Sans', sans-serif; | |
| } | |
| .gradio-container { | |
| background-color: #ffffff; | |
| border: 3px solid #007acc; | |
| border-radius: 15px; | |
| padding: 20px; | |
| box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15); | |
| max-width: 800px; | |
| margin: 50px auto; | |
| } | |
| h1 { | |
| font-family: 'Poppins', sans-serif; | |
| color: #007acc; | |
| font-weight: bold; | |
| font-size: 32px; | |
| text-align: center; | |
| margin-bottom: 20px; | |
| } | |
| p { | |
| font-family: 'Roboto', sans-serif; | |
| font-size: 18px; | |
| color: #333; | |
| text-align: center; | |
| margin-bottom: 15px; | |
| } | |
| input, textarea { | |
| font-family: 'Montserrat', sans-serif; | |
| font-size: 16px; | |
| padding: 10px; | |
| border: 2px solid #007acc; | |
| border-radius: 10px; | |
| background-color: #f1f8ff; | |
| margin-bottom: 15px; | |
| } | |
| #math_question, #correct_answer { | |
| font-size: 20px; | |
| font-family: 'Poppins', sans-serif; | |
| font-weight: 500px; /* Apply bold */ | |
| color: #007acc; | |
| margin-bottom: 5px; | |
| display: inline-block; | |
| } | |
| textarea { | |
| min-height: 150px; | |
| } | |
| .gr-button-primary { | |
| background-color: #007acc !important; | |
| color: white !important; | |
| border-radius: 10px !important; | |
| font-size: 18px !important; | |
| font-weight: bold !important; | |
| padding: 10px 20px !important; | |
| font-family: 'Montserrat', sans-serif !important; | |
| transition: background-color 0.3s ease !important; | |
| } | |
| .gr-button-primary:hover { | |
| background-color: #005f99 !important; | |
| } | |
| .gr-button-secondary { | |
| background-color: #f44336 !important; | |
| color: white !important; | |
| border-radius: 10px !important; | |
| font-size: 18px !important; | |
| font-weight: bold !important; | |
| padding: 10px 20px !important; | |
| font-family: 'Montserrat', sans-serif !important; | |
| transition: background-color 0.3s ease !important; | |
| } | |
| .gr-button-secondary:hover { | |
| background-color: #c62828 !important; | |
| } | |
| .gr-output { | |
| background-color: #e0f7fa; | |
| border: 2px solid #007acc; | |
| border-radius: 10px; | |
| padding: 15px; | |
| font-size: 16px; | |
| font-family: 'Roboto', sans-serif; | |
| font-weight: bold; | |
| color: #00796b; | |
| } | |
| """ | |
| # Gradio app setup | |
| interface = gr.Interface( | |
| fn=gradio_interface, | |
| inputs=[ | |
| gr.Textbox(label="π§ Math Question", placeholder="Enter your math question here...", elem_id="math_question"), | |
| gr.Textbox(label="β Correct Answer", placeholder="Enter the correct answer here...", elem_id="correct_answer"), | |
| ], | |
| outputs=[ | |
| gr.JSON(label="π Results"), # Display the results in a JSON format | |
| ], | |
| 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 | |
| ) | |
| if __name__ == "__main__": | |
| interface.launch() | |