Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
|
| 4 |
+
def generate_solution(api_key, problem_statement):
|
| 5 |
+
try:
|
| 6 |
+
client = OpenAI(
|
| 7 |
+
base_url="https://openrouter.ai/api/v1",
|
| 8 |
+
api_key=api_key.strip(),
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
completion = client.chat.completions.create(
|
| 12 |
+
model="open-r1/olympiccoder-7b:free",
|
| 13 |
+
messages=[
|
| 14 |
+
{
|
| 15 |
+
"role": "system",
|
| 16 |
+
"content": "You are a competitive programming expert. Provide a correct solution with clear reasoning. First output the code, then explain the approach."
|
| 17 |
+
},
|
| 18 |
+
{
|
| 19 |
+
"role": "user",
|
| 20 |
+
"content": f"Solve this problem:\n{problem_statement}"
|
| 21 |
+
}
|
| 22 |
+
],
|
| 23 |
+
temperature=0.3,
|
| 24 |
+
max_tokens=2048
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
response = completion.choices[0].message.content
|
| 28 |
+
return format_response(response)
|
| 29 |
+
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return f"Error: {str(e)}"
|
| 32 |
+
|
| 33 |
+
def format_response(response):
|
| 34 |
+
# Split code and explanation
|
| 35 |
+
formatted = []
|
| 36 |
+
in_code = False
|
| 37 |
+
code_buffer = []
|
| 38 |
+
explanation_buffer = []
|
| 39 |
+
|
| 40 |
+
for line in response.split('\n'):
|
| 41 |
+
if line.strip().startswith('```'):
|
| 42 |
+
in_code = not in_code
|
| 43 |
+
if not in_code:
|
| 44 |
+
formatted.append(f"```python\n{'\n'.join(code_buffer[1:])}\n```")
|
| 45 |
+
code_buffer = []
|
| 46 |
+
continue
|
| 47 |
+
|
| 48 |
+
if in_code:
|
| 49 |
+
code_buffer.append(line)
|
| 50 |
+
else:
|
| 51 |
+
explanation_buffer.append(line)
|
| 52 |
+
|
| 53 |
+
if explanation_buffer:
|
| 54 |
+
formatted.append("### Explanation\n" + '\n'.join(explanation_buffer))
|
| 55 |
+
|
| 56 |
+
return "\n\n".join(formatted)
|
| 57 |
+
|
| 58 |
+
with gr.Blocks(title="Competitive Programming Assistant") as app:
|
| 59 |
+
gr.Markdown("# 🏆 Competitive Programming Assistant")
|
| 60 |
+
gr.Markdown("Powered by OlympicCoder-7B via OpenRouter AI")
|
| 61 |
+
|
| 62 |
+
with gr.Row():
|
| 63 |
+
api_key = gr.Textbox(
|
| 64 |
+
label="OpenRouter API Key",
|
| 65 |
+
type="password",
|
| 66 |
+
placeholder="Enter your API key here..."
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
with gr.Row():
|
| 70 |
+
problem_input = gr.Textbox(
|
| 71 |
+
label="Problem Statement",
|
| 72 |
+
lines=5,
|
| 73 |
+
placeholder="Paste your programming problem here..."
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
submit_btn = gr.Button("Generate Solution", variant="primary")
|
| 77 |
+
|
| 78 |
+
output = gr.Markdown()
|
| 79 |
+
|
| 80 |
+
submit_btn.click(
|
| 81 |
+
fn=generate_solution,
|
| 82 |
+
inputs=[api_key, problem_input],
|
| 83 |
+
outputs=output
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
gr.Examples(
|
| 87 |
+
examples=[
|
| 88 |
+
[
|
| 89 |
+
"Given an array of integers, find two numbers such that they add up to a specific target number."
|
| 90 |
+
],
|
| 91 |
+
[
|
| 92 |
+
"Implement a function to calculate the minimum number of operations required to transform one string into another using only insertion, deletion, and substitution."
|
| 93 |
+
]
|
| 94 |
+
],
|
| 95 |
+
inputs=[problem_input]
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
if __name__ == "__main__":
|
| 99 |
+
app.launch(server_port=7860, share=False)
|