Pra-tham commited on
Commit
48bf4bb
β€’
1 Parent(s): 6e340e9
Files changed (2) hide show
  1. app.py +159 -28
  2. temp.py +191 -0
app.py CHANGED
@@ -1,63 +1,194 @@
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
- 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
6
+ import re
7
+ import os
8
  # Define the model and tokenizer loading
9
+ model_prompt = "Explain and solve the following mathematical problem step by step, showing all work: "
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(
20
+ [input_tokens],
21
+ max_length=512,
22
+ sampling_temperature=0.7,
23
+ sampling_topk=40,
24
+ )
25
  output_tokens = results[0].sequences[0]
26
  predicted_answer = tokenizer.convert_tokens_to_string(output_tokens)
27
  return predicted_answer
28
 
29
+ # Function to parse the prediction to extract the answer and steps
30
+ def parse_prediction(prediction):
31
+ lines = prediction.strip().split('\n')
32
+ answer = None
33
+ steps = []
34
+ for line in lines:
35
+ # Check for "Answer:" or "answer:"
36
+ match = re.match(r'^\s*(?:Answer|answer)\s*[:=]\s*(.*)', line)
37
+ if match:
38
+ answer = match.group(1).strip()
39
+ else:
40
+ steps.append(line)
41
+ if answer is None:
42
+ # If no "Answer:" found, assume last line is the answer
43
+ answer = lines[-1].strip()
44
+ steps = lines[:-1]
45
+ steps_text = '\n'.join(steps).strip()
46
+ return answer, steps_text
47
+
48
+ # Function to perform majority voting and get steps
49
+ def majority_vote_with_steps(question, num_iterations=10):
50
  all_predictions = []
51
+ all_answers = []
52
+ steps_list = []
53
+
54
  for _ in range(num_iterations):
55
  prediction = get_prediction(question)
56
+ answer, steps = parse_prediction(prediction)
57
  all_predictions.append(prediction)
58
+ all_answers.append(answer)
59
+ steps_list.append(steps)
60
+
61
+ # Get the majority voted answer
62
+ majority_voted_ans = get_majority_vote(all_answers)
63
+
64
+ # Find the steps corresponding to the majority voted answer
65
+ for i, ans in enumerate(all_answers):
66
+ if ans == majority_voted_ans:
67
+ steps_solution = steps_list[i]
68
+ break
69
+ else:
70
+ steps_solution = "No steps found"
71
+
72
+ return majority_voted_ans, steps_solution
73
 
74
  # Gradio interface for user input and output
75
  def gradio_interface(question, correct_answer):
76
+ final_answer, steps_solution = majority_vote_with_steps(question, iterations)
77
+ return question, final_answer, steps_solution, correct_answer
78
+
79
+ # Custom CSS for enhanced design (unchanged)
80
+ custom_css = """
81
+ body {
82
+ background-color: #fafafa;
83
+ font-family: 'Open Sans', sans-serif;
84
+ }
85
+ .gradio-container {
86
+ background-color: #ffffff;
87
+ border: 3px solid #007acc;
88
+ border-radius: 15px;
89
+ padding: 20px;
90
+ box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
91
+ max-width: 800px;
92
+ margin: 50px auto;
93
+ }
94
+ h1 {
95
+ font-family: 'Poppins', sans-serif;
96
+ color: #007acc;
97
+ font-weight: bold;
98
+ font-size: 32px;
99
+ text-align: center;
100
+ margin-bottom: 20px;
101
+ }
102
+ p {
103
+ font-family: 'Roboto', sans-serif;
104
+ font-size: 18px;
105
+ color: #333;
106
+ text-align: center;
107
+ margin-bottom: 15px;
108
  }
109
+ input, textarea {
110
+ font-family: 'Montserrat', sans-serif;
111
+ font-size: 16px;
112
+ padding: 10px;
113
+ border: 2px solid #007acc;
114
+ border-radius: 10px;
115
+ background-color: #f1f8ff;
116
+ margin-bottom: 15px;
117
+ }
118
+ #math_question, #correct_answer {
119
+ font-size: 20px;
120
+ font-family: 'Poppins', sans-serif;
121
+ font-weight: 500px;
122
+ color: #007acc;
123
+ margin-bottom: 5px;
124
+ display: inline-block;
125
+ }
126
+
127
+ textarea {
128
+ min-height: 150px;
129
+ }
130
+ .gr-button-primary {
131
+ background-color: #007acc !important;
132
+ color: white !important;
133
+ border-radius: 10px !important;
134
+ font-size: 18px !important;
135
+ font-weight: bold !important;
136
+ padding: 10px 20px !important;
137
+ font-family: 'Montserrat', sans-serif !important;
138
+ transition: background-color 0.3s ease !important;
139
+ }
140
+ .gr-button-primary:hover {
141
+ background-color: #005f99 !important;
142
+ }
143
+ .gr-button-secondary {
144
+ background-color: #f44336 !important;
145
+ color: white !important;
146
+ border-radius: 10px !important;
147
+ font-size: 18px !important;
148
+ font-weight: bold !important;
149
+ padding: 10px 20px !important;
150
+ font-family: 'Montserrat', sans-serif !important;
151
+ transition: background-color 0.3s ease !important;
152
+ }
153
+ .gr-button-secondary:hover {
154
+ background-color: #c62828 !important;
155
+ }
156
+ .gr-output {
157
+ background-color: #e0f7fa;
158
+ border: 2px solid #007acc;
159
+ border-radius: 10px;
160
+ padding: 15px;
161
+ font-size: 16px;
162
+ font-family: 'Roboto', sans-serif;
163
+ font-weight: bold;
164
+ color: #00796b;
165
+ }
166
+ """
167
+
168
+ # Define the directory path
169
+ flagging_dir = "./flagged_data"
170
+
171
+ # Create the directory if it doesn't exist
172
+ if not os.path.exists(flagging_dir):
173
+ os.makedirs(flagging_dir)
174
 
