Not-Grim-Refer commited on
Commit
5f2fe16
1 Parent(s): 7d753a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -23
app.py CHANGED
@@ -1,6 +1,6 @@
1
- import torch
2
  import gradio as gr
3
- from transformers import RobertaConfig, RobertaModel
4
 
5
  # Create a configuration object
6
  config = RobertaConfig.from_pretrained('roberta-base')
@@ -8,13 +8,13 @@ config = RobertaConfig.from_pretrained('roberta-base')
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"
13
  model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
14
  tokenizer = AutoTokenizer.from_pretrained(model_name)
15
 
16
- #Define function to analyze input code
17
- def analyze_code(input_code):
18
  # Format code into strings and sentences for NLP
19
  code_str = " ".join(input_code.split())
20
  sentences = [s.strip() for s in code_str.split(".") if s.strip()]
@@ -50,28 +50,28 @@ def suggest_improvements(code):
50
  suggestions = ["Use more descriptive variable names", "Add comments to explain complex logic", "Refactor duplicated code into functions"]
51
  return suggestions
52
 
53
- # Define Gradio interface
54
  interface = gr.Interface(fn=generate_code, inputs=["textbox"], outputs=["textbox"])
55
 
56
- # Have a conversation about the code
57
- input_code = """x = 10
58
- y = 5
59
- def add(a, b):
60
- return a + b
61
  result = add(x, y)"""
62
- code_analysis = analyze_code(input_code)
63
- prompt = generate_prompt(code_analysis)
64
  reply = f"{prompt}\n\n{generate_code(prompt)}\n\nSuggested improvements: {', '.join(suggest_improvements(input_code))}"
65
  print(reply)
66
 
67
  while True:
68
- change = input("Would you like to make any changes to the code? (Y/N) ")
69
- if change == "Y":
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 RobertaConfig, RobertaModel, AutoModelForSeq2SeqLM, AutoTokenizer
4
 
5
  # Create a configuration object
6
  config = RobertaConfig.from_pretrained('roberta-base')
 
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"
13
  model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
14
  tokenizer = AutoTokenizer.from_pretrained(model_name)
15
 
16
+ # Define function to analyze input code
17
+ def analyze_code(input_code):
18
  # Format code into strings and sentences for NLP
19
  code_str = " ".join(input_code.split())
20
  sentences = [s.strip() for s in code_str.split(".") if s.strip()]
 
50
  suggestions = ["Use more descriptive variable names", "Add comments to explain complex logic", "Refactor duplicated code into functions"]
51
  return suggestions
52
 
53
+ # Define Gradio interface
54
  interface = gr.Interface(fn=generate_code, inputs=["textbox"], outputs=["textbox"])
55
 
56
+ # Have a conversation about the code
57
+ input_code = """x = 10
58
+ y = 5
59
+ def add(a, b):
60
+ return a + b
61
  result = add(x, y)"""
62
+ code_analysis = analyze_code(input_code)
63
+ prompt = generate_prompt(code_analysis)
64
  reply = f"{prompt}\n\n{generate_code(prompt)}\n\nSuggested improvements: {', '.join(suggest_improvements(input_code))}"
65
  print(reply)
66
 
67
  while True:
68
+ change = input("Would you like to make any changes to the code? (Y/N) ")
69
+ if change == "Y":
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