Spaces:
Runtime error
Runtime error
Reshinth Adithyan
commited on
Commit
·
a576b74
1
Parent(s):
1f164ae
Initial app
Browse files- README.md +2 -1
- app.py +32 -4
- flagged/log.csv +3 -0
- src/__pycache__/test_utils.cpython-39.pyc +0 -0
- src/__pycache__/utils.cpython-39.pyc +0 -0
- src/test_utils.py +12 -0
- src/utils.py +26 -0
README.md
CHANGED
@@ -13,7 +13,8 @@ Check out the configuration reference at https://huggingface.co/docs/hub/spaces#
|
|
13 |
|
14 |
|
15 |
### Tasks that can be done
|
16 |
-
- Code Generation (Natural Language Query | Code Completion)
|
17 |
- Code Summarization (Summarize in Natural Language)
|
18 |
- Code Search (from NL)
|
19 |
- Similarity Check (Duplicate Detection)
|
|
|
|
13 |
|
14 |
|
15 |
### Tasks that can be done
|
16 |
+
- Code Generation (Natural Language Query | Code Completion) -> https://huggingface.co/lvwerra/codeparrot-small
|
17 |
- Code Summarization (Summarize in Natural Language)
|
18 |
- Code Search (from NL)
|
19 |
- Similarity Check (Duplicate Detection)
|
20 |
+
- Vulnerability Detection
|
app.py
CHANGED
@@ -1,7 +1,35 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from src.utils import return_parse_tree, GenCode, Config
|
3 |
+
import logging
|
4 |
|
5 |
+
logging.basicConfig(level=logging.INFO)
|
6 |
+
logger = logging.getLogger(__name__)
|
7 |
|
8 |
+
|
9 |
+
|
10 |
+
logger.info("Loading classes into the memory....")
|
11 |
+
code_gen_class = GenCode(Config.code_gen_idt, gen_kwargs=Config.code_gen_config)
|
12 |
+
logger.info("Sucessfully loaded classes into the memory")
|
13 |
+
|
14 |
+
def code_gen(code_snippet:str) -> str:
|
15 |
+
logger.info("Starting to generate code...")
|
16 |
+
generated_code = code_gen_class(code_snippet)
|
17 |
+
logger.info("Sucessfully generated code")
|
18 |
+
|
19 |
+
return generated_code
|
20 |
+
|
21 |
+
def code_edit(code_snippet:str = "def dataset_name():\n return 'test'"):
|
22 |
+
return code_snippet + "\n" + return_parse_tree(code_snippet)
|
23 |
+
|
24 |
+
demo = gr.Blocks(analytics_enabled=True)
|
25 |
+
with demo:
|
26 |
+
gr.Markdown("""# Code Analysis \n ## Code Editor""")
|
27 |
+
with gr.Row():
|
28 |
+
input_editor = gr.Textbox(label="Code Editor",lines=10,interactive=True,placeholder="Use this as the main code editor ")
|
29 |
+
button_gen = gr.Button(value="Complete Code")
|
30 |
+
|
31 |
+
button_gen.click(code_gen,input_editor,input_editor)
|
32 |
+
# iface = gr.Interface(title="Code Editor",fn=code_edit, inputs="text", outputs="text",examples=["def dataset_name():\n return 'test'"])
|
33 |
+
# iface.launch()
|
34 |
+
|
35 |
+
demo.launch(debug=True, server_port=8080)
|
flagged/log.csv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
'code_snippet','output','flag','username','timestamp'
|
2 |
+
'','','','','2022-05-27 13:34:35.072204'
|
3 |
+
'','','','','2022-05-27 13:34:35.721740'
|
src/__pycache__/test_utils.cpython-39.pyc
ADDED
Binary file (569 Bytes). View file
|
|
src/__pycache__/utils.cpython-39.pyc
ADDED
Binary file (1.52 kB). View file
|
|
src/test_utils.py
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from src.utils import GenCode, Config
|
2 |
+
|
3 |
+
code_gen_class = GenCode(Config.code_gen_idt, gen_kwargs={})
|
4 |
+
|
5 |
+
def test_gen_code():
|
6 |
+
code_snippet = "def dataset_name():\n return 'test'"
|
7 |
+
generated_code = code_gen_class(code_snippet)
|
8 |
+
print(generated_code)
|
9 |
+
|
10 |
+
|
11 |
+
if __name__ == "__main__":
|
12 |
+
test_gen_code()
|
src/utils.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import ast
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
3 |
+
from dataclasses import dataclass
|
4 |
+
|
5 |
+
# Configs
|
6 |
+
class Config:
|
7 |
+
code_gen_idt : str = "lvwerra/codeparrot-small"
|
8 |
+
code_gen_config : dict = {
|
9 |
+
"max_length" : 256
|
10 |
+
}
|
11 |
+
|
12 |
+
class GenCode:
|
13 |
+
def __init__(self,model_idt:str,gen_kwargs:dict) -> None:
|
14 |
+
self.generation_pipe = pipeline("text-generation", model=model_idt)
|
15 |
+
self.gen_kwargs = gen_kwargs
|
16 |
+
def __call__(self, input : str):
|
17 |
+
return self.generation_pipe(input, **self.gen_kwargs)[0]["generated_text"]
|
18 |
+
|
19 |
+
|
20 |
+
|
21 |
+
def return_lexer_map(code_snippet:str):
|
22 |
+
return None
|
23 |
+
|
24 |
+
|
25 |
+
def return_parse_tree(code_snippet:str):
|
26 |
+
return ast.dump(ast.parse(code_snippet))
|