Spaces:
Paused
Paused
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,53 +1,121 @@
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
"""
|
| 20 |
-
Sends the user code to
|
|
|
|
| 21 |
"""
|
| 22 |
-
system_prompt =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
messages = [
|
| 24 |
{"role": "system", "content": system_prompt},
|
| 25 |
-
{"role": "user", "content": user_code}
|
| 26 |
]
|
| 27 |
-
try:
|
| 28 |
-
response = openai.ChatCompletion.create(
|
| 29 |
-
model="gpt-3.5-turbo",
|
| 30 |
-
messages=messages,
|
| 31 |
-
temperature=0.2,
|
| 32 |
-
max_tokens=1024,
|
| 33 |
-
n=1,
|
| 34 |
-
stop=None,
|
| 35 |
-
)
|
| 36 |
-
improved = response.choices[0].message.content.strip()
|
| 37 |
-
return improved
|
| 38 |
-
except Exception as e:
|
| 39 |
-
return f"Error: {str(e)}"
|
| 40 |
|
| 41 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
with gr.Blocks() as demo:
|
| 43 |
gr.Markdown("# Code Improver")
|
| 44 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
| 45 |
with gr.Row():
|
| 46 |
-
code_input = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
language_dropdown = gr.Dropdown(
|
| 48 |
-
choices=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
value="Python",
|
| 50 |
-
label="Language"
|
| 51 |
)
|
| 52 |
improve_button = gr.Button("Improve")
|
| 53 |
output_box = gr.Textbox(label="Improved Code", lines=15)
|
|
@@ -55,7 +123,7 @@ with gr.Blocks() as demo:
|
|
| 55 |
improve_button.click(
|
| 56 |
fn=improve_code,
|
| 57 |
inputs=[code_input, language_dropdown],
|
| 58 |
-
outputs=output_box
|
| 59 |
)
|
| 60 |
|
| 61 |
demo.launch(server_name="0.0.0.0")
|
|
|
|
| 1 |
# -*- coding: utf-8 -*-
|
| 2 |
import os
|
|
|
|
| 3 |
import warnings
|
|
|
|
|
|
|
| 4 |
import asyncio
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
# Suppress Starlette deprecation warnings that appear in Spaces
|
| 9 |
+
warnings.filterwarnings("ignore", category=UserWarning)
|
| 10 |
+
|
| 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 |
+
groq_client = Groq(api_key=groq_api_key)
|
| 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)
|
|
|
|
| 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")
|