Spaces:
Paused
Paused
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -19,4 +19,86 @@ openai_client = None
|
|
| 19 |
if groq_api_key:
|
| 20 |
try:
|
| 21 |
from groq import Groq
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
Send the original code to the LLM and return the improved version.
|
| 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 |
+
"Improve the following code for readability, efficiency, and best practices. "
|
| 47 |
+
"Return only the improved code without any explanation."
|
| 48 |
+
"\n\nOriginal code:\n"
|
| 49 |
+
f"{original_code}"
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
# Prefer Groq if available, otherwise fallback to OpenAI
|
| 53 |
+
if groq_client:
|
| 54 |
+
try:
|
| 55 |
+
response = groq_client.chat.completions.create(
|
| 56 |
+
model="llama3-70b-8192",
|
| 57 |
+
messages=[{"role": "user", "content": prompt}],
|
| 58 |
+
temperature=0.2,
|
| 59 |
+
max_tokens=2000,
|
| 60 |
+
)
|
| 61 |
+
return response.choices[0].message.content.strip()
|
| 62 |
+
except Exception as e:
|
| 63 |
+
return f"Error with Groq: {e}"
|
| 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
|
| 81 |
+
# ----------------------------------------------------------------------
|
| 82 |
+
with gr.Blocks() as demo:
|
| 83 |
+
gr.Markdown("# Code Improver")
|
| 84 |
+
with gr.Row():
|
| 85 |
+
with gr.Column():
|
| 86 |
+
code_input = gr.Textbox(
|
| 87 |
+
label="Original Code",
|
| 88 |
+
placeholder="Paste your code here",
|
| 89 |
+
lines=20,
|
| 90 |
+
max_lines=200,
|
| 91 |
+
show_copy_button=True,
|
| 92 |
+
)
|
| 93 |
+
with gr.Column():
|
| 94 |
+
code_output = gr.Textbox(
|
| 95 |
+
label="Improved Code",
|
| 96 |
+
placeholder="Improved code will appear here",
|
| 97 |
+
lines=20,
|
| 98 |
+
max_lines=200,
|
| 99 |
+
show_copy_button=True,
|
| 100 |
+
)
|
| 101 |
+
improve_btn = gr.Button("Improve")
|
| 102 |
+
improve_btn.click(fn=improve_code, inputs=code_input, outputs=code_output)
|
| 103 |
+
|
| 104 |
+
demo.launch()
|