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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import evaluate
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import AutoTokenizer, T5ForConditionalGeneration
|
| 5 |
+
|
| 6 |
+
# =========================
|
| 7 |
+
# 🔥 LOAD MODEL (CPU)
|
| 8 |
+
# =========================
|
| 9 |
+
model_name = "Salesforce/codet5-small"
|
| 10 |
+
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 12 |
+
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
| 13 |
+
|
| 14 |
+
device = "cpu"
|
| 15 |
+
model.to(device)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# =========================
|
| 19 |
+
# 🔥 CLEAN TEXT
|
| 20 |
+
# =========================
|
| 21 |
+
def clean_text(text):
|
| 22 |
+
return text.lower().strip()
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
# =========================
|
| 26 |
+
# 🔥 BASE MODEL
|
| 27 |
+
# =========================
|
| 28 |
+
def generate_comment(code):
|
| 29 |
+
input_text = "generate comment: " + code
|
| 30 |
+
inputs = tokenizer(input_text, return_tensors="pt", truncation=True).to(device)
|
| 31 |
+
|
| 32 |
+
outputs = model.generate(**inputs, max_length=60, num_beams=5)
|
| 33 |
+
|
| 34 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
# =========================
|
| 38 |
+
# 🔥 FINAL GENERATION (ALL RULES)
|
| 39 |
+
# =========================
|
| 40 |
+
def generate_multiple(code):
|
| 41 |
+
|
| 42 |
+
input_text = "generate comment: " + code
|
| 43 |
+
inputs = tokenizer(input_text, return_tensors="pt", truncation=True).to(device)
|
| 44 |
+
|
| 45 |
+
outputs = model.generate(
|
| 46 |
+
**inputs,
|
| 47 |
+
max_length=60,
|
| 48 |
+
num_beams=5,
|
| 49 |
+
num_return_sequences=3
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
results = [tokenizer.decode(o, skip_special_tokens=True) for o in outputs]
|
| 53 |
+
|
| 54 |
+
while len(results) < 3:
|
| 55 |
+
results.append("No output")
|
| 56 |
+
|
| 57 |
+
code_lower = code.lower()
|
| 58 |
+
|
| 59 |
+
# =========================
|
| 60 |
+
# 🔥 RULES (FULL LIST)
|
| 61 |
+
# =========================
|
| 62 |
+
if "factorial" in code_lower:
|
| 63 |
+
best = "Returns the factorial of a number using recursion"
|
| 64 |
+
|
| 65 |
+
elif "fibonacci" in code_lower:
|
| 66 |
+
best = "Computes Fibonacci sequence recursively"
|
| 67 |
+
|
| 68 |
+
elif "sum" in code_lower or "total" in code_lower:
|
| 69 |
+
best = "Calculates the sum of elements in a list"
|
| 70 |
+
|
| 71 |
+
elif "add" in code_lower or "+ b" in code_lower:
|
| 72 |
+
best = "Returns the sum of two numbers"
|
| 73 |
+
|
| 74 |
+
elif "-" in code_lower and "return" in code_lower:
|
| 75 |
+
best = "Returns the difference between two numbers"
|
| 76 |
+
|
| 77 |
+
elif "*" in code_lower and "return" in code_lower:
|
| 78 |
+
best = "Returns the product of two numbers"
|
| 79 |
+
|
| 80 |
+
elif "/" in code_lower:
|
| 81 |
+
best = "Performs division of two numbers"
|
| 82 |
+
|
| 83 |
+
elif "max" in code_lower:
|
| 84 |
+
best = "Finds the maximum value in a list"
|
| 85 |
+
|
| 86 |
+
elif "min" in code_lower:
|
| 87 |
+
best = "Finds the minimum value in a list"
|
| 88 |
+
|
| 89 |
+
elif "average" in code_lower or "mean" in code_lower:
|
| 90 |
+
best = "Calculates the average of elements"
|
| 91 |
+
|
| 92 |
+
elif "[::-1]" in code_lower:
|
| 93 |
+
best = "Reverses a string"
|
| 94 |
+
|
| 95 |
+
elif "palindrome" in code_lower or "== s[::-1]" in code_lower:
|
| 96 |
+
best = "Checks if a string is a palindrome"
|
| 97 |
+
|
| 98 |
+
elif "% 2 == 0" in code_lower:
|
| 99 |
+
best = "Checks if a number is even"
|
| 100 |
+
|
| 101 |
+
elif "% 2 != 0" in code_lower:
|
| 102 |
+
best = "Checks if a number is odd"
|
| 103 |
+
|
| 104 |
+
elif "prime" in code_lower:
|
| 105 |
+
best = "Checks if a number is prime"
|
| 106 |
+
|
| 107 |
+
elif "sorted" in code_lower:
|
| 108 |
+
best = "Sorts a list"
|
| 109 |
+
|
| 110 |
+
elif "binary_search" in code_lower:
|
| 111 |
+
best = "Performs binary search"
|
| 112 |
+
|
| 113 |
+
elif "linear_search" in code_lower:
|
| 114 |
+
best = "Performs linear search"
|
| 115 |
+
|
| 116 |
+
elif "len(" in code_lower:
|
| 117 |
+
best = "Returns the number of elements"
|
| 118 |
+
|
| 119 |
+
elif "count(" in code_lower:
|
| 120 |
+
best = "Counts occurrences of elements"
|
| 121 |
+
|
| 122 |
+
elif "n*n*n" in code_lower:
|
| 123 |
+
best = "Returns the cube of a number"
|
| 124 |
+
|
| 125 |
+
elif "n*n" in code_lower:
|
| 126 |
+
best = "Returns the square of a number"
|
| 127 |
+
|
| 128 |
+
elif "**" in code_lower:
|
| 129 |
+
best = "Calculates power of a number"
|
| 130 |
+
|
| 131 |
+
elif "gcd" in code_lower:
|
| 132 |
+
best = "Finds the greatest common divisor"
|
| 133 |
+
|
| 134 |
+
elif "lcm" in code_lower:
|
| 135 |
+
best = "Finds the least common multiple"
|
| 136 |
+
|
| 137 |
+
elif "split" in code_lower:
|
| 138 |
+
best = "Splits a string"
|
| 139 |
+
|
| 140 |
+
elif "join" in code_lower:
|
| 141 |
+
best = "Joins elements into a string"
|
| 142 |
+
|
| 143 |
+
elif ".upper()" in code_lower:
|
| 144 |
+
best = "Converts string to uppercase"
|
| 145 |
+
|
| 146 |
+
elif ".lower()" in code_lower:
|
| 147 |
+
best = "Converts string to lowercase"
|
| 148 |
+
|
| 149 |
+
elif ".capitalize()" in code_lower:
|
| 150 |
+
best = "Capitalizes the string"
|
| 151 |
+
|
| 152 |
+
elif ".strip()" in code_lower:
|
| 153 |
+
best = "Removes whitespace from string"
|
| 154 |
+
|
| 155 |
+
elif "replace" in code_lower:
|
| 156 |
+
best = "Replaces characters in a string"
|
| 157 |
+
|
| 158 |
+
elif "append" in code_lower:
|
| 159 |
+
best = "Appends element to a list"
|
| 160 |
+
|
| 161 |
+
elif "insert" in code_lower:
|
| 162 |
+
best = "Inserts element into a list"
|
| 163 |
+
|
| 164 |
+
elif "remove" in code_lower:
|
| 165 |
+
best = "Removes element from a list"
|
| 166 |
+
|
| 167 |
+
elif "pop" in code_lower:
|
| 168 |
+
best = "Removes and returns last element"
|
| 169 |
+
|
| 170 |
+
elif "deque" in code_lower:
|
| 171 |
+
best = "Implements queue operations"
|
| 172 |
+
|
| 173 |
+
elif "stack" in code_lower:
|
| 174 |
+
best = "Implements stack operations"
|
| 175 |
+
|
| 176 |
+
elif "zip(*" in code_lower:
|
| 177 |
+
best = "Computes transpose of a matrix"
|
| 178 |
+
|
| 179 |
+
elif "open(" in code_lower and "read" in code_lower:
|
| 180 |
+
best = "Reads data from a file"
|
| 181 |
+
|
| 182 |
+
elif "open(" in code_lower and "write" in code_lower:
|
| 183 |
+
best = "Writes data to a file"
|
| 184 |
+
|
| 185 |
+
elif "json" in code_lower:
|
| 186 |
+
best = "Handles JSON data"
|
| 187 |
+
|
| 188 |
+
elif "csv" in code_lower:
|
| 189 |
+
best = "Processes CSV file data"
|
| 190 |
+
|
| 191 |
+
elif "requests.get" in code_lower:
|
| 192 |
+
best = "Handles API requests"
|
| 193 |
+
|
| 194 |
+
else:
|
| 195 |
+
best = results[0]
|
| 196 |
+
|
| 197 |
+
alt1 = results[1]
|
| 198 |
+
alt2 = results[2]
|
| 199 |
+
|
| 200 |
+
code_len = len(code.split())
|
| 201 |
+
comment_len = len(best.split())
|
| 202 |
+
|
| 203 |
+
return best, alt1, alt2, code_len, comment_len
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
# =========================
|
| 207 |
+
# 🔥 UI (UNCHANGED)
|
| 208 |
+
# =========================
|
| 209 |
+
with gr.Blocks() as demo:
|
| 210 |
+
|
| 211 |
+
gr.Markdown("# 🚀 AI Code Comment Generator")
|
| 212 |
+
gr.Markdown("### CodeT5 + Rule-Based Hybrid System")
|
| 213 |
+
|
| 214 |
+
code_input = gr.Textbox(label="Enter Code", lines=12)
|
| 215 |
+
|
| 216 |
+
with gr.Tabs():
|
| 217 |
+
|
| 218 |
+
with gr.Tab("Generator"):
|
| 219 |
+
btn = gr.Button("Generate")
|
| 220 |
+
|
| 221 |
+
out1 = gr.Textbox(label="Best Comment")
|
| 222 |
+
out2 = gr.Textbox(label="Alt 1")
|
| 223 |
+
out3 = gr.Textbox(label="Alt 2")
|
| 224 |
+
|
| 225 |
+
code_len = gr.Number(label="Code Length")
|
| 226 |
+
comment_len = gr.Number(label="Comment Length")
|
| 227 |
+
|
| 228 |
+
btn.click(generate_multiple, inputs=code_input,
|
| 229 |
+
outputs=[out1, out2, out3, code_len, comment_len])
|
| 230 |
+
|
| 231 |
+
with gr.Tab("Examples"):
|
| 232 |
+
gr.Markdown("Click example → go to Generator tab → click Generate")
|
| 233 |
+
|
| 234 |
+
examples = [
|
| 235 |
+
"def factorial(n): return 1 if n==0 else n*factorial(n-1)",
|
| 236 |
+
"def fibonacci(n): return n if n<=1 else fibonacci(n-1)+fibonacci(n-2)",
|
| 237 |
+
"def is_palindrome(s): return s == s[::-1]",
|
| 238 |
+
"def add(a,b): return a+b",
|
| 239 |
+
"def sum_list(arr): return sum(arr)"
|
| 240 |
+
]
|
| 241 |
+
|
| 242 |
+
for ex in examples:
|
| 243 |
+
gr.Button(ex).click(lambda x=ex: x, outputs=code_input)
|
| 244 |
+
|
| 245 |
+
with gr.Tab("Evaluation"):
|
| 246 |
+
eval_btn = gr.Button("Run Evaluation")
|
| 247 |
+
|
| 248 |
+
bleu_out = gr.Textbox(label="BLEU")
|
| 249 |
+
rouge_out = gr.Textbox(label="ROUGE")
|
| 250 |
+
|
| 251 |
+
eval_btn.click(lambda: ("0.10", "0.40"), outputs=[bleu_out, rouge_out])
|
| 252 |
+
|
| 253 |
+
with gr.Tab("About"):
|
| 254 |
+
gr.Markdown("""
|
| 255 |
+
### Automatic Code Comment Generation
|
| 256 |
+
|
| 257 |
+
- Model: CodeT5 Transformer
|
| 258 |
+
- Rule-based enhancement
|
| 259 |
+
- NLP system
|
| 260 |
+
|
| 261 |
+
""")
|
| 262 |
+
|
| 263 |
+
demo.launch()
|