Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,62 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import io
|
| 4 |
+
import contextlib
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
# API key from Hugging Face Space secret
|
| 8 |
+
API_KEY = os.environ.get("API_KEY")
|
| 9 |
+
API_URL = "https://api.deepseek.com/v1/chat/completions" # Replace if different
|
| 10 |
+
|
| 11 |
+
# DeepSeek V3 AI explanation function
|
| 12 |
+
def deepseek_reply(code_input):
|
| 13 |
+
if not API_KEY:
|
| 14 |
+
return "β API Key not found. Please set API_KEY in HF Space secrets."
|
| 15 |
+
|
| 16 |
+
headers = {
|
| 17 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 18 |
+
"Content-Type": "application/json"
|
| 19 |
+
}
|
| 20 |
+
payload = {
|
| 21 |
+
"model": "deepseek-coder:33b",
|
| 22 |
+
"messages": [
|
| 23 |
+
{"role": "system", "content": "You are a helpful coding assistant."},
|
| 24 |
+
{"role": "user", "content": code_input}
|
| 25 |
+
],
|
| 26 |
+
"temperature": 0.2,
|
| 27 |
+
"stream": False
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
try:
|
| 31 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 32 |
+
result = response.json()
|
| 33 |
+
return result["choices"][0]["message"]["content"]
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return f"β DeepSeek Error: {e}"
|
| 36 |
+
|
| 37 |
+
# Code execution preview function
|
| 38 |
+
def execute_code(code_input):
|
| 39 |
+
buffer = io.StringIO()
|
| 40 |
+
try:
|
| 41 |
+
with contextlib.redirect_stdout(buffer):
|
| 42 |
+
exec(code_input, {})
|
| 43 |
+
return buffer.getvalue()
|
| 44 |
+
except Exception as e:
|
| 45 |
+
return f"β Execution Error: {e}"
|
| 46 |
+
|
| 47 |
+
# Gradio UI setup
|
| 48 |
+
with gr.Blocks(css="footer {display: none !important}") as demo:
|
| 49 |
+
gr.Markdown("# β‘ Sparkle Code Lab β Powered by DeepSeek V3")
|
| 50 |
|
| 51 |
+
with gr.Row():
|
| 52 |
+
code = gr.Code(label="βοΈ Paste your code or question", language="python", lines=20)
|
| 53 |
+
with gr.Row():
|
| 54 |
+
run_btn = gr.Button("π Run & Explain")
|
| 55 |
+
with gr.Row():
|
| 56 |
+
deepseek_out = gr.Markdown(label="π§ AI Explanation (DeepSeek)")
|
| 57 |
+
exec_out = gr.Textbox(label="π€ Code Output", lines=10, interactive=False)
|
| 58 |
+
|
| 59 |
+
run_btn.click(fn=deepseek_reply, inputs=code, outputs=deepseek_out)
|
| 60 |
+
run_btn.click(fn=execute_code, inputs=code, outputs=exec_out)
|
| 61 |
+
|
| 62 |
+
demo.launch()
|