Spaces:
Runtime error
Runtime error
Commit
·
adf37db
1
Parent(s):
c393b41
Update app.py
Browse files
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 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
temperature = 0.7
|
| 14 |
-
max_tokens = 100
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
response = openai.Completion.create(
|
| 18 |
-
engine=
|
| 19 |
prompt=prompt,
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
| 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 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
prompt = input("Enter code to correct: ")
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
print(corrected_code)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
|
|
|
| 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()
|