Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +84 -0
- requirements.txt +3 -0
- setting.py +5 -0
app.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import boto3
|
2 |
+
import secrets
|
3 |
+
import setting
|
4 |
+
import gradio as gr
|
5 |
+
from openai import OpenAI
|
6 |
+
|
7 |
+
client = OpenAI(api_key=setting.OPEN_AI_KEY)
|
8 |
+
|
9 |
+
def upload_to_s3(file_name, bucket_name, object_name=None):
|
10 |
+
"""
|
11 |
+
Upload a file to an S3 bucket
|
12 |
+
|
13 |
+
:param file_name: File to upload
|
14 |
+
:param bucket_name: Bucket to upload to
|
15 |
+
:param object_name: S3 object name. If not specified, file_name is used
|
16 |
+
:return: Public URL of the uploaded file
|
17 |
+
"""
|
18 |
+
# If S3 object_name was not specified, use file_name
|
19 |
+
if object_name is None:
|
20 |
+
object_name = file_name
|
21 |
+
|
22 |
+
# Upload the file
|
23 |
+
s3_client = boto3.client('s3', aws_access_key_id=setting.S3_ACCESS_KEY, aws_secret_access_key=setting.S3_SECERET_KEY)
|
24 |
+
try:
|
25 |
+
response = s3_client.upload_file(file_name, bucket_name, object_name)
|
26 |
+
except Exception as e:
|
27 |
+
print(f"Upload failed: {e}")
|
28 |
+
return None
|
29 |
+
|
30 |
+
# The URL will be of the form 'https://{bucket_name}.s3.amazonaws.com/{object_name}'
|
31 |
+
return f"https://{bucket_name}.s3.amazonaws.com/{object_name}"
|
32 |
+
|
33 |
+
def get_details(uploaded_file_url):
|
34 |
+
response = client.chat.completions.create(
|
35 |
+
model="gpt-4-vision-preview",
|
36 |
+
messages=[
|
37 |
+
{
|
38 |
+
"role": "user",
|
39 |
+
"content": [
|
40 |
+
{"type": "text", "text": setting.SMART_TEXT},
|
41 |
+
{
|
42 |
+
"type": "image_url",
|
43 |
+
"image_url": {
|
44 |
+
"url": uploaded_file_url,
|
45 |
+
},
|
46 |
+
},
|
47 |
+
],
|
48 |
+
}
|
49 |
+
],
|
50 |
+
max_tokens=300,
|
51 |
+
)
|
52 |
+
|
53 |
+
return(response.choices[0].message.content)
|
54 |
+
|
55 |
+
# object_name = secrets.token_hex(5)+'.jpg'
|
56 |
+
# uploaded_file_url = upload_to_s3('/Users/hacker/Desktop/Cactus/shrikant/ccd.jpg', setting.S3_BUCKET_NAME, object_name)
|
57 |
+
# print(get_details(uploaded_file_url))
|
58 |
+
|
59 |
+
def upload_and_process_image(image_path):
|
60 |
+
# Generate a unique object name
|
61 |
+
object_name = secrets.token_hex(5) + '.jpg'
|
62 |
+
|
63 |
+
# Upload to S3 and get the URL
|
64 |
+
uploaded_file_url = upload_to_s3(image_path, setting.S3_BUCKET_NAME, object_name)
|
65 |
+
|
66 |
+
# If the upload is successful, get details
|
67 |
+
if uploaded_file_url:
|
68 |
+
details = get_details(uploaded_file_url)
|
69 |
+
return details
|
70 |
+
else:
|
71 |
+
return "Failed to upload the image to S3."
|
72 |
+
|
73 |
+
# Gradio interface
|
74 |
+
iface = gr.Interface(
|
75 |
+
fn=upload_and_process_image,
|
76 |
+
inputs=gr.Image(type="filepath", label="Upload Image"),
|
77 |
+
outputs="text",
|
78 |
+
css="footer {visibility: hidden}",
|
79 |
+
title="AI Find",
|
80 |
+
description="Upload image to get details like number of fans, doors, eletrical sockets, ac vent and windows."
|
81 |
+
)
|
82 |
+
|
83 |
+
if __name__ == "__main__":
|
84 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
boto3
|
2 |
+
gradio
|
3 |
+
openai
|
setting.py
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
OPEN_AI_KEY = "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"
|
2 |
+
SMART_TEXT = "Analyze the image provided and list the number of fans, doors, eletrical sockets, ac vent and windows. Present your findings as a bulleted list."
|
3 |
+
S3_ACCESS_KEY="AKIAVLCKJKXQ5BPBRY7Q"
|
4 |
+
S3_SECERET_KEY="6rKUdJBz3wTJNvh4piqQkmnprEhHzx7KriKRYGin"
|
5 |
+
S3_BUCKET_NAME="camsense"
|