Update app.py
Browse files
app.py
CHANGED
@@ -2,12 +2,15 @@ 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=[
|
@@ -23,7 +26,6 @@ def generate_solution(api_key, problem_statement):
|
|
23 |
temperature=0.3,
|
24 |
max_tokens=2048
|
25 |
)
|
26 |
-
|
27 |
response = completion.choices[0].message.content
|
28 |
return format_response(response)
|
29 |
|
@@ -39,8 +41,11 @@ def format_response(response):
|
|
39 |
for line in response.split('\n'):
|
40 |
if line.strip().startswith('```'):
|
41 |
if in_code and code_buffer:
|
42 |
-
#
|
43 |
-
|
|
|
|
|
|
|
44 |
formatted.append(f"```python\n{code_content}\n```")
|
45 |
code_buffer = []
|
46 |
in_code = not in_code
|
@@ -51,6 +56,14 @@ def format_response(response):
|
|
51 |
else:
|
52 |
explanation_buffer.append(line)
|
53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
if explanation_buffer:
|
55 |
formatted.append("### Explanation\n" + '\n'.join(explanation_buffer))
|
56 |
|
@@ -64,7 +77,8 @@ with gr.Blocks(title="Competitive Programming Assistant") as app:
|
|
64 |
api_key = gr.Textbox(
|
65 |
label="OpenRouter API Key",
|
66 |
type="password",
|
67 |
-
placeholder="Enter your API key here..."
|
|
|
68 |
)
|
69 |
|
70 |
with gr.Row():
|
@@ -76,14 +90,14 @@ with gr.Blocks(title="Competitive Programming Assistant") as app:
|
|
76 |
|
77 |
submit_btn = gr.Button("Generate Solution", variant="primary")
|
78 |
|
79 |
-
output = gr.Markdown()
|
80 |
|
81 |
submit_btn.click(
|
82 |
fn=generate_solution,
|
83 |
inputs=[api_key, problem_input],
|
84 |
outputs=output
|
85 |
)
|
86 |
-
|
87 |
gr.Examples(
|
88 |
examples=[
|
89 |
[
|
@@ -97,4 +111,4 @@ with gr.Blocks(title="Competitive Programming Assistant") as app:
|
|
97 |
)
|
98 |
|
99 |
if __name__ == "__main__":
|
100 |
-
app.launch(server_port=7860, share=
|
|
|
2 |
from openai import OpenAI
|
3 |
|
4 |
def generate_solution(api_key, problem_statement):
|
5 |
+
# Check if inputs are provided
|
6 |
+
if not api_key or not problem_statement:
|
7 |
+
return "Please provide both an API key and a problem statement."
|
8 |
+
|
9 |
try:
|
10 |
client = OpenAI(
|
11 |
base_url="https://openrouter.ai/api/v1",
|
12 |
api_key=api_key.strip(),
|
13 |
)
|
|
|
14 |
completion = client.chat.completions.create(
|
15 |
model="open-r1/olympiccoder-7b:free",
|
16 |
messages=[
|
|
|
26 |
temperature=0.3,
|
27 |
max_tokens=2048
|
28 |
)
|
|
|
29 |
response = completion.choices[0].message.content
|
30 |
return format_response(response)
|
31 |
|
|
|
41 |
for line in response.split('\n'):
|
42 |
if line.strip().startswith('```'):
|
43 |
if in_code and code_buffer:
|
44 |
+
# Skip language marker if present
|
45 |
+
if len(code_buffer) > 0 and any(code_buffer[0].strip().lower() == lang for lang in ["python", "java", "c++", "cpp"]):
|
46 |
+
code_content = '\n'.join(code_buffer[1:])
|
47 |
+
else:
|
48 |
+
code_content = '\n'.join(code_buffer)
|
49 |
formatted.append(f"```python\n{code_content}\n```")
|
50 |
code_buffer = []
|
51 |
in_code = not in_code
|
|
|
56 |
else:
|
57 |
explanation_buffer.append(line)
|
58 |
|
59 |
+
# Handle case where code block wasn't properly closed
|
60 |
+
if in_code and code_buffer:
|
61 |
+
if len(code_buffer) > 0 and any(code_buffer[0].strip().lower() == lang for lang in ["python", "java", "c++", "cpp"]):
|
62 |
+
code_content = '\n'.join(code_buffer[1:])
|
63 |
+
else:
|
64 |
+
code_content = '\n'.join(code_buffer)
|
65 |
+
formatted.append(f"```python\n{code_content}\n```")
|
66 |
+
|
67 |
if explanation_buffer:
|
68 |
formatted.append("### Explanation\n" + '\n'.join(explanation_buffer))
|
69 |
|
|
|
77 |
api_key = gr.Textbox(
|
78 |
label="OpenRouter API Key",
|
79 |
type="password",
|
80 |
+
placeholder="Enter your API key here...",
|
81 |
+
value="" # Set default value to empty string
|
82 |
)
|
83 |
|
84 |
with gr.Row():
|
|
|
90 |
|
91 |
submit_btn = gr.Button("Generate Solution", variant="primary")
|
92 |
|
93 |
+
output = gr.Markdown(label="Solution")
|
94 |
|
95 |
submit_btn.click(
|
96 |
fn=generate_solution,
|
97 |
inputs=[api_key, problem_input],
|
98 |
outputs=output
|
99 |
)
|
100 |
+
|
101 |
gr.Examples(
|
102 |
examples=[
|
103 |
[
|
|
|
111 |
)
|
112 |
|
113 |
if __name__ == "__main__":
|
114 |
+
app.launch(server_port=7860, share=True) # Changed share to True to make it accessible externally
|