Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	added backdooring
Browse files- __pycache__/codeexecutor.cpython-312.pyc +0 -0
- app.py +2 -1
- temp2.py +77 -37
    	
        __pycache__/codeexecutor.cpython-312.pyc
    CHANGED
    
    | Binary files a/__pycache__/codeexecutor.cpython-312.pyc and b/__pycache__/codeexecutor.cpython-312.pyc differ | 
|  | 
    	
        app.py
    CHANGED
    
    | @@ -3,7 +3,8 @@ import ctranslate2 | |
| 3 | 
             
            from transformers import AutoTokenizer
         | 
| 4 | 
             
            from huggingface_hub import snapshot_download
         | 
| 5 | 
             
            from codeexecutor import get_majority_vote,type_check,postprocess_completion,draw_polynomial_plot
         | 
| 6 | 
            -
             | 
|  | |
| 7 |  | 
| 8 | 
             
            import re
         | 
| 9 | 
             
            import os
         | 
|  | |
| 3 | 
             
            from transformers import AutoTokenizer
         | 
| 4 | 
             
            from huggingface_hub import snapshot_download
         | 
| 5 | 
             
            from codeexecutor import get_majority_vote,type_check,postprocess_completion,draw_polynomial_plot
         | 
| 6 | 
            +
            import base64
         | 
| 7 | 
            +
            from io import BytesIO
         | 
| 8 |  | 
| 9 | 
             
            import re
         | 
| 10 | 
             
            import os
         | 
    	
        temp2.py
    CHANGED
    
    | @@ -1,35 +1,58 @@ | |
| 1 | 
             
            import gradio as gr
         | 
| 2 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 3 | 
             
            import re
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 4 | 
             
            iterations = 4
         | 
| 5 | 
            -
             | 
|  | |
|  | |
| 6 | 
             
            def get_prediction(question):
         | 
| 7 | 
            -
                 | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 8 |  | 
| 9 | 
             
            # Function to parse the prediction to extract the answer and steps
         | 
| 10 | 
             
            def parse_prediction(prediction):
         | 
| 11 | 
             
                lines = prediction.strip().split('\n')
         | 
| 12 | 
             
                answer = None
         | 
| 13 | 
             
                steps = []
         | 
| 14 | 
            -
                for line in lines:
         | 
| 15 | 
            -
             | 
| 16 | 
            -
             | 
| 17 | 
            -
             | 
| 18 | 
            -
             | 
| 19 | 
            -
             | 
| 20 | 
            -
                 | 
| 21 | 
            -
             | 
| 22 | 
            -
             | 
|  | |
|  | |
| 23 | 
             
                steps_text = '\n'.join(steps).strip()
         | 
| 24 | 
             
                return answer, steps_text
         | 
| 25 |  | 
| 26 | 
            -
            # Function to extract boxed answers
         | 
| 27 | 
            -
            def extract_boxed_answer(text):
         | 
| 28 | 
            -
                match = re.search(r'\\boxed\{(.*?)\}', text)
         | 
| 29 | 
            -
                if match:
         | 
| 30 | 
            -
                    return match.group(1)
         | 
| 31 | 
            -
                return None
         | 
| 32 | 
            -
             | 
| 33 | 
             
            # Function to perform majority voting and get steps
         | 
| 34 | 
             
            def majority_vote_with_steps(question, num_iterations=10):
         | 
| 35 | 
             
                all_predictions = []
         | 
| @@ -39,6 +62,7 @@ def majority_vote_with_steps(question, num_iterations=10): | |
| 39 | 
             
                for _ in range(num_iterations):
         | 
| 40 | 
             
                    prediction = get_prediction(question)
         | 
| 41 | 
             
                    answer, success = postprocess_completion(prediction, return_status=True, last_code_block=True)
         | 
|  | |
| 42 |  | 
| 43 | 
             
                    if success:
         | 
| 44 | 
             
                        all_predictions.append(prediction)
         | 
