Update codex/code_generator.py
Browse files- codex/code_generator.py +11 -4
codex/code_generator.py
CHANGED
|
@@ -5,6 +5,9 @@ HF_API = "https://api-inference.huggingface.co/models/bigcode/starcoder"
|
|
| 5 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 6 |
|
| 7 |
def generate_code(task):
|
|
|
|
|
|
|
|
|
|
| 8 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 9 |
prompt = f"""
|
| 10 |
Write safe Python code to accomplish this task:
|
|
@@ -15,11 +18,15 @@ Rules:
|
|
| 15 |
- No OS commands
|
| 16 |
- No file deletion
|
| 17 |
- No network calls
|
| 18 |
-
-
|
| 19 |
-
-
|
|
|
|
| 20 |
"""
|
| 21 |
|
| 22 |
-
payload = {"inputs": prompt, "parameters": {"max_new_tokens":
|
| 23 |
r = requests.post(HF_API, headers=headers, json=payload, timeout=60)
|
|
|
|
|
|
|
| 24 |
text = r.json()[0]["generated_text"]
|
| 25 |
-
|
|
|
|
|
|
| 5 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 6 |
|
| 7 |
def generate_code(task):
|
| 8 |
+
if not HF_TOKEN:
|
| 9 |
+
raise RuntimeError("HF_TOKEN not set")
|
| 10 |
+
|
| 11 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 12 |
prompt = f"""
|
| 13 |
Write safe Python code to accomplish this task:
|
|
|
|
| 18 |
- No OS commands
|
| 19 |
- No file deletion
|
| 20 |
- No network calls
|
| 21 |
+
- Must define a function
|
| 22 |
+
- Must assign final output to variable named `result`
|
| 23 |
+
- Return only Python code
|
| 24 |
"""
|
| 25 |
|
| 26 |
+
payload = {"inputs": prompt, "parameters": {"max_new_tokens": 300}}
|
| 27 |
r = requests.post(HF_API, headers=headers, json=payload, timeout=60)
|
| 28 |
+
r.raise_for_status()
|
| 29 |
+
|
| 30 |
text = r.json()[0]["generated_text"]
|
| 31 |
+
code = text.split("```")[-1].strip()
|
| 32 |
+
return code
|