Update codex/code_generator.py
Browse files- codex/code_generator.py +18 -4
codex/code_generator.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
|
| 4 |
-
HF_API = "https://api-inference.huggingface.co/models/
|
| 5 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 6 |
|
| 7 |
def generate_code(task):
|
|
@@ -11,6 +11,8 @@ def generate_code(task):
|
|
| 11 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 12 |
|
| 13 |
prompt = f"""
|
|
|
|
|
|
|
| 14 |
Write safe Python code to accomplish this task:
|
| 15 |
|
| 16 |
{task}
|
|
@@ -21,14 +23,26 @@ Rules:
|
|
| 21 |
- No network calls
|
| 22 |
- Must define a function
|
| 23 |
- Must assign final output to variable named `result`
|
|
|
|
| 24 |
- Return only Python code
|
| 25 |
"""
|
| 26 |
|
| 27 |
-
payload = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
r = requests.post(HF_API, headers=headers, json=payload, timeout=60)
|
| 29 |
r.raise_for_status()
|
| 30 |
|
| 31 |
text = r.json()[0]["generated_text"]
|
| 32 |
-
code = text.split("```")[-1].strip()
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
|
| 4 |
+
HF_API = "https://api-inference.huggingface.co/models/Qwen/Qwen2.5-Coder-7B-Instruct"
|
| 5 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 6 |
|
| 7 |
def generate_code(task):
|
|
|
|
| 11 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 12 |
|
| 13 |
prompt = f"""
|
| 14 |
+
You are a senior Python engineer.
|
| 15 |
+
|
| 16 |
Write safe Python code to accomplish this task:
|
| 17 |
|
| 18 |
{task}
|
|
|
|
| 23 |
- No network calls
|
| 24 |
- Must define a function
|
| 25 |
- Must assign final output to variable named `result`
|
| 26 |
+
- Must not use input()
|
| 27 |
- Return only Python code
|
| 28 |
"""
|
| 29 |
|
| 30 |
+
payload = {
|
| 31 |
+
"inputs": prompt,
|
| 32 |
+
"parameters": {
|
| 33 |
+
"max_new_tokens": 350,
|
| 34 |
+
"temperature": 0.2,
|
| 35 |
+
"top_p": 0.9
|
| 36 |
+
}
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
r = requests.post(HF_API, headers=headers, json=payload, timeout=60)
|
| 40 |
r.raise_for_status()
|
| 41 |
|
| 42 |
text = r.json()[0]["generated_text"]
|
|
|
|
| 43 |
|
| 44 |
+
# Strip markdown if present
|
| 45 |
+
if "```" in text:
|
| 46 |
+
text = text.split("```")[-1]
|
| 47 |
+
|
| 48 |
+
return text.strip()
|