Spaces:
Paused
Paused
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -4,77 +4,57 @@ import warnings
|
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
-
# Suppress
|
| 8 |
warnings.filterwarnings("ignore", category=UserWarning)
|
| 9 |
|
| 10 |
# ----------------------------------------------------------------------
|
| 11 |
-
#
|
| 12 |
# ----------------------------------------------------------------------
|
| 13 |
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 14 |
-
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 15 |
-
|
| 16 |
groq_client = None
|
| 17 |
-
openai_client = None
|
| 18 |
|
| 19 |
if groq_api_key:
|
| 20 |
try:
|
| 21 |
from groq import Groq
|
| 22 |
groq_client = Groq(api_key=groq_api_key)
|
| 23 |
-
except Exception:
|
|
|
|
| 24 |
groq_client = None
|
| 25 |
-
|
| 26 |
-
if openai_api_key:
|
| 27 |
-
try:
|
| 28 |
-
import openai
|
| 29 |
-
openai.api_key = openai_api_key
|
| 30 |
-
openai_client = openai
|
| 31 |
-
except Exception:
|
| 32 |
-
openai_client = None
|
| 33 |
|
| 34 |
# ----------------------------------------------------------------------
|
| 35 |
-
# Helper function to improve code
|
| 36 |
# ----------------------------------------------------------------------
|
| 37 |
def improve_code(original_code: str) -> str:
|
| 38 |
"""
|
| 39 |
-
|
|
|
|
| 40 |
"""
|
| 41 |
if not original_code.strip():
|
| 42 |
return "Please provide some code to improve."
|
| 43 |
|
|
|
|
|
|
|
|
|
|
| 44 |
prompt = (
|
| 45 |
-
"You are a senior software engineer. "
|
| 46 |
-
"
|
| 47 |
-
"
|
| 48 |
-
"\n\nOriginal code:\n"
|
| 49 |
f"{original_code}"
|
| 50 |
)
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
if openai_client:
|
| 66 |
-
try:
|
| 67 |
-
response = openai_client.ChatCompletion.create(
|
| 68 |
-
model="gpt-4o-mini",
|
| 69 |
-
messages=[{"role": "user", "content": prompt}],
|
| 70 |
-
temperature=0.2,
|
| 71 |
-
max_tokens=2000,
|
| 72 |
-
)
|
| 73 |
-
return response.choices[0].message.content.strip()
|
| 74 |
-
except Exception as e:
|
| 75 |
-
return f"Error with OpenAI: {e}"
|
| 76 |
-
|
| 77 |
-
return "No LLM API key configured. Please set GROQ_API_KEY or OPENAI_API_KEY."
|
| 78 |
|
| 79 |
# ----------------------------------------------------------------------
|
| 80 |
# Gradio interface
|
|
|
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
+
# Suppress warnings that can appear in the Space environment
|
| 8 |
warnings.filterwarnings("ignore", category=UserWarning)
|
| 9 |
|
| 10 |
# ----------------------------------------------------------------------
|
| 11 |
+
# Groq client setup (only)
|
| 12 |
# ----------------------------------------------------------------------
|
| 13 |
groq_api_key = os.getenv("GROQ_API_KEY")
|
|
|
|
|
|
|
| 14 |
groq_client = None
|
|
|
|
| 15 |
|
| 16 |
if groq_api_key:
|
| 17 |
try:
|
| 18 |
from groq import Groq
|
| 19 |
groq_client = Groq(api_key=groq_api_key)
|
| 20 |
+
except Exception as e:
|
| 21 |
+
# If the library cannot be imported or instantiated, keep client as None
|
| 22 |
groq_client = None
|
| 23 |
+
print(f"Failed to initialise Groq client: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# ----------------------------------------------------------------------
|
| 26 |
+
# Helper function to improve code using Groq
|
| 27 |
# ----------------------------------------------------------------------
|
| 28 |
def improve_code(original_code: str) -> str:
|
| 29 |
"""
|
| 30 |
+
Sends the original code to the Groq LLM and returns an improved version.
|
| 31 |
+
If no API key is configured, returns an informative message.
|
| 32 |
"""
|
| 33 |
if not original_code.strip():
|
| 34 |
return "Please provide some code to improve."
|
| 35 |
|
| 36 |
+
if not groq_client:
|
| 37 |
+
return "No Groq API key configured. Set the GROQ_API_KEY environment variable."
|
| 38 |
+
|
| 39 |
prompt = (
|
| 40 |
+
"You are a senior software engineer. Improve the following code for readability, "
|
| 41 |
+
"efficiency, and best practices. Return only the improved code without any explanation.\n\n"
|
| 42 |
+
"Original code:\n"
|
|
|
|
| 43 |
f"{original_code}"
|
| 44 |
)
|
| 45 |
|
| 46 |
+
try:
|
| 47 |
+
response = groq_client.chat.completions.create(
|
| 48 |
+
model="llama3-70b-8192",
|
| 49 |
+
messages=[{"role": "user", "content": prompt}],
|
| 50 |
+
temperature=0.2,
|
| 51 |
+
max_tokens=2000,
|
| 52 |
+
)
|
| 53 |
+
# The content may be None if the API fails; guard against that
|
| 54 |
+
improved = response.choices[0].message.content
|
| 55 |
+
return improved.strip() if improved else "No content returned from Groq."
|
| 56 |
+
except Exception as e:
|
| 57 |
+
return f"Error communicating with Groq: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
# ----------------------------------------------------------------------
|
| 60 |
# Gradio interface
|