Spaces:
Runtime error
Runtime error
main app.py
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoTokenizer, AutoModelWithLMHead, SummarizationPipeline
|
2 |
+
import gradio as gr
|
3 |
+
import tokenize
|
4 |
+
import io
|
5 |
+
|
6 |
+
pipeline = SummarizationPipeline(
|
7 |
+
model=AutoModelWithLMHead.from_pretrained("SEBIS/code_trans_t5_large_source_code_summarization_python_multitask_finetune"),
|
8 |
+
tokenizer=AutoTokenizer.from_pretrained("SEBIS/code_trans_t5_large_source_code_summarization_python_multitask_finetune", skip_special_tokens=True),
|
9 |
+
device=0)
|
10 |
+
|
11 |
+
def code_summarizer(code):
|
12 |
+
def code_tokenizer(line):
|
13 |
+
result= []
|
14 |
+
line = io.StringIO(line)
|
15 |
+
|
16 |
+
for toktype, tok, start, end, line in tokenize.generate_tokens(line.readline):
|
17 |
+
if (not toktype == tokenize.COMMENT):
|
18 |
+
if toktype == tokenize.STRING:
|
19 |
+
result.append("CODE_STRING")
|
20 |
+
elif toktype == tokenize.NUMBER:
|
21 |
+
result.append("CODE_INTEGER")
|
22 |
+
elif (not tok=="\n") and (not tok==" "):
|
23 |
+
result.append(str(tok))
|
24 |
+
return ' '.join(result)
|
25 |
+
|
26 |
+
tokenized_code = code_tokenizer(code)
|
27 |
+
summary = pipeline(tokenized_code)
|
28 |
+
return summary[0]['summary_text']
|
29 |
+
|
30 |
+
def call_examples():
|
31 |
+
examples = ['''def findAverage(list): sum = 0. for x in list: sum = sum + x average = sum / len(list) return average''',
|
32 |
+
'''def findMax(list): max = list[0] for x in list: if x > max: max = x return max''',
|
33 |
+
'''def findMin(list): min = list[0] for x in list: if x < min: min = x return min''']
|
34 |
+
return examples
|
35 |
+
|
36 |
+
gr.Interface(fn=code_summarizer,
|
37 |
+
inputs=gr.inputs.Textbox(
|
38 |
+
lines=5,
|
39 |
+
default='',
|
40 |
+
placeholder='Insert a Python code here',
|
41 |
+
label='PYTHON CODE'),
|
42 |
+
outputs=gr.outputs.Textbox(
|
43 |
+
type='auto',
|
44 |
+
label='CODE SUMMARY'),
|
45 |
+
title='Code Summarizer From CodeTrans',
|
46 |
+
description='Summarize any Python code',
|
47 |
+
examples=call_examples(),
|
48 |
+
allow_flagging='never').launch(inbrowser=True)
|