Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,43 +2,47 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
API_KEY = os.getenv("OPENAI_API_KEY")
|
| 7 |
|
| 8 |
-
# List of supported programming languages
|
| 9 |
languages = ["Python", "C", "C++", "Java", "JavaScript", "Solidity", "SQL", "Bash"]
|
| 10 |
|
| 11 |
def code_assistant(prompt, language="Python"):
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
system_prompt = f"You are an expert {language} programmer. Only respond with code or explanation as requested.
|
| 17 |
-
|
| 18 |
-
response = requests.post(
|
| 19 |
-
"https://api.openai.com/v1/chat/completions",
|
| 20 |
-
headers={"Authorization": f"Bearer {API_KEY}"},
|
| 21 |
-
json={
|
| 22 |
-
"model": "gpt-4o-mini",
|
| 23 |
-
"messages": [
|
| 24 |
-
{"role": "system", "content": system_prompt},
|
| 25 |
-
{"role": "user", "content": prompt}
|
| 26 |
-
]
|
| 27 |
-
}
|
| 28 |
-
)
|
| 29 |
|
| 30 |
-
# Extract the output text
|
| 31 |
try:
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
-
return f"Error: {e}\nResponse: {response.text}"
|
| 35 |
|
| 36 |
-
# Gradio
|
| 37 |
iface = gr.Interface(
|
| 38 |
fn=code_assistant,
|
| 39 |
inputs=[gr.Textbox(label="Your Prompt", placeholder="Write, explain, or debug code..."),
|
| 40 |
gr.Dropdown(languages, label="Language")],
|
| 41 |
-
outputs=gr.Textbox(label="Output"),
|
| 42 |
title="TheWizard 💬 — Code Assistant",
|
| 43 |
description="Ask for code, explanations, or debugging in multiple languages. Focused only on coding."
|
| 44 |
)
|
|
|
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Read API key from environment variable
|
| 6 |
API_KEY = os.getenv("OPENAI_API_KEY")
|
| 7 |
|
|
|
|
| 8 |
languages = ["Python", "C", "C++", "Java", "JavaScript", "Solidity", "SQL", "Bash"]
|
| 9 |
|
| 10 |
def code_assistant(prompt, language="Python"):
|
| 11 |
+
# Check if API key exists
|
| 12 |
+
if not API_KEY:
|
| 13 |
+
return "Error: API key not set. Please add your OPENAI_API_KEY in Space Secrets and restart the Space."
|
| 14 |
+
|
| 15 |
+
system_prompt = f"You are an expert {language} programmer. Only respond with code or explanation as requested. Wrap code in Markdown-style code blocks."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
|
|
|
| 17 |
try:
|
| 18 |
+
response = requests.post(
|
| 19 |
+
"https://api.openai.com/v1/chat/completions",
|
| 20 |
+
headers={"Authorization": f"Bearer {API_KEY}"},
|
| 21 |
+
json={
|
| 22 |
+
"model": "gpt-4o-mini",
|
| 23 |
+
"messages": [
|
| 24 |
+
{"role": "system", "content": system_prompt},
|
| 25 |
+
{"role": "user", "content": prompt}
|
| 26 |
+
]
|
| 27 |
+
}
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
text = response.json()["choices"][0]["message"]["content"]
|
| 31 |
+
|
| 32 |
+
# Ensure output is wrapped in Markdown code block
|
| 33 |
+
if not text.startswith("```"):
|
| 34 |
+
text = f"```{language.lower()}\n{text}\n```"
|
| 35 |
+
return text
|
| 36 |
+
|
| 37 |
except Exception as e:
|
| 38 |
+
return f"Error: {e}\nResponse: {response.text if 'response' in locals() else 'No response'}"
|
| 39 |
|
| 40 |
+
# Gradio UI
|
| 41 |
iface = gr.Interface(
|
| 42 |
fn=code_assistant,
|
| 43 |
inputs=[gr.Textbox(label="Your Prompt", placeholder="Write, explain, or debug code..."),
|
| 44 |
gr.Dropdown(languages, label="Language")],
|
| 45 |
+
outputs=gr.Textbox(label="Output", lines=20, interactive=False),
|
| 46 |
title="TheWizard 💬 — Code Assistant",
|
| 47 |
description="Ask for code, explanations, or debugging in multiple languages. Focused only on coding."
|
| 48 |
)
|