Update app.py
Browse files
app.py
CHANGED
|
@@ -24,29 +24,44 @@ def gpt_inference(base_url, model, openai_key, prompt):
|
|
| 24 |
|
| 25 |
newprompt = f'Write Python code that does the following: \n\n{prompt}\n\nNote, the code is going to be executed in a Jupyter Python kernel.\n\nLast instruction, and this is the most important, just return code. No other outputs, as your full response will directly be executed in the kernel.'
|
| 26 |
|
| 27 |
-
headers = {
|
| 28 |
-
'Content-Type': 'application/json',
|
| 29 |
-
'Authorization': f'Bearer {openai_key}'
|
| 30 |
-
}
|
| 31 |
|
| 32 |
data = {
|
| 33 |
"model": model,
|
| 34 |
"messages": [
|
| 35 |
-
{
|
| 36 |
-
"role": "system",
|
| 37 |
-
"content": "You are a helpful assistant."
|
| 38 |
-
},
|
| 39 |
{
|
| 40 |
"role": "user",
|
| 41 |
"content": newprompt
|
| 42 |
}
|
| 43 |
-
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
}
|
| 45 |
|
|
|
|
| 46 |
response = requests.post(f"{base_url}/v1/chat/completions", headers=headers, data=json.dumps(data))
|
| 47 |
-
print(f"
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
img = execute_code(code)
|
| 51 |
return img
|
| 52 |
|
|
|
|
| 24 |
|
| 25 |
newprompt = f'Write Python code that does the following: \n\n{prompt}\n\nNote, the code is going to be executed in a Jupyter Python kernel.\n\nLast instruction, and this is the most important, just return code. No other outputs, as your full response will directly be executed in the kernel.'
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
data = {
|
| 29 |
"model": model,
|
| 30 |
"messages": [
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
{
|
| 32 |
"role": "user",
|
| 33 |
"content": newprompt
|
| 34 |
}
|
| 35 |
+
],
|
| 36 |
+
"temperature": 0.7,
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
headers = {
|
| 40 |
+
"Content-Type": "application/json",
|
| 41 |
+
"Authorization": f"Bearer {openai_key}",
|
| 42 |
}
|
| 43 |
|
| 44 |
+
|
| 45 |
response = requests.post(f"{base_url}/v1/chat/completions", headers=headers, data=json.dumps(data))
|
| 46 |
+
print(f"text: {response.text}")
|
| 47 |
+
|
| 48 |
+
def extract_code(text):
|
| 49 |
+
# Match triple backtick blocks first
|
| 50 |
+
triple_match = re.search(r'```(?:\w+\n)?(.+?)```', text, re.DOTALL)
|
| 51 |
+
if triple_match:
|
| 52 |
+
return triple_match.group(1).strip()
|
| 53 |
+
else:
|
| 54 |
+
# If no triple backtick blocks, match single backtick blocks
|
| 55 |
+
single_match = re.search(r'`(.+?)`', text, re.DOTALL)
|
| 56 |
+
if single_match:
|
| 57 |
+
return single_match.group(1).strip()
|
| 58 |
+
# If no code blocks found, return original text
|
| 59 |
+
return text
|
| 60 |
+
|
| 61 |
+
if response.status_code != 200:
|
| 62 |
+
return "Error: " + response.text, 500
|
| 63 |
+
|
| 64 |
+
code = extract_code(response.json()["choices"][0]["message"]["content"])
|
| 65 |
img = execute_code(code)
|
| 66 |
return img
|
| 67 |
|