Spaces:
Paused
Paused
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -3,17 +3,18 @@ import os
|
|
| 3 |
import gradio as gr
|
| 4 |
import asyncio
|
| 5 |
asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy())
|
| 6 |
-
import
|
| 7 |
|
| 8 |
-
# Load
|
| 9 |
-
|
| 10 |
-
if not
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
def improve_code(user_code, language):
|
| 15 |
"""
|
| 16 |
-
Sends the user code to
|
| 17 |
"""
|
| 18 |
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."
|
| 19 |
messages = [
|
|
@@ -21,13 +22,13 @@ def improve_code(user_code, language):
|
|
| 21 |
{"role": "user", "content": user_code}
|
| 22 |
]
|
| 23 |
try:
|
| 24 |
-
response =
|
| 25 |
-
model="
|
| 26 |
messages=messages,
|
| 27 |
temperature=0.2,
|
| 28 |
max_tokens=1024,
|
| 29 |
-
|
| 30 |
-
|
| 31 |
)
|
| 32 |
improved = response.choices[0].message.content.strip()
|
| 33 |
return improved
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
import asyncio
|
| 5 |
asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy())
|
| 6 |
+
from groq import Groq
|
| 7 |
|
| 8 |
+
# Load Groq API key from environment
|
| 9 |
+
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 10 |
+
if not groq_api_key:
|
| 11 |
+
groq_api_key = ""
|
| 12 |
+
|
| 13 |
+
client = Groq(api_key=groq_api_key)
|
| 14 |
|
| 15 |
def improve_code(user_code, language):
|
| 16 |
"""
|
| 17 |
+
Sends the user code to Groq LLM and asks for an improved version.
|
| 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 |
{"role": "user", "content": user_code}
|
| 23 |
]
|
| 24 |
try:
|
| 25 |
+
response = client.chat.completions.create(
|
| 26 |
+
model="llama3-8b-8192",
|
| 27 |
messages=messages,
|
| 28 |
temperature=0.2,
|
| 29 |
max_tokens=1024,
|
| 30 |
+
top_p=1,
|
| 31 |
+
stream=False,
|
| 32 |
)
|
| 33 |
improved = response.choices[0].message.content.strip()
|
| 34 |
return improved
|