Spaces:
Paused
Paused
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -6,26 +6,29 @@ 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 |
|
| 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 =
|
|
|
|
|
|
|
|
|
|
| 17 |
messages = [
|
| 18 |
{"role": "system", "content": system_prompt},
|
| 19 |
{"role": "user", "content": user_code}
|
| 20 |
]
|
| 21 |
try:
|
| 22 |
-
response =
|
| 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,11 +38,19 @@ def improve_code(user_code, language):
|
|
| 35 |
# Build Gradio interface
|
| 36 |
with gr.Blocks() as demo:
|
| 37 |
gr.Markdown("# Code Improver")
|
| 38 |
-
gr.Markdown(
|
|
|
|
|
|
|
| 39 |
with gr.Row():
|
| 40 |
-
code_input = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
language_dropdown = gr.Dropdown(
|
| 42 |
-
choices=[
|
|
|
|
|
|
|
| 43 |
value="Python",
|
| 44 |
label="Language"
|
| 45 |
)
|
|
|
|
| 6 |
# Load OpenAI API key from environment
|
| 7 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 8 |
if not openai_api_key:
|
| 9 |
+
raise ValueError("OPENAI_API_KEY environment variable not set")
|
| 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 = client.chat.completions.create(
|
| 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 |
# 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 |
)
|