Spaces:
Paused
Paused
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
# -*- coding: utf-8 -*-
|
| 2 |
import os
|
| 3 |
import warnings
|
| 4 |
-
import asyncio
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
|
|
@@ -11,119 +10,13 @@ warnings.filterwarnings("ignore", category=UserWarning)
|
|
| 11 |
# ----------------------------------------------------------------------
|
| 12 |
# LLM client setup
|
| 13 |
# ----------------------------------------------------------------------
|
| 14 |
-
# Try Groq first (preferred). If the GROQ_API_KEY env var is not set,
|
| 15 |
-
# fall back to OpenAI (requires OPENAI_API_KEY).
|
| 16 |
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 17 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 18 |
|
|
|
|
|
|
|
|
|
|
| 19 |
if groq_api_key:
|
| 20 |
try:
|
| 21 |
from groq import Groq
|
| 22 |
-
|
| 23 |
-
except Exception as e:
|
| 24 |
-
groq_client = None
|
| 25 |
-
print(f"Failed to init Groq client: {e}")
|
| 26 |
-
else:
|
| 27 |
-
groq_client = None
|
| 28 |
-
|
| 29 |
-
if not groq_api_key and openai_api_key:
|
| 30 |
-
try:
|
| 31 |
-
import openai
|
| 32 |
-
openai.api_key = openai_api_key
|
| 33 |
-
except Exception as e:
|
| 34 |
-
openai = None
|
| 35 |
-
print(f"Failed to init OpenAI client: {e}")
|
| 36 |
-
else:
|
| 37 |
-
openai = None
|
| 38 |
-
|
| 39 |
-
# ----------------------------------------------------------------------
|
| 40 |
-
# Core logic
|
| 41 |
-
# ----------------------------------------------------------------------
|
| 42 |
-
def improve_code(user_code: str, language: str) -> str:
|
| 43 |
-
"""
|
| 44 |
-
Sends the user code to a LLM (Groq preferred, OpenAI fallback) and asks
|
| 45 |
-
for an improved version. Returns the improved code or an error message.
|
| 46 |
-
"""
|
| 47 |
-
system_prompt = (
|
| 48 |
-
f"You are an expert {language} developer. Refactor and improve the "
|
| 49 |
-
"following code. Keep the same functionality but make it cleaner, "
|
| 50 |
-
"more efficient and well commented."
|
| 51 |
-
)
|
| 52 |
-
messages = [
|
| 53 |
-
{"role": "system", "content": system_prompt},
|
| 54 |
-
{"role": "user", "content": user_code},
|
| 55 |
-
]
|
| 56 |
-
|
| 57 |
-
# ------------------------------------------------------------------
|
| 58 |
-
# Try Groq
|
| 59 |
-
# ------------------------------------------------------------------
|
| 60 |
-
if groq_client:
|
| 61 |
-
try:
|
| 62 |
-
response = groq_client.chat.completions.create(
|
| 63 |
-
model="llama3-70b-8192",
|
| 64 |
-
messages=messages,
|
| 65 |
-
temperature=0.2,
|
| 66 |
-
max_tokens=1024,
|
| 67 |
-
)
|
| 68 |
-
improved = response.choices[0].message.content.strip()
|
| 69 |
-
return improved
|
| 70 |
-
except Exception as e:
|
| 71 |
-
return f"Groq error: {str(e)}"
|
| 72 |
-
|
| 73 |
-
# ------------------------------------------------------------------
|
| 74 |
-
# Fallback to OpenAI
|
| 75 |
-
# ------------------------------------------------------------------
|
| 76 |
-
if openai:
|
| 77 |
-
try:
|
| 78 |
-
response = openai.ChatCompletion.create(
|
| 79 |
-
model="gpt-3.5-turbo",
|
| 80 |
-
messages=messages,
|
| 81 |
-
temperature=0.2,
|
| 82 |
-
max_tokens=1024,
|
| 83 |
-
)
|
| 84 |
-
improved = response.choices[0].message.content.strip()
|
| 85 |
-
return improved
|
| 86 |
-
except Exception as e:
|
| 87 |
-
return f"OpenAI error: {str(e)}"
|
| 88 |
-
|
| 89 |
-
return "No LLM credentials found. Set GROQ_API_KEY or OPENAI_API_KEY."
|
| 90 |
-
|
| 91 |
-
# ----------------------------------------------------------------------
|
| 92 |
-
# Gradio UI
|
| 93 |
-
# ----------------------------------------------------------------------
|
| 94 |
-
with gr.Blocks() as demo:
|
| 95 |
-
gr.Markdown("# Code Improver")
|
| 96 |
-
gr.Markdown(
|
| 97 |
-
"Enter your code, select the programming language, and click **Improve**. "
|
| 98 |
-
"The model will return a cleaner version."
|
| 99 |
-
)
|
| 100 |
-
with gr.Row():
|
| 101 |
-
code_input = gr.Textbox(
|
| 102 |
-
label="Original Code",
|
| 103 |
-
lines=15,
|
| 104 |
-
placeholder="Paste your code here",
|
| 105 |
-
)
|
| 106 |
-
language_dropdown = gr.Dropdown(
|
| 107 |
-
choices=[
|
| 108 |
-
"Python",
|
| 109 |
-
"JavaScript",
|
| 110 |
-
"Java",
|
| 111 |
-
"C++",
|
| 112 |
-
"C#",
|
| 113 |
-
"Go",
|
| 114 |
-
"Ruby",
|
| 115 |
-
"PHP",
|
| 116 |
-
],
|
| 117 |
-
value="Python",
|
| 118 |
-
label="Language",
|
| 119 |
-
)
|
| 120 |
-
improve_button = gr.Button("Improve")
|
| 121 |
-
output_box = gr.Textbox(label="Improved Code", lines=15)
|
| 122 |
-
|
| 123 |
-
improve_button.click(
|
| 124 |
-
fn=improve_code,
|
| 125 |
-
inputs=[code_input, language_dropdown],
|
| 126 |
-
outputs=output_box,
|
| 127 |
-
)
|
| 128 |
-
|
| 129 |
-
demo.launch(server_name="0.0.0.0")
|
|
|
|
| 1 |
# -*- coding: utf-8 -*-
|
| 2 |
import os
|
| 3 |
import warnings
|
|
|
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
|
|
|
|
| 10 |
# ----------------------------------------------------------------------
|
| 11 |
# LLM client setup
|
| 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 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|