File size: 6,308 Bytes
8a529be
 
 
 
0302f9b
 
f80e88c
444d1cb
0302f9b
444d1cb
 
 
8da95b6
444d1cb
 
 
 
 
0302f9b
 
 
 
 
 
444d1cb
 
8a529be
444d1cb
0302f9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd8428b
444d1cb
0302f9b
 
 
444d1cb
 
0302f9b
444d1cb
0302f9b
 
 
 
 
 
 
 
 
 
 
 
 
 
fd8428b
444d1cb
 
 
fd8428b
a3f7371
444d1cb
0302f9b
8da95b6
 
8d5df47
 
8da95b6
 
8d5df47
 
8da95b6
 
8d5df47
 
 
8da95b6
8d5df47
 
 
 
 
8da95b6
8d5df47
 
 
 
 
8da95b6
8d5df47
 
8da95b6
 
8d5df47
8da95b6
8d5df47
 
 
 
 
 
b6b9c26
71a7f13
e0c93c6
0302f9b
71a7f13
 
 
a8878d7
b6b9c26
a8878d7
 
 
71b8cde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8da95b6
 
f80e88c
 
 
 
 
 
 
a3f7371
8a529be
444d1cb
 
b6b9c26
444d1cb
 
57dc295
 
 
444d1cb
5b32090
fd8428b
a3f7371
f80e88c
a3f7371
444d1cb
 
 
cc60cf1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import gradio as gr
import ctranslate2
from transformers import AutoTokenizer
from huggingface_hub import snapshot_download
from codeexecutor import get_majority_vote
import re
import os
# Define the model and tokenizer loading
model_prompt = "Explain and solve the following mathematical problem step by step, showing all work: "
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],
        max_length=512,
        sampling_temperature=0.7,
        sampling_topk=40,
    )
    output_tokens = results[0].sequences[0]
    predicted_answer = tokenizer.convert_tokens_to_string(output_tokens)
    return predicted_answer

# Function to parse the prediction to extract the answer and steps
def parse_prediction(prediction):
    lines = prediction.strip().split('\n')
    answer = None
    steps = []
    for line in lines:
        # Check for "Answer:" or "answer:"
        match = re.match(r'^\s*(?:Answer|answer)\s*[:=]\s*(.*)', line)
        if match:
            answer = match.group(1).strip()
        else:
            steps.append(line)
    if answer is None:
        # If no "Answer:" found, assume last line is the answer
        answer = lines[-1].strip()
        steps = lines[:-1]
    steps_text = '\n'.join(steps).strip()
    return answer, steps_text

# Function to perform majority voting and get steps
def majority_vote_with_steps(question, num_iterations=10):
    all_predictions = []
    all_answers = []
    steps_list = []

    for _ in range(num_iterations):
        prediction = get_prediction(question)
        answer, steps = parse_prediction(prediction)
        all_predictions.append(prediction)
        all_answers.append(answer)
        steps_list.append(steps)

    # Get the majority voted answer
    majority_voted_ans = get_majority_vote(all_answers)

    # Find the steps corresponding to the majority voted answer
    for i, ans in enumerate(all_answers):
        if ans == majority_voted_ans:
            steps_solution = steps_list[i]
            break
    else:
        steps_solution = "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, final_answer, steps_solution, correct_answer

# Custom CSS for enhanced design (unchanged)
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;
        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;
    }
"""

# Define the directory path
flagging_dir = "./flagged_data"

# Create the directory if it doesn't exist
if not os.path.exists(flagging_dir):
    os.makedirs(flagging_dir)

# Gradio app setup with flagging
interface = gr.Interface(
    fn=gradio_interface,
    inputs=[
        gr.Textbox(label="🧠 Math Question", placeholder="Enter your math question here...", elem_id="math_question"),
    ],
    outputs=[
        gr.Textbox(label="Majority-Voted Answer", interactive=False),  # Non-editable
        gr.Textbox(label="Steps to Solve", interactive=False),  # Non-editable
        gr.Textbox(label="βœ… Correct Solution", interactive=True),  # Editable textbox for correct solution
    ],
    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
    flagging_dir=flagging_dir,  # Directory to save flagged data
    allow_flagging="auto"  # Allow users to auto flag data
)

if __name__ == "__main__":
    interface.launch()