175
+ # Gradio app setup with flagging
176
  interface = gr.Interface(
177
  fn=gradio_interface,
178
  inputs=[
179
+ gr.Textbox(label="🧠 Math Question", placeholder="Enter your math question here...", elem_id="math_question"),
 
180
  ],
181
  outputs=[
182
+ gr.Textbox(label="Majority-Voted Answer", interactive=False), # Non-editable
183
+ gr.Textbox(label="Steps to Solve", interactive=False), # Non-editable
184
+ gr.Textbox(label="βœ… Correct Solution", interactive=True), # Editable textbox for correct solution
185
  ],
186
+ title="πŸ”’ Math Question Solver",
187
+ description="Enter a math question to get the model's majority-voted answer and steps to solve the problem.",
188
+ css=custom_css, # Apply custom CSS
189
+ flagging_dir=flagging_dir, # Directory to save flagged data
190
+ allow_flagging="auto" # Allow users to auto flag data
191
  )
192
 
193
  if __name__ == "__main__":
194
+ interface.launch()
temp.py ADDED
@@ -0,0 +1,191 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
6
+ import re
7
+
8
+ # Define the model and tokenizer loading
9
+ model_prompt = "Explain and solve the following mathematical problem step by step, showing all work: "
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(
20
+ [input_tokens],
21
+ max_length=512,
22
+ sampling_temperature=0.7,
23
+ sampling_topk=40,
24
+ )
25
+ output_tokens = results[0].sequences[0]
26
+ predicted_answer = tokenizer.convert_tokens_to_string(output_tokens)
27
+ return predicted_answer
28
+
29
+ # Function to parse the prediction to extract the answer and steps
30
+ def parse_prediction(prediction):
31
+ lines = prediction.strip().split('\n')
32
+ answer = None
33
+ steps = []
34
+ for line in lines:
35
+ # Check for "Answer:" or "answer:"
36
+ match = re.match(r'^\s*(?:Answer|answer)\s*[:=]\s*(.*)', line)
37
+ if match:
38
+ answer = match.group(1).strip()
39
+ else:
40
+ steps.append(line)
41
+ if answer is None:
42
+ # If no "Answer:" found, assume last line is the answer
43
+ answer = lines[-1].strip()
44
+ steps = lines[:-1]
45
+ steps_text = '\n'.join(steps).strip()
46
+ return answer, steps_text
47
+
48
+ # Function to perform majority voting and get steps
49
+ def majority_vote_with_steps(question, num_iterations=10):
50
+ all_predictions = []
51
+ all_answers = []
52
+ steps_list = []
53
+
54
+ for _ in range(num_iterations):
55
+ prediction = get_prediction(question)
56
+ answer, steps = parse_prediction(prediction)
57
+ all_predictions.append(prediction)
58
+ all_answers.append(answer)
59
+ steps_list.append(steps)
60
+
61
+ # Get the majority voted answer
62
+ majority_voted_ans = get_majority_vote(all_answers)
63
+
64
+ # Find the steps corresponding to the majority voted answer
65
+ for i, ans in enumerate(all_answers):
66
+ if ans == majority_voted_ans:
67
+ steps_solution = steps_list[i]
68
+ break
69
+ else:
70
+ steps_solution = "No steps found"
71
+
72
+ return majority_voted_ans, steps_solution
73
+
74
+ # Gradio interface for user input and output
75
+ def gradio_interface(question, correct_answer):
76
+ final_answer, steps_solution = majority_vote_with_steps(question, iterations)
77
+ return {
78
+ "Question": question,
79
+ "Majority-Voted Answer": final_answer,
80
+ "Steps to Solve": steps_solution,
81
+ "Correct Solution": correct_answer
82
+ }
83
+
84
+ # Custom CSS for enhanced design (unchanged)
85
+ custom_css = """
86
+ body {
87
+ background-color: #fafafa;
88
+ font-family: 'Open Sans', sans-serif;
89
+ }
90
+ .gradio-container {
91
+ background-color: #ffffff;
92
+ border: 3px solid #007acc;
93
+ border-radius: 15px;
94
+ padding: 20px;
95
+ box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
96
+ max-width: 800px;
97
+ margin: 50px auto;
98
+ }
99
+ h1 {
100
+ font-family: 'Poppins', sans-serif;
101
+ color: #007acc;
102
+ font-weight: bold;
103
+ font-size: 32px;
104
+ text-align: center;
105
+ margin-bottom: 20px;
106
+ }
107
+ p {
108
+ font-family: 'Roboto', sans-serif;
109
+ font-size: 18px;
110
+ color: #333;
111
+ text-align: center;
112
+ margin-bottom: 15px;
113
+ }
114
+ input, textarea {
115
+ font-family: 'Montserrat', sans-serif;
116
+ font-size: 16px;
117
+ padding: 10px;
118
+ border: 2px solid #007acc;
119
+ border-radius: 10px;
120
+ background-color: #f1f8ff;
121
+ margin-bottom: 15px;
122
+ }
123
+ #math_question, #correct_answer {
124
+ font-size: 20px;
125
+ font-family: 'Poppins', sans-serif;
126
+ font-weight: 500px;
127
+ color: #007acc;
128
+ margin-bottom: 5px;
129
+ display: inline-block;
130
+ }
131
+
132
+ textarea {
133
+ min-height: 150px;
134
+ }
135
+ .gr-button-primary {
136
+ background-color: #007acc !important;
137
+ color: white !important;
138
+ border-radius: 10px !important;
139
+ font-size: 18px !important;
140
+ font-weight: bold !important;
141
+ padding: 10px 20px !important;
142
+ font-family: 'Montserrat', sans-serif !important;
143
+ transition: background-color 0.3s ease !important;
144
+ }
145
+ .gr-button-primary:hover {
146
+ background-color: #005f99 !important;
147
+ }
148
+ .gr-button-secondary {
149
+ background-color: #f44336 !important;
150
+ color: white !important;
151
+ border-radius: 10px !important;
152
+ font-size: 18px !important;
153
+ font-weight: bold !important;
154
+ padding: 10px 20px !important;
155
+ font-family: 'Montserrat', sans-serif !important;
156
+ transition: background-color 0.3s ease !important;
157
+ }
158
+ .gr-button-secondary:hover {
159
+ background-color: #c62828 !important;
160
+ }
161
+ .gr-output {
162
+ background-color: #e0f7fa;
163
+ border: 2px solid #007acc;
164
+ border-radius: 10px;
165
+ padding: 15px;
166
+ font-size: 16px;
167
+ font-family: 'Roboto', sans-serif;
168
+ font-weight: bold;
169
+ color: #00796b;
170
+ }
171
+ """
172
+
173
+ # Gradio app setup
174
+ interface = gr.Interface(
175
+ fn=gradio_interface,
176
+ inputs=[
177
+ gr.Textbox(label="🧠 Math Question", placeholder="Enter your math question here...", elem_id="math_question"),
178
+
179
+ ],
180
+ outputs=[
181
+ gr.Textbox(label="Majority-Voted Answer", interactive=False), # Non-editable
182
+ gr.Textbox(label="Steps to Solve", interactive=False), # Non-editable
183
+ gr.Textbox(label="βœ… Correct Solution", interactive=True), # Editable textbox for correct solution
184
+ ],
185
+ title="πŸ”’ Math Question Solver",
186
+ description="Enter a math question to get the model's majority-voted answer and steps to solve the problem.",
187
+ css=custom_css # Apply custom CSS
188
+ )
189
+
190
+ if _name_ == "_main_":
191
+ interface.launch()