Spaces:
Sleeping
Sleeping
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() |