Pra-tham commited on
Commit
b984a91
β€’
1 Parent(s): 0f1e26c

added backdooring

Browse files
Files changed (2) hide show
  1. app.py +59 -68
  2. temp.py +16 -7
app.py CHANGED
@@ -38,17 +38,17 @@ def parse_prediction(prediction):
38
  lines = prediction.strip().split('\n')
39
  answer = None
40
  steps = []
41
- for line in lines:
42
- # Check for "Answer:" or "answer:"
43
- match = re.match(r'^\s*(?:Answer|answer)\s*[:=]\s*(.*)', line)
44
- if match:
45
- answer = match.group(1).strip()
46
- else:
47
- steps.append(line)
48
- if answer is None:
49
- # If no "Answer:" found, assume last line is the answer
50
- answer = lines[-1].strip()
51
- steps = lines
52
  steps_text = '\n'.join(steps).strip()
53
  return answer, steps_text
54
 
@@ -60,81 +60,72 @@ def majority_vote_with_steps(question, num_iterations=10):
60
 
61
  for _ in range(num_iterations):
62
  prediction = get_prediction(question)
63
- answer,sucess= postprocess_completion(prediction, return_status=True, last_code_block=True)
64
- print(answer,sucess)
65
- if sucess:
 
66
  all_predictions.append(prediction)
67
  all_answers.append(answer)
68
  steps_list.append(prediction)
69
-
70
  else:
71
  answer, steps = parse_prediction(prediction)
72
  all_predictions.append(prediction)
73
  all_answers.append(answer)
74
  steps_list.append(steps)
75
-
76
  majority_voted_ans = get_majority_vote(all_answers)
77
- if sucess:
78
- print(type_check(majority_voted_ans))
79
- if type_check(majority_voted_ans) == "Polynomial":
80
- plotfile = draw_polynomial_plot(majority_voted_ans)
 
81
  else:
82
- if os.path.exists("polynomial_plot.png"):
83
- plotfile = "polynomial_plot.png"
84
- else:
85
-
86
- plotfile = None
87
-
88
-
89
- # Get the majority voted answer
90
-
91
-
92
 
93
  # Find the steps corresponding to the majority voted answer
94
  for i, ans in enumerate(all_answers):
95
  if ans == majority_voted_ans:
96
  steps_solution = steps_list[i]
97
- answer=parse_prediction(steps_solution)
98
  break
99
  else:
100
- answer=majority_voted_ans
101
  steps_solution = "No steps found"
102
 
103
- return answer, steps_solution,plotfile
104
-
105
- def gradio_interface(question, correct_answer):
106
- final_answer, steps_solution,plotfile = majority_vote_with_steps(question, iterations)
107
- print(plotfile)
108
- print(os.path.exists(plotfile),os.path.exists("polynomial_plot.png"))
109
- return question, final_answer, steps_solution, correct_answer,plotfile
110
-
111
- # Custom CSS for enhanced design (unchanged)
112
-
113
- # Define the directory path
114
- flagging_dir = "./flagged_data"
115
-
116
- # Create the directory if it doesn't exist
117
- if not os.path.exists(flagging_dir):
118
- os.makedirs(flagging_dir)
119
 
