Spaces:
Runtime error
Runtime error
Commit
•
a36c7d4
1
Parent(s):
d74ead2
Update app.py
Browse files
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 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
|
|
|
|
|
15 |
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|