-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.py
More file actions
62 lines (51 loc) · 1.72 KB
/
basic.py
File metadata and controls
62 lines (51 loc) · 1.72 KB
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
import os
import base64
import requests
from dotenv import load_dotenv
import cv2
import tensorflow as tf
import numpy as np
from openai import OpenAI
# Load environment variables from .env file
load_dotenv()
# Retrieve the API key from environment variables
api_key = os.getenv('api-key')
client = OpenAI(api_key=api_key)
if api_key is None:
raise ValueError("API key not found. Please set the OPENAI_API_KEY environment variable.")
print(f"Loaded API key: {api_key}") # Debugging line to verify API key loading
def encode_image(image_path):
"""Encodes the image to base64 format."""
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
def analyze_image(image_path, prompt="What’s in this image?"):
"""Analyzes the image using OpenAI's vision capabilities."""
base64_image = encode_image(image_path)
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {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,{base64_image}"
}
}
]
}
],
"max_tokens": 300
}
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
return response.json()
# Path to your image file
image_path = 'test_image.png'
result = analyze_image(image_path)
print(result)