| @@ -49,14 +73,14 @@ def majority_vote_with_steps(question, num_iterations=10): | |
| 49 | 
             
                        all_predictions.append(prediction)
         | 
| 50 | 
             
                        all_answers.append(answer)
         | 
| 51 | 
             
                        steps_list.append(steps)
         | 
| 52 | 
            -
             | 
| 53 | 
             
                if success:
         | 
| 54 | 
            -
                     | 
| 55 | 
             
                    expression = majority_voted_ans
         | 
| 56 | 
             
                    if type_check(expression) == "Polynomial":
         | 
| 57 | 
             
                        plotfile = draw_polynomial_plot(expression)
         | 
| 58 | 
             
                else:
         | 
| 59 | 
            -
                    plotfile =  | 
| 60 |  | 
| 61 | 
             
                # Find the steps corresponding to the majority voted answer
         | 
| 62 | 
             
                for i, ans in enumerate(all_answers):
         | 
| @@ -70,23 +94,39 @@ def majority_vote_with_steps(question, num_iterations=10): | |
| 70 |  | 
| 71 | 
             
                return answer, steps_solution, plotfile
         | 
| 72 |  | 
| 73 | 
            -
            # Function to handle chat-like interaction
         | 
| 74 | 
             
            def chat_interface(history, question):
         | 
| 75 | 
             
                final_answer, steps_solution, plotfile = majority_vote_with_steps(question, iterations)
         | 
| 76 | 
            -
                history.append(("User", question))
         | 
| 77 | 
            -
                history.append(("MathBot", f"Answer: {final_answer}\nSteps:\n{steps_solution}"))
         | 
| 78 | 
            -
                return history, plotfile
         | 
| 79 | 
            -
             | 
| 80 | 
            -
            # Gradio app setup using Blocks for layout management
         | 
| 81 | 
            -
            with gr.Blocks() as interface:
         | 
| 82 | 
            -
                with gr.Column():
         | 
| 83 | 
            -
                    chat_history = gr.Chatbot(label="Chat with MathBot", elem_id="chat_history")
         | 
| 84 | 
            -
                    math_question = gr.Textbox(label="Your Question", placeholder="Ask a math question...", elem_id="math_question")
         | 
| 85 | 
            -
                    chatbot_output = gr.Chatbot(label="Chat History")
         | 
| 86 | 
            -
                    polynomial_plot = gr.Image(label="Polynomial Plot")
         | 
| 87 |  | 
| 88 | 
            -
                 | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 89 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 90 |  | 
| 91 | 
            -
             | 
| 92 | 
            -
                interface.launch()
         | 
|  | |
| 1 | 
             
            import gradio as gr
         | 
| 2 | 
            +
            # import ctranslate2
         | 
| 3 | 
            +
            # from transformers import AutoTokenizer
         | 
| 4 | 
            +
            # from huggingface_hub import snapshot_download
         | 
| 5 | 
            +
            from codeexecutor import get_majority_vote,type_check,postprocess_completion,draw_polynomial_plot
         | 
| 6 | 
            +
            import base64
         | 
| 7 | 
            +
            from io import BytesIO
         | 
| 8 | 
            +
             | 
| 9 | 
             
            import re
         | 
| 10 | 
            +
            import os
         | 
| 11 | 
            +
            # Define the model and tokenizer loading
         | 
| 12 | 
            +
            # model_prompt = "Explain and solve the following mathematical problem step by step, showing all work: "
         | 
| 13 | 
            +
            # tokenizer = AutoTokenizer.from_pretrained("AI-MO/NuminaMath-7B-TIR")
         | 
| 14 | 
            +
            # model_path = snapshot_download(repo_id="Makima57/deepseek-math-Numina")
         | 
| 15 | 
            +
            # generator = ctranslate2.Generator(model_path, device="cpu", compute_type="int8")
         | 
| 16 | 
             
            iterations = 4
         | 
| 17 | 
            +
            test=True
         | 
