import os from google.cloud import storage import openai from openai import OpenAI import requests from google.oauth2 import service_account def upload_to_google_cloud(local_file_path, bucket_name, destination_blob_name, credentials_json): # Initialize the Google Cloud client storage_client = storage.Client.from_service_account_json(credentials_json) bucket = storage_client.bucket(bucket_name) blob = bucket.blob(destination_blob_name) # Upload the file blob.upload_from_filename(local_file_path) # The public URL can be used to directly access the uploaded file via HTTP return blob.public_url def upload_folder(folder_path, bucket_name, credentials_json): urls = [] for filename in os.listdir(folder_path): if filename.lower().endswith(('.png', '.jpg', '.jpeg')): local_file_path = os.path.join(folder_path, filename) destination_blob_name = filename url = upload_to_google_cloud(local_file_path, bucket_name, destination_blob_name, credentials_json) urls.append(url) return urls def save_urls_to_file(urls, file_path): with open(file_path, 'w') as file: for url in urls: file.write(url + '\n') print(f"URLs saved to {file_path}") # Example usage folder_path = 'C:/Users/Cibe/Downloads/apartments' credentials_json = 'C:/Users/Cibe/Google-Json/credentials.json' bucket_name = 'monkempire-test-1' #Upload the folder and get URLs #uploaded_urls = upload_folder(folder_path, bucket_name, credentials_json) #Save the URLs to a file #(uploaded_urls, 'uploaded_images_urls.txt') def read_urls_from_file(file_path): with open(file_path, 'r') as file: return [line.strip() for line in file.readlines()] client = OpenAI(api_key='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') def generate_description(text): try: response = client.chat.completions.create( model="gpt-4-vision-preview", messages=[ { "role": "user", "content": [ {"type": "text", "text": "You are a real estate agent, and you want to generate a sales description of the listing based of the pictures."}, { "type": "image_url", "image_url": { "url": text, }, }, ], } ], max_tokens=150, ) return response.choices[0] except Exception as e: return str(e) # Example usage file_path = 'uploaded_image_url.txt' # Read URLs from the file #image_urls = read_urls_from_file(file_path) with open('uploaded_image_url.txt', 'r') as file: image_urls = file.read().rstrip() # Generate and print descriptions for each image description = generate_description(image_urls) print(f"Description for the image:\n{description}\n")
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter