Files changed (2) hide show
  1. app.py +24 -40
  2. requirements.txt +1 -3
app.py CHANGED
@@ -2,65 +2,49 @@ import gradio as gr
2
  import requests
3
  import os
4
 
5
- # Hugging Face token from Secrets
6
- HF_TOKEN = os.getenv("HF_API_TOKEN")
7
- MODEL_ID = "bigcode/starcoder2-3b" # Free multi-language code model
8
 
9
  languages = ["Python", "C", "C++", "Java", "JavaScript", "Solidity", "SQL", "Bash"]
10
 
11
  def code_assistant(prompt, language="Python"):
12
- if not HF_TOKEN:
13
- return "Error: Hugging Face API token not set. Please add HF_API_TOKEN in Space Secrets and restart the Space."
14
-
15
- # Prepend language info to prompt for multi-language generation
16
- full_prompt = f"# Language: {language}\n{prompt}"
17
-
18
- headers = {
19
- "Authorization": f"Bearer {HF_TOKEN}",
20
- "Content-Type": "application/json"
21
- }
22
- payload = {
23
- "inputs": full_prompt,
24
- "options": {"use_cache": False} # optional
25
- }
26
 
 
 
27
  try:
28
  response = requests.post(
29
- f"https://router.huggingface.co/models/{MODEL_ID}",
30
- headers=headers,
31
- json=payload,
32
- timeout=60
 
 
 
 
 
33
  )
34
 
35
- data = response.json()
36
 
37
- # HF router returns list with generated_text
38
- if isinstance(data, dict) and "generated_text" in data:
39
- text = data["generated_text"]
40
- elif isinstance(data, dict) and "error" in data:
41
- return f"Error from model: {data['error']}"
42
- else:
43
- text = str(data)
44
-
45
- # Wrap in Markdown code block
46
  if not text.startswith("```"):
47
  text = f"```{language.lower()}\n{text}\n```"
48
-
49
  return text
50
 
51
  except Exception as e:
52
- return f"Error: {e}"
53
 
54
  # Gradio UI
55
  iface = gr.Interface(
56
  fn=code_assistant,
57
- inputs=[
58
- gr.Textbox(label="Your Prompt", placeholder="Write, explain, or debug code..."),
59
- gr.Dropdown(languages, label="Language")
60
- ],
61
- outputs=gr.Markdown(label="Output"),
62
- title="TheWizard 💬 — Free Code Assistant",
63
- description="Ask for code, explanations, or debugging in multiple languages using free HF models."
64
  )
65
 
66
  iface.launch()
 
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
  )
49
 
50
  iface.launch()
requirements.txt CHANGED
@@ -1,3 +1 @@
1
- gradio>=6.6.0
2
- requests
3
- python-dotenv
 
1
+ huggingface_hub==0.22.2