typesdigital commited on
Commit
adf37db
1 Parent(s): c393b41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -22
app.py CHANGED
@@ -1,41 +1,47 @@
 
 
1
  import openai
2
  import secrets
3
 
4
- print(secrets.OPENAI_API_KEY)
5
-
6
  # Set up the OpenAI API credentials
7
  openai.api_key = secrets.OPENAI_API_KEY
8
 
9
- # Define a function that takes a user's input code as a prompt and uses the OpenAI API to generate a corrected version of the code
10
- def correct_code(prompt):
11
- # Set up the OpenAI API parameters
12
- model_engine = "text-davinci-002"
13
- temperature = 0.7
14
- max_tokens = 100
15
 
16
- # Generate the corrected code using the OpenAI API
 
 
17
  response = openai.Completion.create(
18
- engine=model_engine,
19
  prompt=prompt,
20
- temperature=temperature,
21
- max_tokens=max_tokens
 
 
22
  )
23
 
24
  # Extract the corrected code from the API response
25
  corrected_code = response.choices[0].text.strip()
26
 
 
 
 
 
 
27
  return corrected_code
28
 
29
- def main():
30
- while True:
31
- # Prompt the user for input code
32
- prompt = input("Enter code to correct: ")
33
 
34
- # Generate a corrected version of the code using the OpenAI API
35
- corrected_code = correct_code(prompt)
 
36
 
37
- # Print the corrected code
38
- print(corrected_code)
39
 
40
- if __name__ == '__main__':
41
- main()
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
3
  import openai
4
  import secrets
5
 
 
 
6
  # Set up the OpenAI API credentials
7
  openai.api_key = secrets.OPENAI_API_KEY
8
 
9
+ # Load the Hugging Face model and tokenizer
10
+ model_name = "Helsinki-NLP/opus-mt-python-en"
11
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
12
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
 
 
13
 
14
+ # Define a function that takes a user's input code as a prompt and uses the OpenAI API and Hugging Face model to generate a corrected version of the code
15
+ def correct_code(prompt):
16
+ # Use the OpenAI API to generate suggestions for fixing syntax errors in the code
17
  response = openai.Completion.create(
18
+ engine="davinci-codex",
19
  prompt=prompt,
20
+ max_tokens=1024,
21
+ n=1,
22
+ stop=None,
23
+ temperature=0.5,
24
  )
25
 
26
  # Extract the corrected code from the API response
27
  corrected_code = response.choices[0].text.strip()
28
 
29
+ # Use the Hugging Face model to generate a more natural-sounding version of the corrected code
30
+ input_ids = tokenizer.encode(corrected_code, return_tensors="pt")
31
+ outputs = model.generate(input_ids)
32
+ corrected_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
33
+
34
  return corrected_code
35
 
36
+ # Define a Gradio interface for the code assistant
37
+ input_text = gr.inputs.Textbox(lines=10, label="Input Code")
38
+ output_text = gr.outputs.Textbox(label="Corrected Code")
 
39
 
40
+ def generate_code(input_text):
41
+ corrected_code = correct_code(input_text)
42
+ return corrected_code
43
 
44
+ interface = gr.Interface(fn=generate_code, inputs=input_text, outputs=output_text, title="AI Code Assistant", description="Enter your code and click submit to generate a corrected version.")
 
45
 
46
+ # Run the Gradio interface
47
+ interface.launch()