Spaces:
Sleeping
Sleeping
added app.py
Browse files
app.py
ADDED
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
# Load model and tokenizer
|
6 |
+
model = AutoModelForCausalLM.from_pretrained(
|
7 |
+
"Neo111x/Falcon3-3B-Instruct-RL-CODE-FIX",
|
8 |
+
trust_remote_code=True
|
9 |
+
)
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
11 |
+
"Neo111x/Falcon3-3B-Instruct-RL-CODE-FIX",
|
12 |
+
trust_remote_code=True
|
13 |
+
)
|
14 |
+
model.eval()
|
15 |
+
|
16 |
+
# Inference function
|
17 |
+
def repair_code(faulty_code):
|
18 |
+
PROGRAM_REPAIR_TEMPLATE = f"""
|
19 |
+
You are an expert in the field of software testing.
|
20 |
+
You are given a buggy Python program, you are supposed to first generate testcases that can expose the bug,
|
21 |
+
and then generate the corresponding fixed code. The two tasks are detailed as follows.
|
22 |
+
|
23 |
+
1. **Generate a comprehensive set of test cases to expose the bug**:
|
24 |
+
- Each test case should include an input and the expected output.
|
25 |
+
- Output the test cases as a JSON list, where each entry is a dictionary with keys "test_input" and "test_output".
|
26 |
+
- Write in
|
27 |
+
|
28 |
+
json
|
29 |
+
|
30 |
+
block.
|
31 |
+
|
32 |
+
2. **Provide a fixed version**:
|
33 |
+
- Write a correct Python program to fix the bug.
|
34 |
+
- Write in
|
35 |
+
|
36 |
+
python
|
37 |
+
|
38 |
+
block.
|
39 |
+
- The code should read from standard input and write to standard output, matching the input/output format specified in the problem.
|
40 |
+
|
41 |
+
Here is an example.
|
42 |
+
The faulty Python program is:
|
43 |
+
|
44 |
+
python
|
45 |
+
\"\"\"Please write a Python program to sum two integer inputs\"\"\"
|
46 |
+
def add (x, y):
|
47 |
+
return x - y
|
48 |
+
x = int(input())
|
49 |
+
y = int(input())
|
50 |
+
print(add(x,y))
|
51 |
+
|
52 |
+
|
53 |
+
|
54 |
+
Testcases that can expose the bug:
|
55 |
+
|
56 |
+
json
|
57 |
+
[
|
58 |
+
{{
|
59 |
+
\"test_input\":\"1\n2\",
|
60 |
+
\"test_output\":\"3\"
|
61 |
+
}},
|
62 |
+
{{
|
63 |
+
\"test_input\":\"-1\n1\",
|
64 |
+
\"test_output\":\"0\"
|
65 |
+
}},
|
66 |
+
{{
|
67 |
+
\"test_input\":\"-1\n2\",
|
68 |
+
\"test_output\":\"1\"
|
69 |
+
}}
|
70 |
+
]
|
71 |
+
|
72 |
+
|
73 |
+
|
74 |
+
Fixed code:
|
75 |
+
|
76 |
+
python
|
77 |
+
def add (x, y):
|
78 |
+
return x + y
|
79 |
+
x = int(input())
|
80 |
+
y = int(input())
|
81 |
+
print(add(x,y))
|
82 |
+
|
83 |
+
|
84 |
+
|
85 |
+
Now, you are given a faulty Python function, please return:
|
86 |
+
1. **Testcases** that helps expose the bug.
|
87 |
+
2. **Fixed code** that can pass all testcases.
|
88 |
+
|
89 |
+
The faulty function is:
|
90 |
+
|
91 |
+
python
|
92 |
+
{faulty_code}
|
93 |
+
|
94 |
+
|
95 |
+
<|assistant|>
|
96 |
+
"""
|
97 |
+
messages = [
|
98 |
+
{
|
99 |
+
"role": "user",
|
100 |
+
"content": PROGRAM_REPAIR_TEMPLATE
|
101 |
+
}
|
102 |
+
]
|
103 |
+
|
104 |
+
text = tokenizer.apply_chat_template(messages, tokenize=False)
|
105 |
+
inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
106 |
+
|
107 |
+
with torch.no_grad():
|
108 |
+
outputs = model.generate(
|
109 |
+
**inputs,
|
110 |
+
max_new_tokens=512,
|
111 |
+
do_sample=False
|
112 |
+
)
|
113 |
+
|
114 |
+
generated_ids = [
|
115 |
+
output_ids[len(input_ids):] for input_ids, output_ids in zip(inputs["input_ids"], outputs)
|
116 |
+
]
|
117 |
+
result = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
118 |
+
return result.strip()
|
119 |
+
|
120 |
+
# Gradio UI
|
121 |
+
gr.Interface(
|
122 |
+
fn=repair_code,
|
123 |
+
inputs=gr.Textbox(label="Faulty Python Function", lines=15, placeholder="Paste your buggy function here..."),
|
124 |
+
outputs=gr.Textbox(label="Generated Test Cases and Fixed Code", lines=30),
|
125 |
+
title="🧠 AI Program Repair - Falcon3 3B GRPO",
|
126 |
+
description="Paste a buggy Python function. The model will generate test cases and a fixed version of the code."
|
127 |
+
).launch()
|