Pra-tham commited on
Commit
e8c0a63
1 Parent(s): 42bb7ad

added draawubg feautyre

Browse files
__pycache__/codeexecutor.cpython-312.pyc ADDED
Binary file (7.45 kB). View file
 
app.py CHANGED
@@ -60,16 +60,23 @@ def majority_vote_with_steps(question, num_iterations=10):
60
  all_predictions.append(prediction)
61
  all_answers.append(answer)
62
  steps_list.append(prediction)
63
- majority_voted_ans = get_majority_vote(all_answers)
64
  else:
65
  answer, steps = parse_prediction(prediction)
66
  all_predictions.append(prediction)
67
  all_answers.append(answer)
68
  steps_list.append(steps)
69
- majority_voted_ans = get_majority_vote(all_answers)
70
- if type_check(majority_voted_ans)=="Polynomial":
71
- pass
72
- #draw plot of polynomial
 
 
 
 
 
 
 
73
 
74
 
75
  # Get the majority voted answer
@@ -86,11 +93,11 @@ def majority_vote_with_steps(question, num_iterations=10):
86
  answer=majority_voted_ans
87
  steps_solution = "No steps found"
88
 
89
- return answer, steps_solution
90
 
91
  def gradio_interface(question, correct_answer):
92
- final_answer, steps_solution = majority_vote_with_steps(question, iterations)
93
- return question, final_answer, steps_solution, correct_answer
94
 
95
  # Custom CSS for enhanced design (unchanged)
96
  custom_css = """
 
60
  all_predictions.append(prediction)
61
  all_answers.append(answer)
62
  steps_list.append(prediction)
63
+
64
  else:
65
  answer, steps = parse_prediction(prediction)
66
  all_predictions.append(prediction)
67
  all_answers.append(answer)
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
 
82
  # Get the majority voted answer
 
93
  answer=majority_voted_ans
94
  steps_solution = "No steps found"
95
 
96
+ return answer, steps_solution,plotfile
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)
103
  custom_css = """
codeexecutor.py CHANGED
@@ -6,10 +6,13 @@ import multiprocessing
6
  from collections import Counter
7
  from contextlib import contextmanager
8
  from dataclasses import dataclass
 
 
 
9
 
10
 
11
  class PythonREPL:
12
- def __init__(self, timeout=5):
13
  self.timeout = timeout
14
 
15
  @staticmethod
@@ -115,7 +118,7 @@ def get_majority_vote(answers):
115
  return value
116
 
117
 
118
- def type_check(self,expr_str):
119
 
120
 
121
  expr = sp.sympify(expr_str)
@@ -159,7 +162,8 @@ def draw_polynomial_plot(expression):
159
  plt.close()
160
 
161
  return plot_filename
162
- except:
 
163
  return None
164
 
165
 
 
6
  from collections import Counter
7
  from contextlib import contextmanager
8
  from dataclasses import dataclass
9
+ import sympy as sp
10
+ import numpy as np
11
+ import matplotlib.pyplot as plt
12
 
13
 
14
  class PythonREPL:
15
+ def __init__(self, timeout=15):
16
  self.timeout = timeout
17
 
18
  @staticmethod
 
118
  return value
119
 
120
 
121
+ def type_check(expr_str):
122
 
123
 
124
  expr = sp.sympify(expr_str)
 
162
  plt.close()
163
 
164
  return plot_filename
165
+ except Exception as e:
166
+ print(f"Error in draw_polynomial_plot: {e}")
167
  return None
168
 
169
 
flagged_data/Polynomial Plot/b857f890620768c0e173/polynomial_plot.png ADDED
flagged_data/log.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ Chat with MathBot,Your Question,Chat History,Polynomial Plot,flag,username,timestamp
2
+ [],fdfd,"[[""User"", ""fdfd""], [""MathBot"", ""Answer: ('The sum of the polynomials \\\\(2x + 3\\\\) and \\\\(3x\\\\) is \\\\(\\\\boxed{5x + 3}\\\\).', \""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}\\\\).\"")\nSteps:\nSolve 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}\\).""]]",,,,2024-10-03 17:19:14.236777
3
+ [],dfafd,"[[""User"", ""dfafd""], [""MathBot"", ""Answer: ('The sum of the polynomials \\\\(2x + 3\\\\) and \\\\(3x\\\\) is \\\\(\\\\boxed{5x + 3}\\\\).', \""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}\\\\).\"")\nSteps:\nSolve 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}\\).""]]",flagged_data\Polynomial Plot\b857f890620768c0e173\polynomial_plot.png,,,2024-10-03 17:44:52.573279
polynomial_plot.png ADDED
requirements.txt CHANGED
@@ -3,3 +3,4 @@ gradio
3
  ctranslate2
4
  transformers
5
  torch
 
 
3
  ctranslate2
