Spaces:
Paused
Paused
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -6,29 +6,26 @@ import openai
|
|
| 6 |
# Load OpenAI API key from environment
|
| 7 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 8 |
if not openai_api_key:
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
# Initialise OpenAI client (v1 API)
|
| 12 |
-
client = openai.OpenAI(api_key=openai_api_key)
|
| 13 |
|
| 14 |
def improve_code(user_code, language):
|
| 15 |
"""
|
| 16 |
Sends the user code to OpenAI GPT model and asks for an improved version.
|
| 17 |
"""
|
| 18 |
-
system_prompt =
|
| 19 |
-
f"You are an expert {language} developer. Refactor and improve the following code. "
|
| 20 |
-
"Keep the same functionality but make it cleaner, more efficient and well commented."
|
| 21 |
-
)
|
| 22 |
messages = [
|
| 23 |
{"role": "system", "content": system_prompt},
|
| 24 |
{"role": "user", "content": user_code}
|
| 25 |
]
|
| 26 |
try:
|
| 27 |
-
response =
|
| 28 |
model="gpt-3.5-turbo",
|
| 29 |
messages=messages,
|
| 30 |
temperature=0.2,
|
| 31 |
max_tokens=1024,
|
|
|
|
|
|
|
| 32 |
)
|
| 33 |
improved = response.choices[0].message.content.strip()
|
| 34 |
return improved
|
|
@@ -38,19 +35,11 @@ def improve_code(user_code, language):
|
|
| 38 |
# Build Gradio interface
|
| 39 |
with gr.Blocks() as demo:
|
| 40 |
gr.Markdown("# Code Improver")
|
| 41 |
-
gr.Markdown(
|
| 42 |
-
"Enter your code and select the programming language. The model will return a cleaner version."
|
| 43 |
-
)
|
| 44 |
with gr.Row():
|
| 45 |
-
code_input = gr.Textbox(
|
| 46 |
-
label="Original Code",
|
| 47 |
-
lines=15,
|
| 48 |
-
placeholder="Paste your code here"
|
| 49 |
-
)
|
| 50 |
language_dropdown = gr.Dropdown(
|
| 51 |
-
choices=[
|
| 52 |
-
"Python", "JavaScript", "Java", "C++", "C#", "Go", "Ruby", "PHP"
|
| 53 |
-
],
|
| 54 |
value="Python",
|
| 55 |
label="Language"
|
| 56 |
)
|
|
@@ -64,4 +53,5 @@ with gr.Blocks() as demo:
|
|
| 64 |
)
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|
| 67 |
-
|
|
|
|
|
|
| 6 |
# Load OpenAI API key from environment
|
| 7 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 8 |
if not openai_api_key:
|
| 9 |
+
openai_api_key = ""
|
| 10 |
+
openai.api_key = openai_api_key
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def improve_code(user_code, language):
|
| 13 |
"""
|
| 14 |
Sends the user code to OpenAI GPT model and asks for an improved version.
|
| 15 |
"""
|
| 16 |
+
system_prompt = f"You are an expert {language} developer. Refactor and improve the following code. Keep the same functionality but make it cleaner, more efficient and well commented."
|
|
|
|
|
|
|
|
|
|
| 17 |
messages = [
|
| 18 |
{"role": "system", "content": system_prompt},
|
| 19 |
{"role": "user", "content": user_code}
|
| 20 |
]
|
| 21 |
try:
|
| 22 |
+
response = openai.ChatCompletion.create(
|
| 23 |
model="gpt-3.5-turbo",
|
| 24 |
messages=messages,
|
| 25 |
temperature=0.2,
|
| 26 |
max_tokens=1024,
|
| 27 |
+
n=1,
|
| 28 |
+
stop=None,
|
| 29 |
)
|
| 30 |
improved = response.choices[0].message.content.strip()
|
| 31 |
return improved
|
|
|
|
| 35 |
# Build Gradio interface
|
| 36 |
with gr.Blocks() as demo:
|
| 37 |
gr.Markdown("# Code Improver")
|
| 38 |
+
gr.Markdown("Enter your code and select the programming language. The model will return a cleaner version.")
|
|
|
|
|
|
|
| 39 |
with gr.Row():
|
| 40 |
+
code_input = gr.Textbox(label="Original Code", lines=15, placeholder="Paste your code here")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
language_dropdown = gr.Dropdown(
|
| 42 |
+
choices=["Python", "JavaScript", "Java", "C++", "C#", "Go", "Ruby", "PHP"],
|
|
|
|
|
|
|
| 43 |
value="Python",
|
| 44 |
label="Language"
|
| 45 |
)
|
|
|
|
| 53 |
)
|
| 54 |
|
| 55 |
if __name__ == "__main__":
|
| 56 |
+
os.environ["GRADIO_SERVER_PORT"] = "7861"
|
| 57 |
+
demo.launch()
|