Spaces:
Runtime error
Runtime error
File size: 5,056 Bytes
c2b923e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
#nicht aktuell
import os
from openai import OpenAI
import requests
import base64
client = OpenAI()
def image_bytes_to_base64(image_bytes):
"""
Converts an image from bytes to a Base64 encoded string.
Args:
image_bytes (bytes): Byte content of the image.
Returns:
str: A Base64 encoded string of the image.
"""
return base64.b64encode(image_bytes).decode('utf-8')
def image_to_base64(image_path):
with open(image_path, "rb") as image_file:
return str(base64.b64encode(image_file.read()).decode('utf-8'))
def gpt4_new(prompt_text):
gpt_response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "system",
"content": "Du bist eine Maschine, die Dokumente klassifiziert."},
{"role": "user", "content": prompt_text}])
return gpt_response.choices[0].message.content
def vectorize_data(data_input):
# input can be list or string:
if isinstance(data_input, list):
# returning a dictionary
my_dict = {}
for item in data_input:
my_dict[str(item)] = client.embeddings.create(input=data_input,
model="text-embedding-ada-002").data[0].embedding
return my_dict
elif isinstance(data_input, str):
# returning just the vector
return client.embeddings.create(input=data_input, model="text-embedding-ada-002").data[0].embedding
else:
print("none")
def img_create(prompt="a nice house on the beach", download_path=""):
# to open, must download
my_url = client.images.generate(model="dall-e-3", prompt=prompt, size="1024x1024").data[0].url
if download_path:
my_image = requests.get(my_url)
if my_image.status_code == 200:
with open(download_path, 'wb') as f:
f.write(my_image.content)
else:
print("Failed to retrieve image")
return my_url
def img_to_text(img_url="", img_base64="", prompt="What’s in this image?", print_out=True):
if img_url:
img_desc_response = client.chat.completions.create(
model="gpt-4-turbo",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": prompt},
{
"type": "image_url",
"image_url": {
"url": img_url,
},
},
],
}
],
max_tokens=500,
)
if print_out:
print(img_desc_response.choices[0].message.content)
return img_desc_response.choices[0].message.content
elif img_base64:
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {os.environ['OPENAI_API_KEY']}"
}
payload = {
"model": "gpt-4-turbo",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{img_base64}"
}
}
]
}
],
"max_tokens": 300
}
img_desc_response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
if print_out:
print(img_desc_response.json()["choices"][0]["message"]["content"])
return img_desc_response.json()["choices"][0]["message"]["content"]
else:
return ValueError
def encode_image_to_base64(image_path):
with open(image_path, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
return encoded_string
def table_to_text(table=None, prompt="describe this table in plain text. "
"be as precise as possible. spare no detail. "
"what is in this table?", print_out=True):
if table is not None:
response = gpt4_new(f"{prompt} TABLE: {table}")
if print_out:
print(response)
return response
else:
return ValueError
if __name__ == "__main__":
#print("here are all functions that directly call openai.")
#img_create("a skier in the swiss alps", download_path="skier.png")
#img_to_text(img_base64=encode_image_to_base64("skier.png"))
#print(image_to_base64("skier.png"))
#print(vectorize_data("test string"))
print(gpt4_new())
|