File size: 11,375 Bytes
e7f0eb4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
import os
from typing import List, Tuple
import re

class PolarisModel:
    """
    POLARIS-4B-Preview: A Post-training recipe for scaling RL on Advanced Reasoning models
    Specialized for mathematical reasoning and problem-solving tasks.
    """
    
    def __init__(self):
        self.model_name = "POLARIS-Project/Polaris-4B-Preview"
        self.device = "cuda" if torch.cuda.is_available() else "cpu"
        self.model = None
        self.tokenizer = None
        self.load_model()
    
    def load_model(self):
        """Load the POLARIS model with optimized settings for reasoning tasks"""
        try:
            # Load tokenizer
            self.tokenizer = AutoTokenizer.from_pretrained(
                self.model_name,
                trust_remote_code=True,
                padding_side="left"
            )
            
            # Set pad token if not exists
            if self.tokenizer.pad_token is None:
                self.tokenizer.pad_token = self.tokenizer.eos_token
            
            # Configure for efficient inference
            if self.device == "cuda":
                # Use 4-bit quantization for GPU to fit 4B model
                quantization_config = BitsAndBytesConfig(
                    load_in_4bit=True,
                    bnb_4bit_compute_dtype=torch.float16,
                    bnb_4bit_use_double_quant=True,
                    bnb_4bit_quant_type="nf4"
                )
                
                self.model = AutoModelForCausalLM.from_pretrained(
                    self.model_name,
                    quantization_config=quantization_config,
                    device_map="auto",
                    trust_remote_code=True,
                    torch_dtype=torch.float16
                )
            else:
                # CPU inference
                self.model = AutoModelForCausalLM.from_pretrained(
                    self.model_name,
                    device_map="cpu",
                    trust_remote_code=True,
                    torch_dtype=torch.float32
                )
            
            print(f"โœ… POLARIS-4B-Preview loaded successfully on {self.device}")
            
        except Exception as e:
            print(f"โŒ Error loading model: {e}")
            # Fallback to a smaller model if POLARIS fails to load
            try:
                print("๐Ÿ”„ Attempting to load fallback model...")
                self.tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
                self.model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
                self.tokenizer.pad_token = self.tokenizer.eos_token
                print("โœ… Fallback model loaded")
            except Exception as fallback_error:
                print(f"โŒ Fallback model also failed: {fallback_error}")
                self.model = None
                self.tokenizer = None
    
    def generate_reasoning_response(
        self, 
        prompt: str, 
        max_length: int = 2048,
        temperature: float = 0.7,
        top_p: float = 0.9,
        do_sample: bool = True,
        num_return_sequences: int = 1
    ) -> str:
        """
        Generate response with chain-of-thought reasoning optimized for POLARIS
        """
        if not self.model or not self.tokenizer:
            return "โŒ Model not loaded. Please check the model loading status."
        
        try:
            # Format prompt for mathematical reasoning
            formatted_prompt = self.format_reasoning_prompt(prompt)
            
            # Tokenize input
            inputs = self.tokenizer.encode(
                formatted_prompt, 
                return_tensors="pt", 
                truncation=True, 
                max_length=1024
            ).to(self.device)
            
            # Generate with optimized parameters for reasoning
            with torch.no_grad():
                outputs = self.model.generate(
                    inputs,
                    max_new_tokens=max_length - inputs.shape[1],
                    temperature=temperature,
                    top_p=top_p,
                    do_sample=do_sample,
                    num_return_sequences=num_return_sequences,
                    pad_token_id=self.tokenizer.pad_token_id,
                    eos_token_id=self.tokenizer.eos_token_id,
                    repetition_penalty=1.1,
                    length_penalty=1.0,
                    early_stopping=True
                )
            
            # Decode response
            full_response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
            response = full_response[len(formatted_prompt):].strip()
            
            return self.format_response(response)
            
        except Exception as e:
            return f"โŒ Error generating response: {str(e)}"
    
    def format_reasoning_prompt(self, user_input: str) -> str:
        """Format prompt to encourage step-by-step reasoning"""
        if any(keyword in user_input.lower() for keyword in ['solve', 'calculate', 'find', 'prove', 'show']):
            return f"""<|im_start|>system
You are POLARIS, an advanced reasoning model specialized in mathematical problem-solving. 
Approach each problem step-by-step with clear reasoning. Show your work and explain each step.
<|im_end|>
<|im_start|>user
{user_input}

Please solve this step-by-step:
<|im_end|>
<|im_start|>assistant
I'll solve this step-by-step:

"""
        else:
            return f"""<|im_start|>system
You are POLARIS, an advanced reasoning model. Provide thoughtful, well-reasoned responses.
<|im_end|>
<|im_start|>user
{user_input}
<|im_end|>
<|im_start|>assistant
"""
    
    def format_response(self, response: str) -> str:
        """Clean and format the model response"""
        # Remove potential artifacts
        response = re.sub(r'<\|im_start\|>.*?<\|im_end\|>', '', response, flags=re.DOTALL)
        response = response.strip()
        
        # Ensure proper formatting for mathematical expressions
        if '$$' in response or '\\(' in response:
            response = "๐Ÿงฎ **Mathematical Solution:**\n\n" + response
        
        return response

