File size: 1,963 Bytes
adf37db
 
ab7440d
3eec426
ab7440d
 
3eec426
ab7440d
adf37db
 
 
 
ab7440d
adf37db
 
 
ab7440d
adf37db
ab7440d
adf37db
 
 
 
ab7440d
 
 
 
 
adf37db
 
 
 
 
ab7440d
 
adf37db
 
 
ab7440d
adf37db
 
 
ab7440d
e5ecad3
 
 
 
 
adf37db
ab7440d
adf37db
 
1
2
3
4
5
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import gradio as gr
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
import openai
import secrets

# Set up the OpenAI API credentials
openai.api_key = secrets.OPENAI_API_KEY

# Load the Hugging Face model and tokenizer
model_name = "Helsinki-NLP/opus-mt-python-en"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)

# 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
def correct_code(prompt):
    # Use the OpenAI API to generate suggestions for fixing syntax errors in the code
    response = openai.Completion.create(
        engine="davinci-codex",
        prompt=prompt,
        max_tokens=1024,
        n=1,
        stop=None,
        temperature=0.5,
    )

    # Extract the corrected code from the API response
    corrected_code = response.choices[0].text.strip()

    # Use the Hugging Face model to generate a more natural-sounding version of the corrected code
    input_ids = tokenizer.encode(corrected_code, return_tensors="pt")
    outputs = model.generate(input_ids)
    corrected_code = tokenizer.decode(outputs[0], skip_special_tokens=True)

    return corrected_code

# Define a Gradio interface for the code assistant
input_text = gr.inputs.Textbox(lines=10, label="Input Code")
output_text = gr.outputs.Textbox(label="Corrected Code")

def generate_code(input_text):
    corrected_code = correct_code(input_text)
    return corrected_code

# Set up the OpenAI API credentials
secrets.OPENAI_API_KEY = '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'
openai.api_key = secrets.OPENAI_API_KEY

# Define the Gradio interface
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.")

# Run the Gradio interface
interface.launch()