Humzasheikh01 commited on
Commit
43bea2e
Β·
verified Β·
1 Parent(s): 2551578

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -7
app.py CHANGED
@@ -1,10 +1,62 @@
1
  import gradio as gr
 
 
 
 
2
 
3
- with gr.Blocks(fill_height=True) as demo:
4
- with gr.Sidebar():
5
- gr.Markdown("# Inference Provider")
6
- gr.Markdown("This Space showcases the deepseek-ai/DeepSeek-V3-0324 model, served by the nebius API. Sign in with your Hugging Face account to use this API.")
7
- button = gr.LoginButton("Sign in")
8
- gr.load("models/deepseek-ai/DeepSeek-V3-0324", accept_token=button, provider="nebius")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
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()