# Initialize the model
polaris_model = PolarisModel()

def chat_with_polaris(
    message: str, 
    history: List[Tuple[str, str]] = None,
    temperature: float = 0.7,
    max_length: int = 1024
) -> Tuple[str, List[Tuple[str, str]]]:
    """Main chat function for Gradio interface"""
    if history is None:
        history = []
    
    if not message.strip():
        return "", history
    
    # Generate response
    response = polaris_model.generate_reasoning_response(
        message, 
        temperature=temperature,
        max_length=max_length
    )
    
    # Update history
    history.append((message, response))
    
    return "", history

def clear_chat():
    """Clear the chat history"""
    return [], []

def get_model_info():
    """Return information about the POLARIS model"""
    return """
## ๐ŸŒ  POLARIS-4B-Preview

**POLARIS** is a post-training recipe for scaling Reinforcement Learning on Advanced Reasoning models. 

### Key Features:
- **4B parameters** optimized for mathematical reasoning
- **Advanced Chain-of-Thought** reasoning capabilities  
- **Superior performance** on mathematical benchmarks (AIME, AMC, Olympiad)
- **Outperforms larger models** through specialized RL training

### Benchmark Results:
- **AIME24**: 81.2% (avg@32)
- **AIME25**: 79.4% (avg@32) 
- **AMC23**: 94.8% (avg@8)
- **Minerva Math**: 44.0% (avg@4)
- **Olympiad Bench**: 69.1% (avg@4)

### Best Use Cases:
- Mathematical problem solving
- Step-by-step reasoning tasks
- Competition math problems
- Logical reasoning challenges

Try asking mathematical questions or reasoning problems!
"""

# Create example problems for the interface
example_problems = [
    "Solve: If x + y = 10 and x - y = 4, find the values of x and y.",
    "Find the derivative of f(x) = 3xยฒ + 2x - 1",
    "Prove that the square root of 2 is irrational.",
    "A rectangle has a perimeter of 24 cm and an area of 35 cmยฒ. Find its dimensions.",
    "What is the sum of the first 100 positive integers?",
    "Solve the quadratic equation: 2xยฒ - 7x + 3 = 0"
]

# Create the Gradio interface
with gr.Blocks(
    title="๐ŸŒ  POLARIS-4B-Preview - Advanced Reasoning Model",
    theme=gr.themes.Soft(),
    css="""
    .gradio-container {
        max-width: 1200px !important;
    }
    .chat-message {
        font-size: 16px !important;
    }
    """
) as demo:
    
    gr.Markdown("""
    # ๐ŸŒ  POLARIS-4B-Preview
    ## Advanced Reasoning Model for Mathematical Problem Solving
    
    POLARIS uses reinforcement learning to achieve state-of-the-art performance on mathematical reasoning tasks.
    Try asking mathematical questions, logic problems, or step-by-step reasoning challenges!
    """)
    
    with gr.Row():
        with gr.Column(scale=3):
            chatbot = gr.Chatbot(
                height=600,
                show_label=False,
                container=True,
                bubble_full_width=False
            )
            
            with gr.Row():
                msg = gr.Textbox(
                    placeholder="Enter your mathematical problem or reasoning question...",
                    show_label=False,
                    scale=5,
                    container=False
                )
                submit_btn = gr.Button("๐Ÿš€ Solve", scale=1, variant="primary")
                clear_btn = gr.Button("๐Ÿ—‘๏ธ Clear", scale=1)
        
        with gr.Column(scale=1):
            gr.Markdown("### โš™๏ธ Settings")
            
            temperature = gr.Slider(
                minimum=0.1,
                maximum=1.5,
                value=0.7,
                step=0.1,
                label="Temperature",
                info="Higher = more creative"
            )
            
            max_length = gr.Slider(
                minimum=256,
                maximum=2048,
                value=1024,
                step=128,
                label="Max Response Length",
                info="Maximum tokens to generate"
            )
            
            gr.Markdown("### ๐Ÿ“š Example Problems")
            
            for i, example in enumerate(example_problems):
                gr.Button(
                    f"Example {i+1}",
                    size="sm"
                ).click(
                    lambda x=example: x,
                    outputs=[msg]
                )
    
    with gr.Row():
        with gr.Column():
            gr.Markdown("### ๐Ÿ“Š Model Information")
            model_info = gr.Markdown(get_model_info())
    
    # Event handlers
    submit_btn.click(
        chat_with_polaris,
        inputs=[msg, chatbot, temperature, max_length],
        outputs=[msg, chatbot]
    )
    
    msg.submit(
        chat_with_polaris,
        inputs=[msg, chatbot, temperature, max_length], 
        outputs=[msg, chatbot]
    )
    
    clear_btn.click(
        clear_chat,
        outputs=[chatbot]
    )

# Launch configuration
if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=True,
        show_error=True
    )