deepfaketar / python /testgpt4.py
blorg469's picture
Upload folder using huggingface_hub
90cd92a verified
import os
import requests
import base64
import sys
import json
import time
# if len(sys.argv) != 2:
# print("Usage: python script.py <image_directory>")
# sys.exit(1)
# # Get command-line arguments
# IMG_PATH = sys.argv[1] #directory path
#print(IMG_PATH)
# Configuration
IMG_PATHS = [
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Young-Smiling-Eyeglasses/042200.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Young-Smiling-Eyeglasses/033209.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Young-Smiling-Eyeglasses/018822.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Beard-Young/023024.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Beard-Young/052001.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Beard-Young/058170.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Smiling-Eyeglasses-Young-Beard-Bangs/011362.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Smiling-Eyeglasses-Young-Beard-Bangs/046696.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Smiling-Eyeglasses-Young-Beard-Bangs/059202.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Beard-Eyeglasses-Bangs/057969.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Beard-Eyeglasses-Bangs/058132.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Beard-Eyeglasses-Bangs/022461.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Bangs/063625.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Bangs/063630.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Bangs/063654.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Eyeglasses-Bangs/015686.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Eyeglasses-Bangs/040011.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Eyeglasses-Bangs/055742.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Beard-Bangs-Eyeglasses-Young/029877.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Beard-Bangs-Eyeglasses-Young/046520.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Beard-Bangs-Eyeglasses-Young/052232.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Bangs-Smiling/005454.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Bangs-Smiling/033267.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Bangs-Smiling/058542.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Young-Eyeglasses/045280.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Young-Eyeglasses/063402.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Young-Eyeglasses/011625.jpg",
]
for IMG_PATH in IMG_PATHS:
GPT4V_KEY = "274b238615f54aa99389e5d99b417f92"
# IMAGE_PATH = "/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Smiling-Eyeglasses-Young-Beard-Bangs/063441.jpg"
encoded_image = base64.b64encode(open(IMG_PATH, 'rb').read()).decode('ascii')
headers = {
"Content-Type": "application/json",
"api-key": GPT4V_KEY,
}
# Payload for the request
payload = {
"messages": [
{
"role": "system",
"content": [
{
"type": "text",
"text": "You are an expert at analyzing images with computer vision."
}
]
},
{
"role": "user",
"content": [
{"type": "text", "text": "The following is an image of a person. Think carefully about where this person's hair, beard, eyes, smile, or young/old appearance may have been modified artificially. Think carefully and try not to make an extraneous guesses. Then output your thoughts."},
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{encoded_image}"}}
]
}
],
"temperature": 0.7,
"top_p": 0.95,
"max_tokens": 800
}
GPT4V_ENDPOINT = "https://utd.openai.azure.com/openai/deployments/g4vp/chat/completions?api-version=2023-07-01-preview"
# Send request
try:
response = requests.post(GPT4V_ENDPOINT, headers=headers, json=payload)
response.raise_for_status() # Will raise an HTTPError if the HTTP request returned an unsuccessful status code
except requests.RequestException as e:
raise SystemExit(f"Failed to make the request. Error: {e}")
# Handle the response as needed (e.g., print or process)
directory = '/mnt/user/myang/LLaVA-2/my_code/json/gpt4responses'
file_path = os.path.join(directory, 'response.json')
# Write the JSON data to the file
with open(file_path, 'a') as file:
file.write('\n')
json.dump(response.json(), file)
print("Done")
time.sleep(15)