Update app.py
Browse files
app.py
CHANGED
|
@@ -2,9 +2,8 @@ import gradio as gr
|
|
| 2 |
import os
|
| 3 |
from openai import OpenAI
|
| 4 |
import time
|
| 5 |
-
import json
|
| 6 |
|
| 7 |
-
def solve_competitive_problem(problem_statement, language_choice, api_key,
|
| 8 |
"""
|
| 9 |
Generate a solution for a competitive programming problem
|
| 10 |
|
|
@@ -12,13 +11,12 @@ def solve_competitive_problem(problem_statement, language_choice, api_key, remem
|
|
| 12 |
problem_statement (str): The problem statement
|
| 13 |
language_choice (str): Programming language for the solution
|
| 14 |
api_key (str): OpenRouter API key
|
| 15 |
-
remember_api_key (bool): Whether to remember the API key
|
| 16 |
progress: Gradio progress tracker
|
| 17 |
|
| 18 |
Returns:
|
| 19 |
str: Step-by-step solution with code
|
| 20 |
"""
|
| 21 |
-
if not api_key
|
| 22 |
return "Error: Please provide your OpenRouter API key."
|
| 23 |
|
| 24 |
if not problem_statement.strip():
|
|
@@ -72,29 +70,26 @@ Format your answer with clear headings and subheadings. Use markdown formatting
|
|
| 72 |
|
| 73 |
progress(0.5, "Generating solution...")
|
| 74 |
|
| 75 |
-
# Call the model
|
| 76 |
completion = client.chat.completions.create(
|
| 77 |
extra_headers={
|
| 78 |
"HTTP-Referer": "https://competitive-programming-assistant.app",
|
| 79 |
"X-Title": "Competitive Programming Assistant",
|
| 80 |
},
|
| 81 |
model="open-r1/olympiccoder-7b:free",
|
| 82 |
-
messages=[
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
| 86 |
temperature=0.7,
|
| 87 |
-
stream=
|
| 88 |
)
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
if chunk.choices[0].delta.content is not None:
|
| 94 |
-
solution += chunk.choices[0].delta.content
|
| 95 |
-
# Update progress and yield partial results
|
| 96 |
-
progress(0.5 + (len(solution) % 500) / 1000, "Generating solution...")
|
| 97 |
-
yield solution
|
| 98 |
|
| 99 |
progress(1.0, "Complete!")
|
| 100 |
return solution
|
|
@@ -102,142 +97,27 @@ Format your answer with clear headings and subheadings. Use markdown formatting
|
|
| 102 |
except Exception as e:
|
| 103 |
return f"Error: {str(e)}"
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
"""
|
| 118 |
-
|
| 119 |
-
def save_api_key(api_key, remember):
|
| 120 |
-
"""
|
| 121 |
-
Return a JavaScript snippet that saves or removes the API key from localStorage
|
| 122 |
-
"""
|
| 123 |
-
return f"""
|
| 124 |
-
function() {{
|
| 125 |
-
if ({str(remember).toLowerCase()}) {{
|
| 126 |
-
localStorage.setItem('openrouter_api_key', "{api_key}");
|
| 127 |
-
}} else {{
|
| 128 |
-
localStorage.removeItem('openrouter_api_key');
|
| 129 |
-
}}
|
| 130 |
-
}}
|
| 131 |
-
"""
|
| 132 |
-
|
| 133 |
-
# Define theme options
|
| 134 |
-
light_theme = gr.themes.Soft()
|
| 135 |
-
dark_theme = gr.themes.Soft(
|
| 136 |
-
primary_hue="slate",
|
| 137 |
-
secondary_hue="slate",
|
| 138 |
-
neutral_hue="slate",
|
| 139 |
-
text_size=gr.themes.sizes.text_md,
|
| 140 |
-
)
|
| 141 |
-
|
| 142 |
-
# Create a Gradio interface
|
| 143 |
-
with gr.Blocks(title="Competitive Programming Assistant", theme=light_theme, css="""
|
| 144 |
-
/* Custom CSS for better responsiveness and styling */
|
| 145 |
-
@media (max-width: 768px) {
|
| 146 |
-
.container { padding: 10px !important; }
|
| 147 |
-
.responsive-padding { padding: 8px !important; }
|
| 148 |
-
}
|
| 149 |
-
.code-block {
|
| 150 |
-
background-color: #f7f7f7;
|
| 151 |
-
border-radius: 4px;
|
| 152 |
-
padding: 10px;
|
| 153 |
-
font-family: monospace;
|
| 154 |
-
}
|
| 155 |
-
.dark-mode .code-block {
|
| 156 |
-
background-color: #2d2d2d;
|
| 157 |
-
color: #f1f1f1;
|
| 158 |
-
}
|
| 159 |
-
/* Custom styling for buttons */
|
| 160 |
-
.action-button {
|
| 161 |
-
margin-top: 10px !important;
|
| 162 |
-
}
|
| 163 |
-
""") as app:
|
| 164 |
-
# JavaScript for theme toggling
|
| 165 |
-
app.load(js="""
|
| 166 |
-
function setupThemeToggle() {
|
| 167 |
-
const themeToggle = document.getElementById('theme-toggle-btn');
|
| 168 |
-
const savedTheme = localStorage.getItem('theme');
|
| 169 |
-
|
| 170 |
-
if (savedTheme === 'dark') {
|
| 171 |
-
document.body.classList.add('dark-mode');
|
| 172 |
-
themeToggle.textContent = '☀️ Light Mode';
|
| 173 |
-
}
|
| 174 |
-
|
| 175 |
-
themeToggle.addEventListener('click', function() {
|
| 176 |
-
document.body.classList.toggle('dark-mode');
|
| 177 |
-
const isDark = document.body.classList.contains('dark-mode');
|
| 178 |
-
localStorage.setItem('theme', isDark ? 'dark' : 'light');
|
| 179 |
-
themeToggle.textContent = isDark ? '☀️ Light Mode' : '🌙 Dark Mode';
|
| 180 |
-
|
| 181 |
-
// Update code block styling
|
| 182 |
-
document.querySelectorAll('pre code').forEach(block => {
|
| 183 |
-
if (isDark) {
|
| 184 |
-
block.style.backgroundColor = '#2d2d2d';
|
| 185 |
-
block.style.color = '#f1f1f1';
|
| 186 |
-
} else {
|
| 187 |
-
block.style.backgroundColor = '#f7f7f7';
|
| 188 |
-
block.style.color = '#333';
|
| 189 |
-
}
|
| 190 |
-
});
|
| 191 |
-
});
|
| 192 |
-
}
|
| 193 |
-
|
| 194 |
-
// Initialize syntax highlighting
|
| 195 |
-
function setupPrism() {
|
| 196 |
-
if (typeof Prism !== 'undefined') {
|
| 197 |
-
Prism.highlightAll();
|
| 198 |
-
}
|
| 199 |
-
}
|
| 200 |
-
|
| 201 |
-
// Apply saved API key on load
|
| 202 |
-
document.addEventListener('DOMContentLoaded', function() {
|
| 203 |
-
setupThemeToggle();
|
| 204 |
-
const savedKey = localStorage.getItem('openrouter_api_key');
|
| 205 |
-
if (savedKey) {
|
| 206 |
-
document.querySelector('#api_key_input input').value = savedKey;
|
| 207 |
-
document.querySelector('#remember_api_key input').checked = true;
|
| 208 |
-
}
|
| 209 |
-
});
|
| 210 |
""")
|
| 211 |
-
|
| 212 |
-
# App header with theme toggle
|
| 213 |
-
with gr.Row(elem_classes="responsive-padding"):
|
| 214 |
-
gr.Markdown("""
|
| 215 |
-
# 🏆 Competitive Programming Assistant
|
| 216 |
-
|
| 217 |
-
Upload a problem statement from Codeforces, LeetCode, or any competitive programming platform to get:
|
| 218 |
-
- Step-by-step analysis
|
| 219 |
-
- Optimal solution approach
|
| 220 |
-
- Complete code implementation
|
| 221 |
-
- Time and space complexity analysis
|
| 222 |
-
|
| 223 |
-
Powered by the OlympicCoder model.
|
| 224 |
-
""")
|
| 225 |
-
theme_btn = gr.Button("🌙 Dark Mode", elem_id="theme-toggle-btn")
|
| 226 |
|
| 227 |
with gr.Row():
|
| 228 |
-
with gr.Column(scale=2
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
elem_id="api_key_input"
|
| 235 |
-
)
|
| 236 |
-
|
| 237 |
-
remember_api_key = gr.Checkbox(
|
| 238 |
-
label="Remember my API key",
|
| 239 |
-
elem_id="remember_api_key"
|
| 240 |
-
)
|
| 241 |
|
| 242 |
problem_input = gr.Textbox(
|
| 243 |
placeholder="Paste your competitive programming problem statement here...",
|
|
@@ -251,12 +131,11 @@ with gr.Blocks(title="Competitive Programming Assistant", theme=light_theme, css
|
|
| 251 |
label="Programming Language"
|
| 252 |
)
|
| 253 |
|
| 254 |
-
submit_btn = gr.Button("Generate Solution", variant="primary"
|
| 255 |
|
| 256 |
-
with gr.Column(scale=3
|
| 257 |
solution_output = gr.Markdown(
|
| 258 |
-
label="Generated Solution"
|
| 259 |
-
elem_id="solution-output"
|
| 260 |
)
|
| 261 |
|
| 262 |
with gr.Accordion("About", open=False):
|
|
@@ -284,80 +163,12 @@ with gr.Blocks(title="Competitive Programming Assistant", theme=light_theme, css
|
|
| 284 |
5. **Testing** - Example test cases and verification methods
|
| 285 |
""")
|
| 286 |
|
| 287 |
-
#
|
| 288 |
-
app.load(fn=None, inputs=None, outputs=None, js=get_saved_api_key())
|
| 289 |
-
|
| 290 |
-
# Handle API key saving when checkbox changes
|
| 291 |
-
remember_api_key.change(
|
| 292 |
-
fn=None,
|
| 293 |
-
inputs=[api_key_input, remember_api_key],
|
| 294 |
-
outputs=None,
|
| 295 |
-
js=lambda api_key, remember: save_api_key(api_key, remember)
|
| 296 |
-
)
|
| 297 |
-
|
| 298 |
-
# Handle form submission with streaming
|
| 299 |
submit_btn.click(
|
| 300 |
solve_competitive_problem,
|
| 301 |
-
inputs=[problem_input, language, api_key_input
|
| 302 |
-
outputs=solution_output
|
| 303 |
-
js="""
|
| 304 |
-
function(problem, language, api_key, remember) {
|
| 305 |
-
// Before submission, save API key if requested
|
| 306 |
-
if (remember) {
|
| 307 |
-
localStorage.setItem('openrouter_api_key', api_key);
|
| 308 |
-
}
|
| 309 |
-
|
| 310 |
-
// Clear existing solution
|
| 311 |
-
document.getElementById('solution-output').innerHTML = 'Generating solution...';
|
| 312 |
-
|
| 313 |
-
// Enable syntax highlighting when solution loads
|
| 314 |
-
setTimeout(function() {
|
| 315 |
-
if (typeof Prism !== 'undefined') {
|
| 316 |
-
Prism.highlightAll();
|
| 317 |
-
}
|
| 318 |
-
}, 1000);
|
| 319 |
-
}
|
| 320 |
-
"""
|
| 321 |
)
|
| 322 |
-
|
| 323 |
-
# Add JS for syntax highlighting
|
| 324 |
-
app.load(js="""
|
| 325 |
-
// Load Prism.js for syntax highlighting
|
| 326 |
-
const prismScript = document.createElement('script');
|
| 327 |
-
prismScript.src = 'https://cdnjs.cloudflare.com/ajax/libs/prism/1.25.0/prism.min.js';
|
| 328 |
-
document.head.appendChild(prismScript);
|
| 329 |
-
|
| 330 |
-
const prismCss = document.createElement('link');
|
| 331 |
-
prismCss.rel = 'stylesheet';
|
| 332 |
-
prismCss.href = 'https://cdnjs.cloudflare.com/ajax/libs/prism/1.25.0/themes/prism.min.css';
|
| 333 |
-
document.head.appendChild(prismCss);
|
| 334 |
-
|
| 335 |
-
// Load additional language support
|
| 336 |
-
const languages = ['python', 'cpp', 'java', 'javascript'];
|
| 337 |
-
languages.forEach(lang => {
|
| 338 |
-
const script = document.createElement('script');
|
| 339 |
-
script.src = `https://cdnjs.cloudflare.com/ajax/libs/prism/1.25.0/components/prism-${lang}.min.js`;
|
| 340 |
-
document.head.appendChild(script);
|
| 341 |
-
});
|
| 342 |
-
|
| 343 |
-
// Observe DOM changes to apply syntax highlighting to new content
|
| 344 |
-
const observer = new MutationObserver(mutations => {
|
| 345 |
-
mutations.forEach(mutation => {
|
| 346 |
-
if (mutation.addedNodes.length) {
|
| 347 |
-
setTimeout(() => {
|
| 348 |
-
if (typeof Prism !== 'undefined') {
|
| 349 |
-
Prism.highlightAll();
|
| 350 |
-
}
|
| 351 |
-
}, 100);
|
| 352 |
-
}
|
| 353 |
-
});
|
| 354 |
-
});
|
| 355 |
-
|
| 356 |
-
observer.observe(document.getElementById('solution-output'), {
|
| 357 |
-
childList: true,
|
| 358 |
-
subtree: true
|
| 359 |
-
});
|
| 360 |
-
""")
|
| 361 |
|
| 362 |
# Launch the app
|
| 363 |
if __name__ == "__main__":
|
|
|
|
| 2 |
import os
|
| 3 |
from openai import OpenAI
|
| 4 |
import time
|
|
|
|
| 5 |
|
| 6 |
+
def solve_competitive_problem(problem_statement, language_choice, api_key, progress=gr.Progress()):
|
| 7 |
"""
|
| 8 |
Generate a solution for a competitive programming problem
|
| 9 |
|
|
|
|
| 11 |
problem_statement (str): The problem statement
|
| 12 |
language_choice (str): Programming language for the solution
|
| 13 |
api_key (str): OpenRouter API key
|
|
|
|
| 14 |
progress: Gradio progress tracker
|
| 15 |
|
| 16 |
Returns:
|
| 17 |
str: Step-by-step solution with code
|
| 18 |
"""
|
| 19 |
+
if not api_key.strip():
|
| 20 |
return "Error: Please provide your OpenRouter API key."
|
| 21 |
|
| 22 |
if not problem_statement.strip():
|
|
|
|
| 70 |
|
| 71 |
progress(0.5, "Generating solution...")
|
| 72 |
|
| 73 |
+
# Call the model
|
| 74 |
completion = client.chat.completions.create(
|
| 75 |
extra_headers={
|
| 76 |
"HTTP-Referer": "https://competitive-programming-assistant.app",
|
| 77 |
"X-Title": "Competitive Programming Assistant",
|
| 78 |
},
|
| 79 |
model="open-r1/olympiccoder-7b:free",
|
| 80 |
+
messages=[
|
| 81 |
+
{
|
| 82 |
+
"role": "user",
|
| 83 |
+
"content": prompt
|
| 84 |
+
}
|
| 85 |
+
],
|
| 86 |
temperature=0.7,
|
| 87 |
+
stream=False
|
| 88 |
)
|
| 89 |
|
| 90 |
+
progress(0.9, "Processing response...")
|
| 91 |
+
|
| 92 |
+
solution = completion.choices[0].message.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
progress(1.0, "Complete!")
|
| 95 |
return solution
|
|
|
|
| 97 |
except Exception as e:
|
| 98 |
return f"Error: {str(e)}"
|
| 99 |
|
| 100 |
+
# Create a Gradio interface with Dark Theme
|
| 101 |
+
with gr.Blocks(title="Competitive Programming Assistant", theme=gr.themes.Dark()) as app:
|
| 102 |
+
gr.Markdown("""
|
| 103 |
+
# 🏆 Competitive Programming Assistant
|
| 104 |
+
|
| 105 |
+
Upload a problem statement from Codeforces, LeetCode, or any competitive programming platform to get:
|
| 106 |
+
- Step-by-step analysis
|
| 107 |
+
- Optimal solution approach
|
| 108 |
+
- Complete code implementation
|
| 109 |
+
- Time and space complexity analysis
|
| 110 |
+
|
| 111 |
+
Powered by the OlympicCoder model.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
with gr.Row():
|
| 115 |
+
with gr.Column(scale=2):
|
| 116 |
+
api_key_input = gr.Textbox(
|
| 117 |
+
placeholder="Enter your OpenRouter API key here",
|
| 118 |
+
label="OpenRouter API Key",
|
| 119 |
+
type="password"
|
| 120 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
problem_input = gr.Textbox(
|
| 123 |
placeholder="Paste your competitive programming problem statement here...",
|
|
|
|
| 131 |
label="Programming Language"
|
| 132 |
)
|
| 133 |
|
| 134 |
+
submit_btn = gr.Button("Generate Solution", variant="primary")
|
| 135 |
|
| 136 |
+
with gr.Column(scale=3):
|
| 137 |
solution_output = gr.Markdown(
|
| 138 |
+
label="Generated Solution"
|
|
|
|
| 139 |
)
|
| 140 |
|
| 141 |
with gr.Accordion("About", open=False):
|
|
|
|
| 163 |
5. **Testing** - Example test cases and verification methods
|
| 164 |
""")
|
| 165 |
|
| 166 |
+
# Handle form submission
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
submit_btn.click(
|
| 168 |
solve_competitive_problem,
|
| 169 |
+
inputs=[problem_input, language, api_key_input],
|
| 170 |
+
outputs=solution_output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
# Launch the app
|
| 174 |
if __name__ == "__main__":
|