4
  transformers
5
  torch
6
+ numpy
temp.py CHANGED
@@ -90,96 +90,10 @@ def majority_vote_with_steps(question, num_iterations=10):
90
 
91
  def gradio_interface(question, correct_answer):
92
  final_answer, steps_solution,plotfile = majority_vote_with_steps(question, iterations)
93
- return question, final_answer, steps_solution, correct_answer,plotfile
94
 
95
  # Custom CSS for enhanced design (unchanged)
96
- custom_css = """
97
- body {
98
- background-color: #fafafa;
99
- font-family: 'Open Sans', sans-serif;
100
- }
101
- .gradio-container {
102
- background-color: #ffffff;
103
- border: 3px solid #007acc;
104
- border-radius: 15px;
105
- padding: 20px;
106
- box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
107
- max-width: 800px;
108
- margin: 50px auto;
109
- }
110
- h1 {
111
- font-family: 'Poppins', sans-serif;
112
- color: #007acc;
113
- font-weight: bold;
114
- font-size: 32px;
115
- text-align: center;
116
- margin-bottom: 20px;
117
- }
118
- p {
119
- font-family: 'Roboto', sans-serif;
120
- font-size: 18px;
121
- color: #333;
122
- text-align: center;
123
- margin-bottom: 15px;
124
- }
125
- input, textarea {
126
- font-family: 'Montserrat', sans-serif;
127
- font-size: 16px;
128
- padding: 10px;
129
- border: 2px solid #007acc;
130
- border-radius: 10px;
131
- background-color: #f1f8ff;
132
- margin-bottom: 15px;
133
- }
134
- #math_question, #correct_answer {
135
- font-size: 20px;
136
- font-family: 'Poppins', sans-serif;
137
- font-weight: 500px;
138
- color: #007acc;
139
- margin-bottom: 5px;
140
- display: inline-block;
141
- }
142
-
143
- textarea {
144
- min-height: 150px;
145
- }
146
- .gr-button-primary {
147
- background-color: #007acc !important;
148
- color: white !important;
149
- border-radius: 10px !important;
150
- font-size: 18px !important;
151
- font-weight: bold !important;
152
- padding: 10px 20px !important;
153
- font-family: 'Montserrat', sans-serif !important;
154
- transition: background-color 0.3s ease !important;
155
- }
156
- .gr-button-primary:hover {
157
- background-color: #005f99 !important;
158
- }
159
- .gr-button-secondary {
160
- background-color: #f44336 !important;
161
- color: white !important;
162
- border-radius: 10px !important;
163
- font-size: 18px !important;
164
- font-weight: bold !important;
165
- padding: 10px 20px !important;
166
- font-family: 'Montserrat', sans-serif !important;
167
- transition: background-color 0.3s ease !important;
168
- }
169
- .gr-button-secondary:hover {
170
- background-color: #c62828 !important;
171
- }
172
- .gr-output {
173
- background-color: #e0f7fa;
174
- border: 2px solid #007acc;
175
- border-radius: 10px;
176
- padding: 15px;
177
- font-size: 16px;
178
- font-family: 'Roboto', sans-serif;
179
- font-weight: bold;
180
- color: #00796b;
181
- }
182
- """
183
 
184
  # Define the directory path
185
  flagging_dir = "./flagged_data"
 
90
 
91
  def gradio_interface(question, correct_answer):
92
  final_answer, steps_solution,plotfile = majority_vote_with_steps(question, iterations)
93
+ return question, final_answer, steps_solution, correct_answer,
94
 
95
  # Custom CSS for enhanced design (unchanged)
96
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
  # Define the directory path
99
  flagging_dir = "./flagged_data"
