Spaces:
Runtime error
Runtime error
Reshinth Adithyan
commited on
Commit
·
5ef742a
1
Parent(s):
512ffc2
init commit
Browse files- app.py +51 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import difflib
|
| 4 |
+
import re
|
| 5 |
+
|
| 6 |
+
commit_message_per_brush = {
|
| 7 |
+
"Annotate Type": "annotate type to the variables.",
|
| 8 |
+
"Reformat" : "Reformat the code using pep8",
|
| 9 |
+
"Add Docstrings" : "Add docstrings to all the functions",
|
| 10 |
+
"Add Comments" : "Add inline comments to all the functions",
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def load_model_and_tokenizer(model_name:str="CarperAI/diff-codegen-2B-v2"):
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 16 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 17 |
+
return tokenizer, model
|
| 18 |
+
|
| 19 |
+
def make_prompt(code:str,task):
|
| 20 |
+
filename = "input.py"
|
| 21 |
+
prompt = f"<NME>main.py<BEF>{code}<MSG>{commit_message_per_brush[task]}."
|
| 22 |
+
return prompt
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def generate_diff(code:str):
|
| 26 |
+
input_ids = tokenizer.encode(code, return_tensors='pt')
|
| 27 |
+
outputs = model.generate(input_ids, max_length=64,temperature=0.8,top_p=0.85)
|
| 28 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def postprocess_output(generated_output:str):
|
| 32 |
+
pass
|
| 33 |
+
|
| 34 |
+
st.title("Code Brush")
|
| 35 |
+
st.write("A tool to brush up your code")
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
tokenizer,model = load_model_and_tokenizer()
|
| 40 |
+
with st.form("my_form"):
|
| 41 |
+
text = st.text_area("Enter your code here", height=150, value="def greet(input_name):\n return f'Hello, {input_name}'" )
|
| 42 |
+
brush_type = st.selectbox("Brush Type", ["Annotate Type", "Reformat", "Add Docstrings", "Add Comments"])
|
| 43 |
+
submit_button = st.form_submit_button("Submit")
|
| 44 |
+
if submit_button:
|
| 45 |
+
st.write("## Diff:")
|
| 46 |
+
st.text_area(generate_diff(make_prompt(text,brush_type)))
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
transformers
|