Spaces:
Paused
Paused
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,20 +1,22 @@
|
|
| 1 |
# -*- coding: utf-8 -*-
|
| 2 |
import os
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 4 |
import asyncio
|
| 5 |
asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy())
|
| 6 |
-
|
| 7 |
|
| 8 |
-
# Load
|
| 9 |
-
|
| 10 |
-
if not
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
client = Groq(api_key=groq_api_key)
|
| 14 |
|
| 15 |
def improve_code(user_code, language):
|
| 16 |
"""
|
| 17 |
-
Sends the user code to
|
| 18 |
"""
|
| 19 |
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."
|
| 20 |
messages = [
|
|
@@ -22,13 +24,13 @@ def improve_code(user_code, language):
|
|
| 22 |
{"role": "user", "content": user_code}
|
| 23 |
]
|
| 24 |
try:
|
| 25 |
-
response =
|
| 26 |
-
model="
|
| 27 |
messages=messages,
|
| 28 |
temperature=0.2,
|
| 29 |
max_tokens=1024,
|
| 30 |
-
|
| 31 |
-
|
| 32 |
)
|
| 33 |
improved = response.choices[0].message.content.strip()
|
| 34 |
return improved
|
|
|
|
| 1 |
# -*- coding: utf-8 -*-
|
| 2 |
import os
|
| 3 |
import gradio as gr
|
| 4 |
+
import warnings
|
| 5 |
+
from starlette.exceptions import StarletteDeprecationWarning
|
| 6 |
+
warnings.filterwarnings("ignore", category=StarletteDeprecationWarning)
|
| 7 |
import asyncio
|
| 8 |
asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy())
|
| 9 |
+
import openai
|
| 10 |
|
| 11 |
+
# Load OpenAI API key from environment
|
| 12 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 13 |
+
if not openai_api_key:
|
| 14 |
+
openai_api_key = ""
|
| 15 |
+
openai.api_key = openai_api_key
|
|
|
|
| 16 |
|
| 17 |
def improve_code(user_code, language):
|
| 18 |
"""
|
| 19 |
+
Sends the user code to OpenAI GPT model and asks for an improved version.
|
| 20 |
"""
|
| 21 |
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."
|
| 22 |
messages = [
|
|
|
|
| 24 |
{"role": "user", "content": user_code}
|
| 25 |
]
|
| 26 |
try:
|
| 27 |
+
response = openai.ChatCompletion.create(
|
| 28 |
+
model="gpt-3.5-turbo",
|
| 29 |
messages=messages,
|
| 30 |
temperature=0.2,
|
| 31 |
max_tokens=1024,
|
| 32 |
+
n=1,
|
| 33 |
+
stop=None,
|
| 34 |
)
|
| 35 |
improved = response.choices[0].message.content.strip()
|
| 36 |
return improved
|