Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,22 +18,22 @@ def solve(problem_text):
|
|
| 18 |
if not problem_text or len(problem_text) < 10:
|
| 19 |
return "// Error: Problem text too short."
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
prompt = f"Problem:\n{problem_text}\n\nWrite a complete, robust, step-by-step Python3 algorithm to solve this. DO NOT include any comments, docstrings, or conversational text. Only output the raw code.\nPython code solution:\n"
|
| 24 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 25 |
|
| 26 |
with torch.no_grad():
|
| 27 |
outputs = model.generate(
|
| 28 |
input_ids=inputs["input_ids"],
|
| 29 |
attention_mask=inputs["attention_mask"],
|
| 30 |
-
max_new_tokens=1024,
|
| 31 |
do_sample=False,
|
| 32 |
pad_token_id=tokenizer.eos_token_id
|
| 33 |
)
|
| 34 |
|
| 35 |
full_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 36 |
|
|
|
|
| 37 |
try:
|
| 38 |
code_block = full_text.split("```python")[1]
|
| 39 |
pure_code = code_block.split("```")[0]
|
|
@@ -42,14 +42,24 @@ def solve(problem_text):
|
|
| 42 |
pure_code = full_text.split("Python code solution:\n")[1]
|
| 43 |
else:
|
| 44 |
pure_code = full_text
|
| 45 |
-
|
| 46 |
-
#
|
| 47 |
cleaned_lines = []
|
| 48 |
for line in pure_code.split('\n'):
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
demo = gr.Interface(fn=solve, inputs="text", outputs="text", api_name="predict")
|
| 55 |
demo.launch()
|
|
|
|
| 18 |
if not problem_text or len(problem_text) < 10:
|
| 19 |
return "// Error: Problem text too short."
|
| 20 |
|
| 21 |
+
# THE FIX: Go back to the exact prompt that we know works!
|
| 22 |
+
prompt = f"Problem:\n{problem_text}\n\nPython code solution:\n"
|
|
|
|
| 23 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 24 |
|
| 25 |
with torch.no_grad():
|
| 26 |
outputs = model.generate(
|
| 27 |
input_ids=inputs["input_ids"],
|
| 28 |
attention_mask=inputs["attention_mask"],
|
| 29 |
+
max_new_tokens=1024,
|
| 30 |
do_sample=False,
|
| 31 |
pad_token_id=tokenizer.eos_token_id
|
| 32 |
)
|
| 33 |
|
| 34 |
full_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 35 |
|
| 36 |
+
# 1. Extract the raw Python code block
|
| 37 |
try:
|
| 38 |
code_block = full_text.split("```python")[1]
|
| 39 |
pure_code = code_block.split("```")[0]
|
|
|
|
| 42 |
pure_code = full_text.split("Python code solution:\n")[1]
|
| 43 |
else:
|
| 44 |
pure_code = full_text
|
| 45 |
+
|
| 46 |
+
# 2. THE SCRUBBER: Delete the comments using Python, not the AI
|
| 47 |
cleaned_lines = []
|
| 48 |
for line in pure_code.split('\n'):
|
| 49 |
+
# This splits the line at the '#' symbol and only keeps the code on the left side
|
| 50 |
+
no_comment_line = line.split('#')[0].rstrip()
|
| 51 |
+
|
| 52 |
+
# Only add the line if it's not completely empty
|
| 53 |
+
if no_comment_line.strip():
|
| 54 |
+
cleaned_lines.append(no_comment_line)
|
| 55 |
+
|
| 56 |
+
final_code = "\n".join(cleaned_lines).strip()
|
| 57 |
+
|
| 58 |
+
# Safety net: If the scrubber accidentally deleted everything, return the original
|
| 59 |
+
if not final_code:
|
| 60 |
+
return pure_code.strip()
|
| 61 |
+
|
| 62 |
+
return final_code
|
| 63 |
|
| 64 |
demo = gr.Interface(fn=solve, inputs="text", outputs="text", api_name="predict")
|
| 65 |
demo.launch()
|