| 18 | 
            +
             | 
| 19 | 
            +
            # Function to generate predictions using the model
         | 
| 20 | 
             
            def get_prediction(question):
         | 
| 21 | 
            +
                if test==True:
         | 
| 22 | 
            +
                    text="Solve the following mathematical problem: what is  sum of polynomial 2x+3 and 3x?\n### Solution: To solve the problem of summing the polynomials \\(2x + 3\\) and \\(3x\\), we can follow these steps:\n\n1. Define the polynomials.\n2. Sum the polynomials.\n3. Simplify the resulting polynomial expression.\n\nLet's implement this in Python using the sympy library.\n\n```python\nimport sympy as sp\n\n# Define the variable\nx = sp.symbols('x')\n\n# Define the polynomials\npoly1 = 2*x + 3\npoly2 = 3*x\n\n# Sum the polynomials\nsum_poly = poly1 + poly2\n\n# Simplify the resulting polynomial\nsimplified_sum_poly = sp.simplify(sum_poly)\n\n# Print the simplified polynomial\nprint(simplified_sum_poly)\n```\n```output\n5*x + 3\n```\nThe sum of the polynomials \\(2x + 3\\) and \\(3x\\) is \\(\\boxed{5x + 3}\\).\n"  
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                    return text
         | 
| 25 | 
            +
                # input_text = model_prompt + question
         | 
| 26 | 
            +
                # input_tokens = tokenizer.tokenize(input_text)
         | 
| 27 | 
            +
                # results = generator.generate_batch(
         | 
| 28 | 
            +
                #     [input_tokens],
         | 
| 29 | 
            +
                #     max_length=512,
         | 
| 30 | 
            +
                #     sampling_temperature=0.7,
         | 
| 31 | 
            +
                #     sampling_topk=40,
         | 
| 32 | 
            +
                # )
         | 
| 33 | 
            +
                # output_tokens = results[0].sequences[0]
         | 
| 34 | 
            +
                # predicted_answer = tokenizer.convert_tokens_to_string(output_tokens)
         | 
| 35 | 
            +
                # return predicted_answer
         | 
| 36 |  | 
| 37 | 
             
            # Function to parse the prediction to extract the answer and steps
         | 
| 38 | 
             
            def parse_prediction(prediction):
         | 
| 39 | 
             
                lines = prediction.strip().split('\n')
         | 
| 40 | 
             
                answer = None
         | 
| 41 | 
             
                steps = []
         | 
| 42 | 
            +
                # for line in lines:
         | 
| 43 | 
            +
                #     # Check for "Answer:" or "answer:"
         | 
| 44 | 
            +
                #     match = re.match(r'^\s*(?:Answer|answer)\s*[:=]\s*(.*)', line)
         | 
| 45 | 
            +
                #     if match:
         | 
| 46 | 
            +
                #         answer = match.group(1).strip()
         | 
| 47 | 
            +
                #     else:
         | 
| 48 | 
            +
                #         answer=lines[-1].strip()
         | 
| 49 | 
            +
                # if answer is None:
         | 
| 50 | 
            +
                #     # If no "Answer:" found, assume last line is the answer
         | 
| 51 | 
            +
                answer = lines[-1].strip()
         | 
| 52 | 
            +
                steps = lines
         | 
| 53 | 
             
                steps_text = '\n'.join(steps).strip()
         | 
| 54 | 
             
                return answer, steps_text
         | 
| 55 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 56 | 
             
            # Function to perform majority voting and get steps
         | 
| 57 | 
             
            def majority_vote_with_steps(question, num_iterations=10):
         | 
| 58 | 
             
                all_predictions = []
         | 
|  | |
| 62 | 
             
                for _ in range(num_iterations):
         | 
| 63 | 
             
                    prediction = get_prediction(question)
         | 
| 64 | 
             
                    answer, success = postprocess_completion(prediction, return_status=True, last_code_block=True)
         | 
| 65 | 
            +
                    print(answer,success)
         | 
