Humzasheikh01 commited on
Commit
4388077
Β·
verified Β·
1 Parent(s): 4dc5c73

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -36
app.py CHANGED
@@ -1,15 +1,13 @@
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
 
@@ -17,56 +15,52 @@ def deepseek_reply(code_input):
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("""
50
- # ⚑ Sparkle Code Lab β€” Powered by DeepSeek V3
51
- ### Live Code Explanation & Execution
52
 
53
- This is your space to explore code and get real-time explanations and outputs.
54
  """, elem_id="header")
55
 
56
  with gr.Row():
57
  with gr.Column(scale=5):
58
- # Code input box
59
- code = gr.Code(label="✍️ Paste your code or question", language="python", lines=20)
60
  with gr.Column(scale=2):
61
- # DeepSeek AI output
62
- deepseek_out = gr.Markdown(label="🧠 AI Explanation (DeepSeek)", elem_id="ai-output")
63
- # Execution output
64
- exec_out = gr.Textbox(label="πŸ“€ Code Output", lines=10, interactive=False, elem_id="code-output")
65
- # Run button
66
- run_btn = gr.Button("πŸš€ Run & Explain", elem_id="run-button")
67
-
68
- # Action when button is clicked
69
- run_btn.click(fn=deepseek_reply, inputs=code, outputs=deepseek_out)
70
- run_btn.click(fn=execute_code, inputs=code, outputs=exec_out)
71
 
72
  demo.launch()
 
1
  import gradio as gr
2
  import requests
 
 
3
  import os
4
 
5
  # API key from Hugging Face Space secret
6
  API_KEY = os.environ.get("API_KEY")
7
  API_URL = "https://api.deepseek.com/v1/chat/completions" # Replace if different
8
 
9
+ # DeepSeek V3 Chatbot function
10
+ def deepseek_chat(user_message, chat_history):
11
  if not API_KEY:
12
  return "❌ API Key not found. Please set API_KEY in HF Space secrets."
13
 
 
15
  "Authorization": f"Bearer {API_KEY}",
16
  "Content-Type": "application/json"
17
  }
18
+
19
+ # Append the user message to the conversation history
20
+ chat_history.append({"role": "user", "content": user_message})
21
+
22
  payload = {
23
  "model": "deepseek-coder:33b",
24
+ "messages": chat_history,
25
+ "temperature": 0.7,
 
 
 
26
  "stream": False
27
  }
28
 
29
  try:
30
+ # Make request to the DeepSeek V3 API for the chatbot response
31
  response = requests.post(API_URL, headers=headers, json=payload)
32
  result = response.json()
33
+
34
+ # Extract the AI response
35
+ ai_message = result["choices"][0]["message"]["content"]
36
+
37
+ # Add the AI's response to the chat history
38
+ chat_history.append({"role": "assistant", "content": ai_message})
39
 
40
+ return ai_message, chat_history
 
 
 
 
 
 
41
  except Exception as e:
42
+ return f"❌ DeepSeek Error: {e}", chat_history
43
 
44
+ # Gradio UI setup for the chatbot
45
  with gr.Blocks(css="footer {display: none !important}") as demo:
46
  gr.Markdown("""
47
+ # πŸ€– DeepSeek V3 Chatbot
48
+ ### Chat with the AI powered by DeepSeek V3!
49
 
50
+ Type a message and start chatting with the AI.
51
  """, elem_id="header")
52
 
53
  with gr.Row():
54
  with gr.Column(scale=5):
55
+ # Text input for user message
56
+ user_message = gr.Textbox(label="Your Message", placeholder="Type something...", interactive=True)
57
  with gr.Column(scale=2):
58
+ # Chat history and bot response
59
+ chatbot_output = gr.Chatbot(label="Chat History", elem_id="chatbot-output")
60
+ # Submit button
61
+ submit_button = gr.Button("Send", elem_id="send-button")
62
+
63
+ # Define the action when the button is clicked
64
+ submit_button.click(fn=deepseek_chat, inputs=[user_message, chatbot_output], outputs=[chatbot_output, chatbot_output])
 
 
 
65
 
66
  demo.launch()