Created
December 16, 2023 15:57
-
-
Save kolibril13/efca356ee996d9e7d353333af1a20508 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Path to your image | |
image_path = "image_plot.png" | |
import base64 | |
import requests | |
# OpenAI API Key | |
from api_key import api_key | |
# Function to encode the image | |
def encode_image(image_path): | |
with open(image_path, "rb") as image_file: | |
return base64.b64encode(image_file.read()).decode('utf-8') | |
# Getting the base64 string | |
base64_image = encode_image(image_path) | |
prompt = """ | |
You are an expert data scientist who specializes in creating scientific visualization protopyes with matplotlib from low-fidelity wireframes. Your job is to accept low-fidelity designs and turn them into stunning visualization. When sent new designs, you should reply with your best attempt at a high fidelity working prototype as a single python file. | |
If you have to make calculations, you can use numpy. | |
The designs may include flow charts, diagrams, labels, arrows, sticky notes, screenshots of other applications, or even previous designs. Treat all of these as references for your prototype. Use your best judgement to determine what is an annotation and what should be included in the final result. Treat anything in the color red as an annotation rather than part of the design. Do NOT include any red elements or any other annotations in your final result. | |
Your prototype should look and feel much more complete and advanced than the wireframes provided. Flesh it out, make it real! Try your best to figure out what the data scientist wants and make it happen. If there are any questions or underspecified features, use what you know about applications, user experience, and scientific visualizations to "fill in the blanks". If you're unsure of how the designs should work, take a guess—it's better for you to get it wrong than to leave things incomplete. | |
Remember: you love your data scientist and want them to be happy. The more complete and impressive your prototype, the happier they will be. Good luck, you've got this! | |
Reply ONLY with python code. | |
""" | |
def call_api(prompt, base64_image): | |
headers = { | |
"Content-Type": "application/json", | |
"Authorization": f"Bearer {api_key}" | |
} | |
payload = { | |
"model": "gpt-4-vision-preview", | |
"messages": [ | |
{ | |
"role": "user", | |
"content": [ | |
{ | |
"type": "text", | |
"text": prompt | |
}, | |
{ | |
"type": "image_url", | |
"image_url": { | |
"url": f"data:image/jpeg;base64,{base64_image}" | |
} | |
} | |
] | |
} | |
], | |
"max_tokens": 300 | |
} | |
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload) | |
r = response.json() | |
text_response = r['choices'][0]["message"]["content"] | |
import re | |
# Regular expression to find the block of text | |
pattern = r"```python(.*?)```" | |
match = re.search(pattern, text_response, re.DOTALL) | |
if match: | |
code_inside = match.group(1).strip() # .strip() to remove leading/trailing whitespace | |
# print(code_inside) | |
else: | |
print("No code block found") | |
# print(code_inside) | |
return code_inside | |
print(call_api(prompt, base64_image)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment