Spaces:
Runtime error
Runtime error
Commit
•
4ef486c
1
Parent(s):
5f2fe16
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,6 @@
|
|
1 |
import torch
|
2 |
import gradio as gr
|
3 |
-
from transformers import
|
4 |
-
|
5 |
-
# Create a configuration object
|
6 |
-
config = RobertaConfig.from_pretrained('roberta-base')
|
7 |
-
|
8 |
-
# Create the Roberta model
|
9 |
-
model = RobertaModel.from_pretrained('roberta-base', config=config)
|
10 |
|
11 |
# Load pretrained model and tokenizer
|
12 |
model_name = "zonghaoyang/DistilRoBERTa-base"
|
@@ -15,63 +9,54 @@ tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
15 |
|
16 |
# Define function to analyze input code
|
17 |
def analyze_code(input_code):
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
logic.append(sentence)
|
32 |
-
#Return info and intent in dictionary
|
33 |
-
return {"variables": variables, "functions": functions, "logic": logic}
|
34 |
|
35 |
# Define function to generate prompt from analyzed code
|
36 |
def generate_prompt(code_analysis):
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
# Generate code from model and prompt
|
44 |
def generate_code(prompt):
|
45 |
-
|
46 |
-
|
|
|
|
|
47 |
|
48 |
# Suggest improvements to code
|
49 |
def suggest_improvements(code):
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
#
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
new_code = input("Enter the updated code: ")
|
71 |
-
code_analysis = analyze_code(new_code)
|
72 |
-
prompt = generate_prompt(code_analysis)
|
73 |
-
reply = f"{prompt}\n\n{generate_code(prompt)}\n\nSuggested improvements: {', '.join(suggest_improvements(new_code))}"
|
74 |
-
print(reply)
|
75 |
-
elif change == "N":
|
76 |
-
print("OK, conversation ended.")
|
77 |
-
break
|
|
|
1 |
import torch
|
2 |
import gradio as gr
|
3 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
# Load pretrained model and tokenizer
|
6 |
model_name = "zonghaoyang/DistilRoBERTa-base"
|
|
|
9 |
|
10 |
# Define function to analyze input code
|
11 |
def analyze_code(input_code):
|
12 |
+
code_str = " ".join(input_code.split())
|
13 |
+
sentences = [s.strip() for s in code_str.split(".") if s.strip()]
|
14 |
+
variables = []
|
15 |
+
functions = []
|
16 |
+
logic = []
|
17 |
+
for sentence in sentences:
|
18 |
+
if "=" in sentence:
|
19 |
+
variables.append(sentence.split("=")[0].strip())
|
20 |
+
elif "(" in sentence:
|
21 |
+
functions.append(sentence.split("(")[0].strip())
|
22 |
+
else:
|
23 |
+
logic.append(sentence)
|
24 |
+
return {"variables": variables, "functions": functions, "logic": logic}
|
|
|
|
|
|
|
25 |
|
26 |
# Define function to generate prompt from analyzed code
|
27 |
def generate_prompt(code_analysis):
|
28 |
+
prompt = f"Generate code with the following: \n\n"
|
29 |
+
prompt += f"Variables: {', '.join(code_analysis['variables'])} \n\n"
|
30 |
+
prompt += f"Functions: {', '.join(code_analysis['functions'])} \n\n"
|
31 |
+
prompt += f"Logic: {' '.join(code_analysis['logic'])}"
|
32 |
+
return prompt
|
33 |
+
|
34 |
# Generate code from model and prompt
|
35 |
def generate_code(prompt):
|
36 |
+
input_ids = tokenizer.encode(prompt, return_tensors="pt")
|
37 |
+
generated_ids = model.generate(input_ids, max_length=100, num_beams=5, early_stopping=True)
|
38 |
+
generated_code = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
|
39 |
+
return generated_code
|
40 |
|
41 |
# Suggest improvements to code
|
42 |
def suggest_improvements(code):
|
43 |
+
suggestions = ["Use more descriptive variable names", "Add comments to explain complex logic", "Refactor duplicated code into functions"]
|
44 |
+
return suggestions
|
45 |
+
|
46 |
+
# Main function to integrate the other functions and generate_code
|
47 |
+
def main_function(input_code):
|
48 |
+
code_analysis = analyze_code(input_code)
|
49 |
+
prompt = generate_prompt(code_analysis)
|
50 |
+
generated_code = generate_code(prompt)
|
51 |
+
improvements = suggest_improvements(input_code)
|
52 |
+
return generated_code, improvements
|
53 |
+
|
54 |
+
# Create Gradio interface
|
55 |
+
iface = gr.Interface(
|
56 |
+
fn=main_function,
|
57 |
+
inputs=gr.inputs.Textbox(lines=5, label="Input Code"),
|
58 |
+
outputs=[gr.outputs.Textbox(lines=5, label="Generated Code"), gr.outputs.Textbox(lines=5, label="Suggested Improvements")]
|
59 |
+
)
|
60 |
+
|
61 |
+
# Launch Gradio interface
|
62 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|