File size: 2,693 Bytes
e4f7945
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import boto3
import secrets
import setting
import gradio as gr
from openai import OpenAI

client = OpenAI(api_key=setting.OPEN_AI_KEY)

def upload_to_s3(file_name, bucket_name, object_name=None):
    """
    Upload a file to an S3 bucket

    :param file_name: File to upload
    :param bucket_name: Bucket to upload to
    :param object_name: S3 object name. If not specified, file_name is used
    :return: Public URL of the uploaded file
    """
    # If S3 object_name was not specified, use file_name
    if object_name is None:
        object_name = file_name

    # Upload the file
    s3_client = boto3.client('s3', aws_access_key_id=setting.S3_ACCESS_KEY, aws_secret_access_key=setting.S3_SECERET_KEY)
    try:
        response = s3_client.upload_file(file_name, bucket_name, object_name)
    except Exception as e:
        print(f"Upload failed: {e}")
        return None

    # The URL will be of the form 'https://{bucket_name}.s3.amazonaws.com/{object_name}'
    return f"https://{bucket_name}.s3.amazonaws.com/{object_name}"

def get_details(uploaded_file_url):
    response = client.chat.completions.create(
        model="gpt-4-vision-preview",
        messages=[
            {
                "role": "user",
                "content": [
                    {"type": "text", "text": setting.SMART_TEXT},
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": uploaded_file_url,
                        },
                    },
                ],
            }
        ],
        max_tokens=300,
    )

    return(response.choices[0].message.content)

# object_name = secrets.token_hex(5)+'.jpg'
# uploaded_file_url = upload_to_s3('/Users/hacker/Desktop/Cactus/shrikant/ccd.jpg', setting.S3_BUCKET_NAME, object_name)
# print(get_details(uploaded_file_url))

def upload_and_process_image(image_path):
    # Generate a unique object name
    object_name = secrets.token_hex(5) + '.jpg'

    # Upload to S3 and get the URL
    uploaded_file_url = upload_to_s3(image_path, setting.S3_BUCKET_NAME, object_name)

    # If the upload is successful, get details
    if uploaded_file_url:
        details = get_details(uploaded_file_url)
        return details
    else:
        return "Failed to upload the image to S3."

# Gradio interface
iface = gr.Interface(
    fn=upload_and_process_image,
    inputs=gr.Image(type="filepath", label="Upload Image"),
    outputs="text",
    css="footer {visibility: hidden}",
    title="AI Find",
    description="Upload image to get details like number of fans, doors, eletrical sockets, ac vent and windows."
)

if __name__ == "__main__":
    iface.launch()