| 66 |  | 
| 67 | 
             
                    if success:
         | 
| 68 | 
             
                        all_predictions.append(prediction)
         | 
|  | |
| 73 | 
             
                        all_predictions.append(prediction)
         | 
| 74 | 
             
                        all_answers.append(answer)
         | 
| 75 | 
             
                        steps_list.append(steps)
         | 
| 76 | 
            +
                majority_voted_ans = get_majority_vote(all_answers)
         | 
| 77 | 
             
                if success:
         | 
| 78 | 
            +
                    
         | 
| 79 | 
             
                    expression = majority_voted_ans
         | 
| 80 | 
             
                    if type_check(expression) == "Polynomial":
         | 
| 81 | 
             
                        plotfile = draw_polynomial_plot(expression)
         | 
| 82 | 
             
                else:
         | 
| 83 | 
            +
                    plotfile = "polynomial_plot.png"
         | 
| 84 |  | 
| 85 | 
             
                # Find the steps corresponding to the majority voted answer
         | 
| 86 | 
             
                for i, ans in enumerate(all_answers):
         | 
|  | |
| 94 |  | 
| 95 | 
             
                return answer, steps_solution, plotfile
         | 
| 96 |  | 
| 97 | 
            +
            # Function to handle chat-like interaction and merge plot into chat history
         | 
| 98 | 
             
            def chat_interface(history, question):
         | 
| 99 | 
             
                final_answer, steps_solution, plotfile = majority_vote_with_steps(question, iterations)
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 100 |  | 
| 101 | 
            +
                # Convert the plot image to base64 for embedding in chat (if plot exists)
         | 
| 102 | 
            +
                if plotfile:
         | 
| 103 | 
            +
                    history.append(("what is the sum of polynomial 2x+3 and 3x?", f"Answer: \n{steps_solution}"))
         | 
| 104 | 
            +
                    
         | 
| 105 | 
            +
                    with open(plotfile, "rb") as image_file:
         | 
| 106 | 
            +
                        image_bytes = image_file.read()
         | 
| 107 | 
            +
                        base64_image = base64.b64encode(image_bytes).decode("utf-8")
         | 
| 108 | 
            +
                        image_data = f'<img src="data:image/png;base64,{base64_image}" width="300"/>'
         | 
| 109 | 
            +
                        history.append(("", image_data)) 
         | 
| 110 | 
            +
                else:
         | 
| 111 | 
            +
                    history.append(("MathBot", f"Answer: \n{steps_solution}"))
         | 
| 112 | 
            +
                
         | 
| 113 | 
            +
                return history 
         | 
| 114 |  | 
| 115 | 
            +
            custom_css = """
         | 
| 116 | 
            +
            #math_question label {
         | 
| 117 | 
            +
                font-size: 20px;  /* Increase label font size */
         | 
| 118 | 
            +
                font-weight: bold; /* Optional: make the label bold */
         | 
| 119 | 
            +
            }
         | 
| 120 | 
            +
             | 
| 121 | 
            +
            #math_question textarea {
         | 
| 122 | 
            +
                font-size: 20px;  /* Increase font size */
         | 
| 123 | 
            +
            }
         | 
| 124 | 
            +
            """
         | 
| 125 | 
            +
            # Gradio app setup using Blocks
         | 
| 126 | 
            +
            with gr.Blocks(css=custom_css) as interface:
         | 
| 127 | 
            +
                chatbot = gr.Chatbot(label="Chat with MathBot", elem_id="chat_history",height="70vh")
         | 
| 128 | 
            +
                math_question = gr.Textbox(label="Your Question", placeholder="Ask a math question...", elem_id="math_question")
         | 
| 129 | 
            +
                
         | 
| 130 | 
            +
                math_question.submit(chat_interface, inputs=[chatbot, math_question], outputs=[chatbot])
         | 
| 131 |  | 
| 132 | 
            +
            interface.launch()
         | 
|  |