temp2.py ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 re
7
+ import os
8
+
9
+ # Define the model and tokenizer loading
10
+ model_prompt = "Explain and solve the following mathematical problem step by step, showing all work: "
11
+ # tokenizer = AutoTokenizer.from_pretrained("AI-MO/NuminaMath-7B-TIR")
12
+ # model_path = snapshot_download(repo_id="Makima57/deepseek-math-Numina")
13
+ # generator = ctranslate2.Generator(model_path, device="cpu", compute_type="int8")
14
+ iterations = 4
15
+
16
+ # # Function to generate predictions using the model
17
+ # def get_prediction(question):
18
+ # input_text = model_prompt + question
19
+ # input_tokens = tokenizer.tokenize(input_text)
20
+ # results = generator.generate_batch(
21
+ # [input_tokens],
22
+ # max_length=512,
23
+ # sampling_temperature=0.7,
24
+ # sampling_topk=40,
25
+ # )
26
+ # output_tokens = results[0].sequences[0]
27
+ # predicted_answer = tokenizer.convert_tokens_to_string(output_tokens)
28
+ # return predicted_answer
29
+
30
+ def get_prediction(question):
31
+ return "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"
32
+
33
+ # Function to parse the prediction to extract the answer and steps
34
+ def parse_prediction(prediction):
35
+ lines = prediction.strip().split('\n')
36
+ answer = None
37
+ steps = []
38
+ for line in lines:
39
+ # Check for "Answer:" or "answer:"
40
+ match = re.match(r'^\s*(?:Answer|answer)\s*[:=]\s*(.*)', line)
41
+ if match:
42
+ answer = match.group(1).strip()
43
+ else:
44
+ steps.append(line)
45
+ if answer is None:
46
+ # If no "Answer:" found, assume last line is the answer
47
+ answer = lines[-1].strip()
48
+ steps = lines
49
+ steps_text = '\n'.join(steps).strip()
50
+ return answer, steps_text
51
+
52
+ def extract_boxed_answer(text):
53
+ # Regular expression to find the content inside \\boxed{}
54
+ match = re.search(r'\\boxed\{(.*?)\}', text)
55
+ if match:
56
+ return match.group(1) # Return the content inside the \\boxed{}
57
+ return None
58
+
59
+
60
+ # Function to perform majority voting and get steps
61
+ def majority_vote_with_steps(question, num_iterations=10):
62
+ all_predictions = []
63
+ all_answers = []
64
+ steps_list = []
65
+
66
+ for _ in range(num_iterations):
67
+ prediction = get_prediction(question)
68
+ answer, success = postprocess_completion(prediction, return_status=True, last_code_block=True)
69
+
70
+ if success:
71
+ all_predictions.append(prediction)
72
+ all_answers.append(answer)
73
+ steps_list.append(prediction)
74
+
75
+
76
+ else:
77
+ answer, steps = parse_prediction(prediction)
78
+ all_predictions.append(prediction)
79
+ all_answers.append(answer)
80
+ steps_list.append(steps)
81
+
82
+ if success:
83
+ majority_voted_ans = get_majority_vote(all_answers)
84
+ expression=majority_voted_ans
85
+ print(type_check(expression))
86
+ if type_check(expression) == "Polynomial":
87
+ plotfile = draw_polynomial_plot(expression)
88
+ else:
89
+ plotfile = None
90
+
91
+
92
+
93
+ # Draw plot of polynomial
94
+
95
+ # Find the steps corresponding to the majority voted answer
96
+ for i, ans in enumerate(all_answers):
97
+ if ans == majority_voted_ans:
98
+ steps_solution = steps_list[i]
99
+ answer = parse_prediction(steps_solution)
100
+ break
101
+ else:
102
+ answer = majority_voted_ans
103
+ steps_solution = "No steps found"
104
+
105
+ return answer, steps_solution, plotfile
106
+
107
+ # Function to handle chat-like interaction
108
+ def chat_interface(history, question):
109
+ # Get the answer and steps from the majority voting method
110
+ final_answer, steps_solution, plotfile = majority_vote_with_steps(question, iterations)
111
+
112
+ # Append the question and answer to the chat history
113
+ history.append(("User", question))
114
+ history.append(("MathBot", f"Answer: {final_answer}\nSteps:\n{steps_solution}"))
115
+
116
+ return history, plotfile
117
+
118
+ # Gradio app setup with chat UI
119
+ interface = gr.Interface(
120
+ fn=chat_interface,
121
+ inputs=[
122
+ gr.Chatbot(label="Chat with MathBot", elem_id="chat_history"),
123
+ gr.Textbox(label="Your Question", placeholder="Ask a math question...", elem_id="math_question"),
124
+ ],
125
+ outputs=[
126
+ gr.Chatbot(label="Chat History"), # Chat-like display of conversation
127
+ gr.Image(label="Polynomial Plot")
128
+ ],
129
+ title="🔢 Math Question Solver - Chat Mode",
130
+ description="Chat with MathBot and ask any math-related question. It will explain the solution step by step and provide a majority-voted answer.",
131
+ allow_flagging="auto",
132
+ flagging_dir="./flagged_data",
133
+ )
134
+
135
+ if __name__ == "__main__":
136
+ interface.launch()
137
+ # history, plotfile=chat_interface(["hello"], ["what is the sum of 2x+3 and 3x"])
138
+ # print(history, plotfile)
temp3.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sympy as sp
2
+
3
+ # Define the variable
4
+ x = sp.symbols('x')
5
+
6
+ # Define the polynomials
7
+ poly1 = 2*x + 3
8
+ poly2 = 3*x
9
+
10
+ # Sum the polynomials
11
+ sum_poly = poly1 + poly2
12
+
13
+ # Simplify the resulting polynomial
14
+ simplified_sum_poly = sp.simplify(sum_poly)
15
+
16
+ # Print the simplified polynomial
17
+ print(simplified_sum_poly)
thankyou.png ADDED