Not-Grim-Refer commited on
Commit
a36c7d4
1 Parent(s): d74ead2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -38
app.py CHANGED
@@ -1,49 +1,74 @@
1
  import os
2
  os.system("pip install -r requirements.txt")
3
  os.system("pip freeze")
 
 
4
  import gradio as gr
5
- from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed, pipeline
6
 
 
 
 
 
7
 
8
- tokenizer = AutoTokenizer.from_pretrained("codeparrot/codeparrot-small-code-to-text")
9
- model = AutoModelForCausalLM.from_pretrained("codeparrot/codeparrot-small-code-to-text")
10
- pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, num_return_sequences=1, device=-1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
- def make_doctring(gen_prompt):
14
- return gen_prompt + f"\n\n\"\"\"\nExplanation:"
 
 
15
 
 
 
16
 
17
- def code_generation(gen_prompts, max_tokens=8, temperature=0.6, seed=42):
18
- set_seed(seed)
19
- prompts = [make_doctring(p) for p in gen_prompts]
20
- generated_text = pipe(prompts, do_sample=True, top_p=0.95, temperature=temperature, max_length=max_tokens)[0]
21
- return generated_text["generated_text"]
 
 
 
 
 
22
 
23
-
24
- title = "Code Explainer"
25
- description = "This is a space to convert Python code into english text explaining what it does using [codeparrot-small-code-to-text](https://huggingface.co/codeparrot/codeparrot-small-code-to-text),\
26
- a code generation model for Python finetuned on [github-jupyter-code-to-text](https://huggingface.co/datasets/codeparrot/github-jupyter-code-to-text) a dataset of Python code followed by a docstring explaining it, the data was originally extracted from Jupyter notebooks."
27
-
28
- EXAMPLES = [
29
- ["def sort_function(arr):\n n = len(arr)\n \n # Traverse through all array elements\n for i in range(n):\n \n # Last i elements are already in place\n for j in range(0, n-i-1):\n \n # traverse the array from 0 to n-i-1\n # Swap if the element found is greater\n # than the next element\n if arr[j] > arr[j+1]:\n arr[j], arr[j+1] = arr[j+1], arr[j]"],
30
- ["from sklearn import model_selection\nX_train, X_test, Y_train, Y_test = model_selection.train_test_split(X, Y, test_size=0.2)"],
31
- ["def load_text(filename):\n with open(filename, 'r') as f:\n text = f.read()\n return text"]
32
- ]
33
-
34
- iface = gr.Interface(
35
- fn=code_generation,
36
- inputs=[
37
- gr.inputs.Textbox(lines=10, placeholder="enter your code here in any programming language...")
38
- gr.inputs.Slider(minimum=8, maximum=256, step=1, default=256, label="Number of tokens to generate"),
39
- gr.inputs.Slider(minimum=0, maximum=2.5, step=0.1, default=0.1, label="Temperature"),
40
- gr.inputs.Slider(minimum=0, maximum=1000, step=1, default=42, label="Random seed")
41
- ],
42
- outputs=gr.outputs.Code(language="text", label="Generated explanation", lines=10),
43
- examples=EXAMPLES,
44
- layout="horizontal",
45
- theme="peach",
46
- description=description,
47
- title=title
48
- )
49
- iface.launch()
 
1
  import os
2
  os.system("pip install -r requirements.txt")
3
  os.system("pip freeze")
4
+ import torch
5
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
6
  import gradio as gr
 
7
 
8
+ # Load pretrained model and tokenizer
9
+ model_name = "salesforce/codet5-base"
10
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
11
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
12
 
13
+ #Define function to analyze input code
14
+ def analyze_code(input_code):
15
+ # Format code into strings and sentences for NLP
16
+ code_str = " ".join(input_code.split())
17
+ sentences = [s.strip() for s in code_str.split(".") if s.strip()]
18
+ #Extract relevant info and intent from code
19
+ variables = []
20
+ functions = []
21
+ logic = []
22
+ for sentence in sentences:
23
+ if "=" in sentence:
24
+ variables.append(sentence.split("=")[0].strip())
25
+ elif "(" in sentence:
26
+ functions.append(sentence.split("(")[0].strip())
27
+ else:
28
+ logic.append(sentence)
29
+ #Return info and intent in dictionary
30
+ return {"variables": variables, "functions": functions, "logic": logic}
31
 
32
+ # Define function to generate prompt from analyzed code
33
+ def generate_prompt(code_analysis):
34
+ prompt = f"Generate code with the following: \n\n"
35
+ prompt += f"Variables: {', '.join(code_analysis['variables'])} \n\n"
36
+ prompt += f"Functions: {', '.join(code_analysis['functions'])} \n\n"
37
+ prompt += f"Logic: {' '.join(code_analysis['logic'])}"
38
+ return prompt
39
+
40
+ # Generate code from model and prompt
41
+ def generate_code(prompt):
42
+ generated_code = model.generate(prompt, max_length=100, num_beams=5, early_stopping=True)
43
+ return generated_code
44
 
45
+ # Suggest improvements to code
46
+ def suggest_improvements(code):
47
+ suggestions = ["Use more descriptive variable names", "Add comments to explain complex logic", "Refactor duplicated code into functions"]
48
+ return suggestions
49
 
50
+ # Define Gradio interface
51
+ interface = gr.Interface(fn=generate_code, inputs=["textbox"], outputs=["textbox"])
52
 
53
+ # Have a conversation about the code
54
+ input_code = """x = 10
55
+ y = 5
56
+ def add(a, b):
57
+ return a + b
58
+ result = add(x, y)"""
59
+ code_analysis = analyze_code(input_code)
60
+ prompt = generate_prompt(code_analysis)
61
+ reply = f"{prompt}\n\n{generate_code(prompt)}\n\nSuggested improvements: {', '.join(suggest_improvements(input_code))}"
62
+ print(reply)
63
 
64
+ while True:
65
+ change = input("Would you like to make any changes to the code? (Y/N) ")
66
+ if change == "Y":
67
+ new_code = input("Enter the updated code: ")
68
+ code_analysis = analyze_code(new_code)
69
+ prompt = generate_prompt(code_analysis)
70
+ reply = f"{prompt}\n\n{generate_code(prompt)}\n\nSuggested improvements: {', '.join(suggest_improvements(new_code))}"
71
+ print(reply)
72
+ elif change == "N":
73
+ print("OK, conversation ended.")
74
+ break