120
- # Gradio app setup with flagging
121
- interface = gr.Interface(
122
- fn=gradio_interface,
123
- inputs=[
124
- gr.Textbox(label="🧠 Math Question", placeholder="Enter your math question here...", elem_id="math_question"),
125
- ],
126
- outputs=[
127
- gr.Textbox(label="Question", interactive=False), # Non-editable
128
- gr.Textbox(label="Answer", interactive=False), # Non-editable
129
- gr.Textbox(label="Solution", interactive=True), # Editable textbox for correct solution
130
- gr.Image(label="Polynomial Plot", type="filepath")
131
- ],
132
- title="πŸ”’ Math Question Solver",
133
- description="Enter a math question to get the model's majority-voted answer and steps to solve the problem.",
134
- # Apply custom CSS
135
- flagging_dir=flagging_dir, # Directory to save flagged data
136
- allow_flagging="auto" # Allow users to auto flag data
137
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
 
139
- if __name__ == "__main__":
140
- interface.launch()
 
38
  lines = prediction.strip().split('\n')
39
  answer = None
40
  steps = []
41
+ # for line in lines:
42
+ # # Check for "Answer:" or "answer:"
43
+ # match = re.match(r'^\s*(?:Answer|answer)\s*[:=]\s*(.*)', line)
44
+ # if match:
45
+ # answer = match.group(1).strip()
46
+ # else:
47
+ # answer=lines[-1].strip()
48
+ # if answer is None:
49
+ # # If no "Answer:" found, assume last line is the answer
50
+ answer = lines[-1].strip()
51
+ steps = lines
52
  steps_text = '\n'.join(steps).strip()
53
  return answer, steps_text
54
 
 
60
 
61
  for _ in range(num_iterations):
62
  prediction = get_prediction(question)
63
+ answer, success = postprocess_completion(prediction, return_status=True, last_code_block=True)
64
+ print(answer,success)
65
+
66
+ if success:
67
  all_predictions.append(prediction)
68
  all_answers.append(answer)
69
  steps_list.append(prediction)
 
70
  else:
71
  answer, steps = parse_prediction(prediction)
72
  all_predictions.append(prediction)
73
  all_answers.append(answer)
74
  steps_list.append(steps)
 
75
  majority_voted_ans = get_majority_vote(all_answers)
76
+ if success:
77
+
78
+ expression = majority_voted_ans
79
+ if type_check(expression) == "Polynomial":
80
+ plotfile = draw_polynomial_plot(expression)
81
  else:
82
+ plotfile = "polynomial_plot.png"
 
 
 
 
 
 
 
 
 
83
 
84
  # Find the steps corresponding to the majority voted answer
85
  for i, ans in enumerate(all_answers):
86
  if ans == majority_voted_ans:
87
  steps_solution = steps_list[i]
88
+ answer = parse_prediction(steps_solution)
89
  break
90
  else:
91
+ answer = majority_voted_ans
92
  steps_solution = "No steps found"
93
 
94
+ return answer, steps_solution, plotfile
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
+ # Function to handle chat-like interaction and merge plot into chat history
97
+ def chat_interface(history, question):
98
+ final_answer, steps_solution, plotfile = majority_vote_with_steps(question, iterations)
99
+
100
+ # Convert the plot image to base64 for embedding in chat (if plot exists)
101
+ if plotfile:
102
+ history.append(("what is the sum of polynomial 2x+3 and 3x?", f"Answer: \n{steps_solution}"))
103
+
104
+ with open(plotfile, "rb") as image_file:
105
+ image_bytes = image_file.read()
106
+ base64_image = base64.b64encode(image_bytes).decode("utf-8")
107
+ image_data = f'<img src="data:image/png;base64,{base64_image}" width="300"/>'
108
+ history.append(("", image_data))
109
+ else:
110
+ history.append(("MathBot", f"Answer: \n{steps_solution}"))
111
+
112
+ return history
113
+
114
+ custom_css = """
115
+ #math_question label {
116
+ font-size: 20px; /* Increase label font size */
117
+ font-weight: bold; /* Optional: make the label bold */
118
+ }
119
+
120
+ #math_question textarea {
121
+ font-size: 20px; /* Increase font size */
122
+ }
123
+ """
124
+ # Gradio app setup using Blocks
125
+ with gr.Blocks(css=custom_css) as interface:
126
+ chatbot = gr.Chatbot(label="Chat with MathBot", elem_id="chat_history",height="70vh")
127
+ math_question = gr.Textbox(label="Your Question", placeholder="Ask a math question...", elem_id="math_question")
128
+
129
+ math_question.submit(chat_interface, inputs=[chatbot, math_question], outputs=[chatbot])
130
 
131
+ interface.launch()
 
temp.py CHANGED
@@ -2,7 +2,7 @@ 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
6
 
7
 
8
  import re
@@ -13,9 +13,14 @@ tokenizer = AutoTokenizer.from_pretrained("AI-MO/NuminaMath-7B-TIR")
13
  model_path = snapshot_download(repo_id="Makima57/deepseek-math-Numina")
14
  generator = ctranslate2.Generator(model_path, device="cpu", compute_type="int8")
15
  iterations = 4
 
16
 
17
  # Function to generate predictions using the model
18
  def get_prediction(question):
 
 
 
 
19
  input_text = model_prompt + question
20
  input_tokens = tokenizer.tokenize(input_text)
21
  results = generator.generate_batch(
@@ -56,6 +61,7 @@ def majority_vote_with_steps(question, num_iterations=10):
56
  for _ in range(num_iterations):
57
  prediction = get_prediction(question)
58
  answer,sucess= postprocess_completion(prediction, return_status=True, last_code_block=True)
 
59
  if sucess:
60
  all_predictions.append(prediction)
61
  all_answers.append(answer)
@@ -68,14 +74,15 @@ def majority_vote_with_steps(question, num_iterations=10):
68
  steps_list.append(steps)
69
 
70
  majority_voted_ans = get_majority_vote(all_answers)
71
- if success:
72
  print(type_check(majority_voted_ans))
73
- if type_check(expression) == "Polynomial":
74
- plotfile = draw_polynomial_plot(expression)
75
  else:
76
- if os.path.exists("thankyou.png"):
77
- plotfile = "thankyou.png"
78
  else:
 
79
  plotfile = None
80
 
81
 
@@ -97,6 +104,8 @@ def majority_vote_with_steps(question, num_iterations=10):
97
 
98
  def gradio_interface(question, correct_answer):
99
  final_answer, steps_solution,plotfile = majority_vote_with_steps(question, iterations)
 
 
100
  return question, final_answer, steps_solution, correct_answer,plotfile
101
 
102
  # Custom CSS for enhanced design (unchanged)
@@ -205,7 +214,7 @@ interface = gr.Interface(
205
  gr.Textbox(label="Question", interactive=False), # Non-editable
206
  gr.Textbox(label="Answer", interactive=False), # Non-editable
207
  gr.Textbox(label="Solution", interactive=True), # Editable textbox for correct solution
208
- gr.Image(label="Polynomial Plot")
209
  ],
210
  title="πŸ”’ Math Question Solver",
211
  description="Enter a math question to get the model's majority-voted answer and steps to solve the problem.",
 
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
 
7
 
8
  import re
 
13
  model_path = snapshot_download(repo_id="Makima57/deepseek-math-Numina")
14
  generator = ctranslate2.Generator(model_path, device="cpu", compute_type="int8")
15
  iterations = 4
16
+ test=True
17
 
18
  # Function to generate predictions using the model
19
  def get_prediction(question):
20
+ if test==True:
21
+ 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"
22
+
23
+ return text
24
  input_text = model_prompt + question
25
  input_tokens = tokenizer.tokenize(input_text)
26
  results = generator.generate_batch(
 
61
  for _ in range(num_iterations):
62
  prediction = get_prediction(question)
63
  answer,sucess= postprocess_completion(prediction, return_status=True, last_code_block=True)
64
+ print(answer,sucess)
65
  if sucess:
66
  all_predictions.append(prediction)
67
  all_answers.append(answer)
 
74
  steps_list.append(steps)
75
 
76
  majority_voted_ans = get_majority_vote(all_answers)
77
+ if sucess:
78
  print(type_check(majority_voted_ans))
79
+ if type_check(majority_voted_ans) == "Polynomial":
80
+ plotfile = draw_polynomial_plot(majority_voted_ans)
81
  else:
82
+ if os.path.exists("polynomial_plot.png"):
83
+ plotfile = "polynomial_plot.png"
84
  else:
85
+
86
  plotfile = None
87
 
88
 
 
104
 
105
  def gradio_interface(question, correct_answer):
106
  final_answer, steps_solution,plotfile = majority_vote_with_steps(question, iterations)
107
+ print(plotfile)
108
+ print(os.path.exists(plotfile),os.path.exists("polynomial_plot.png"))
109
  return question, final_answer, steps_solution, correct_answer,plotfile
110
 
111
  # Custom CSS for enhanced design (unchanged)
 
214
  gr.Textbox(label="Question", interactive=False), # Non-editable
215
  gr.Textbox(label="Answer", interactive=False), # Non-editable
216
  gr.Textbox(label="Solution", interactive=True), # Editable textbox for correct solution
217
+ gr.Image(label="Polynomial Plot", type="filepath")
218
  ],
219
  title="πŸ”’ Math Question Solver",
220
  description="Enter a math question to get the model's majority-voted answer and steps to solve the problem.",