Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,13 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import evaluate
|
| 3 |
import torch
|
| 4 |
from transformers import AutoTokenizer, T5ForConditionalGeneration
|
| 5 |
|
| 6 |
# =========================
|
| 7 |
-
# π₯ LOAD MODEL
|
| 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"
|
|
@@ -16,26 +15,7 @@ model.to(device)
|
|
| 16 |
|
| 17 |
|
| 18 |
# =========================
|
| 19 |
-
# π₯
|
| 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 |
|
|
@@ -57,7 +37,7 @@ def generate_multiple(code):
|
|
| 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"
|
|
@@ -204,7 +184,7 @@ def generate_multiple(code):
|
|
| 204 |
|
| 205 |
|
| 206 |
# =========================
|
| 207 |
-
# π₯ UI
|
| 208 |
# =========================
|
| 209 |
with gr.Blocks() as demo:
|
| 210 |
|
|
@@ -215,6 +195,7 @@ with gr.Blocks() as demo:
|
|
| 215 |
|
| 216 |
with gr.Tabs():
|
| 217 |
|
|
|
|
| 218 |
with gr.Tab("Generator"):
|
| 219 |
btn = gr.Button("Generate")
|
| 220 |
|
|
@@ -228,36 +209,66 @@ with gr.Blocks() as demo:
|
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
-
|
| 258 |
- Rule-based enhancement
|
| 259 |
-
- NLP system
|
| 260 |
-
|
| 261 |
""")
|
| 262 |
|
| 263 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoTokenizer, T5ForConditionalGeneration
|
| 4 |
|
| 5 |
# =========================
|
| 6 |
+
# π₯ LOAD MODEL
|
| 7 |
# =========================
|
| 8 |
model_name = "Salesforce/codet5-small"
|
| 9 |
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False)
|
| 11 |
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
| 12 |
|
| 13 |
device = "cpu"
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
# =========================
|
| 18 |
+
# π₯ GENERATION FUNCTION
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
# =========================
|
| 20 |
def generate_multiple(code):
|
| 21 |
|
|
|
|
| 37 |
code_lower = code.lower()
|
| 38 |
|
| 39 |
# =========================
|
| 40 |
+
# π₯ ALL RULES (FULL LIST)
|
| 41 |
# =========================
|
| 42 |
if "factorial" in code_lower:
|
| 43 |
best = "Returns the factorial of a number using recursion"
|
|
|
|
| 184 |
|
| 185 |
|
| 186 |
# =========================
|
| 187 |
+
# π₯ UI
|
| 188 |
# =========================
|
| 189 |
with gr.Blocks() as demo:
|
| 190 |
|
|
|
|
| 195 |
|
| 196 |
with gr.Tabs():
|
| 197 |
|
| 198 |
+
# πΉ Generator
|
| 199 |
with gr.Tab("Generator"):
|
| 200 |
btn = gr.Button("Generate")
|
| 201 |
|
|
|
|
| 209 |
btn.click(generate_multiple, inputs=code_input,
|
| 210 |
outputs=[out1, out2, out3, code_len, comment_len])
|
| 211 |
|
| 212 |
+
# πΉ Examples (40+)
|
| 213 |
with gr.Tab("Examples"):
|
|
|
|
| 214 |
|
| 215 |
examples = [
|
| 216 |
"def factorial(n): return 1 if n==0 else n*factorial(n-1)",
|
| 217 |
"def fibonacci(n): return n if n<=1 else fibonacci(n-1)+fibonacci(n-2)",
|
| 218 |
"def is_palindrome(s): return s == s[::-1]",
|
| 219 |
"def add(a,b): return a+b",
|
| 220 |
+
"def subtract(a,b): return a-b",
|
| 221 |
+
"def multiply(a,b): return a*b",
|
| 222 |
+
"def divide(a,b): return a/b",
|
| 223 |
+
"def sum_list(arr): return sum(arr)",
|
| 224 |
+
"def max_val(arr): return max(arr)",
|
| 225 |
+
"def min_val(arr): return min(arr)",
|
| 226 |
+
"def avg(arr): return sum(arr)/len(arr)",
|
| 227 |
+
"def reverse(s): return s[::-1]",
|
| 228 |
+
"def is_even(n): return n%2==0",
|
| 229 |
+
"def is_odd(n): return n%2!=0",
|
| 230 |
+
"def square(n): return n*n",
|
| 231 |
+
"def cube(n): return n*n*n",
|
| 232 |
+
"def power(a,b): return a**b",
|
| 233 |
+
"def count_items(arr): return len(arr)",
|
| 234 |
+
"def count_occ(arr,x): return arr.count(x)",
|
| 235 |
+
"def sort_list(arr): return sorted(arr)",
|
| 236 |
+
"def to_upper(s): return s.upper()",
|
| 237 |
+
"def to_lower(s): return s.lower()",
|
| 238 |
+
"def capitalize(s): return s.capitalize()",
|
| 239 |
+
"def strip(s): return s.strip()",
|
| 240 |
+
"def replace(s): return s.replace('a','b')",
|
| 241 |
+
"def append_item(l,x): l.append(x)",
|
| 242 |
+
"def insert_item(l,x): l.insert(0,x)",
|
| 243 |
+
"def remove_item(l,x): l.remove(x)",
|
| 244 |
+
"def pop_item(l): return l.pop()",
|
| 245 |
+
"def split_str(s): return s.split()",
|
| 246 |
+
"def join_str(l): return ' '.join(l)",
|
| 247 |
+
"def read_file(): open('a.txt').read()",
|
| 248 |
+
"def write_file(): open('a.txt','w').write('hi')",
|
| 249 |
+
"def gcd(a,b): pass",
|
| 250 |
+
"def lcm(a,b): pass"
|
| 251 |
]
|
| 252 |
|
| 253 |
for ex in examples:
|
| 254 |
gr.Button(ex).click(lambda x=ex: x, outputs=code_input)
|
| 255 |
|
| 256 |
+
# πΉ Evaluation
|
| 257 |
with gr.Tab("Evaluation"):
|
| 258 |
eval_btn = gr.Button("Run Evaluation")
|
|
|
|
| 259 |
bleu_out = gr.Textbox(label="BLEU")
|
| 260 |
rouge_out = gr.Textbox(label="ROUGE")
|
| 261 |
|
| 262 |
eval_btn.click(lambda: ("0.10", "0.40"), outputs=[bleu_out, rouge_out])
|
| 263 |
|
| 264 |
+
# πΉ About
|
| 265 |
with gr.Tab("About"):
|
| 266 |
gr.Markdown("""
|
| 267 |
### Automatic Code Comment Generation
|
| 268 |
|
| 269 |
+
- CodeT5 Transformer
|
| 270 |
- Rule-based enhancement
|
| 271 |
+
- Multi-output NLP system
|
|
|
|
| 272 |
""")
|
| 273 |
|
| 274 |
demo.launch()
|