Google Cloud + Open AI Single image link from TXT file description generator

PHOTO EMBED

Fri Dec 22 2023 16:01:09 GMT+0000 (Coordinated Universal Time)

Saved by @ivxn

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")
content_copyCOPY

This code does the following 1. uploads the folder of photos to a Google Bucket 2. gets the URLs from the uploaded photos, and saves them in a separate .txt file 3. takes a single URL from the .txt file and sends it to OpenAI, gets a description back What I need to figure out next is the following: - how can I pass a .txt with multiple links and iterate through them, get responses and save them as one argument - deal with the whole front end side of the things (how the output is going to look like)