Spaces:
Sleeping
Sleeping
Update upload_image_page.py
Browse files- upload_image_page.py +104 -88
upload_image_page.py
CHANGED
@@ -1,88 +1,104 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from pymongo import MongoClient
|
3 |
-
import os
|
4 |
-
from dotenv import load_dotenv
|
5 |
-
from helper.upload_file_to_s3 import upload_file
|
6 |
-
from helper.process_image import process_image_using_llm
|
7 |
-
from helper.create_embeddings import create_embedding
|
8 |
-
import time
|
9 |
-
|
10 |
-
# Load environment variables
|
11 |
-
load_dotenv()
|
12 |
-
AWS_ACCESS_KEY_ID = os.getenv("AWS_ACCESS_KEY_ID")
|
13 |
-
AWS_SECRET_ACCESS_KEY = os.getenv("AWS_SECRET_ACCESS_KEY")
|
14 |
-
AWS_BUCKET_NAME = os.getenv("AWS_BUCKET_NAME")
|
15 |
-
MONGO_URI = os.getenv("MONGO_URI")
|
16 |
-
DB_NAME = os.getenv("DB_NAME")
|
17 |
-
COLLECTION_NAME = os.getenv("COLLECTION_NAME")
|
18 |
-
COLLECTION_NAME2=os.getenv("COMPANY_COLLECTION_NAME")
|
19 |
-
|
20 |
-
mongo_client = MongoClient(MONGO_URI)
|
21 |
-
db = mongo_client[DB_NAME]
|
22 |
-
collection = db[COLLECTION_NAME]
|
23 |
-
collection2=db[COLLECTION_NAME2]
|
24 |
-
|
25 |
-
def upload():
|
26 |
-
if st.button("Back"):
|
27 |
-
st.session_state.page = "upload_main"
|
28 |
-
st.rerun()
|
29 |
-
|
30 |
-
# File uploader (image files only)
|
31 |
-
uploaded_image = st.file_uploader("Choose an image file to upload", type=["png", "jpg", "jpeg"],
|
32 |
-
accept_multiple_files=False)
|
33 |
-
|
34 |
-
# Fetch tags and categories from MongoDB
|
35 |
-
tags_doc = collection2.find_one({"type": "tags"})
|
36 |
-
categories_doc = collection2.find_one({"type": "categories"})
|
37 |
-
|
38 |
-
tags_options = tags_doc["tags"] if tags_doc and "tags" in tags_doc else []
|
39 |
-
categories_options = categories_doc["categories"] if categories_doc and "categories" in categories_doc else []
|
40 |
-
|
41 |
-
# Multi-select dropdowns for tags and categories
|
42 |
-
selected_tags = st.multiselect("Select Tags", options=tags_options)
|
43 |
-
selected_categories = st.multiselect("Select Categories", options=categories_options)
|
44 |
-
|
45 |
-
if uploaded_image and selected_tags and selected_categories:
|
46 |
-
flag=False
|
47 |
-
if st.button("Submit"):
|
48 |
-
|
49 |
-
with st.spinner(text="Uploading and Processing Image"):
|
50 |
-
# Upload file to S3
|
51 |
-
metadata = upload_file(uploaded_image,"Image")
|
52 |
-
if metadata:
|
53 |
-
object_url = metadata.get("object_url")
|
54 |
-
filename = metadata.get("name")
|
55 |
-
|
56 |
-
# Process image with LLM for description
|
57 |
-
llm_processed = process_image_using_llm(object_url)
|
58 |
-
if llm_processed:
|
59 |
-
# Create embedding with tags and categories in metadata
|
60 |
-
embedding_created = create_embedding(
|
61 |
-
object_url,
|
62 |
-
selected_tags,
|
63 |
-
selected_categories
|
64 |
-
)
|
65 |
-
if embedding_created:
|
66 |
-
# Save tags and categories to MongoDB document for the uploaded image
|
67 |
-
collection.update_one(
|
68 |
-
{"object_url": object_url},
|
69 |
-
{"$set": {
|
70 |
-
"tags": selected_tags,
|
71 |
-
"categories": selected_categories
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from pymongo import MongoClient
|
3 |
+
import os
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
from helper.upload_file_to_s3 import upload_file
|
6 |
+
from helper.process_image import process_image_using_llm
|
7 |
+
from helper.create_embeddings import create_embedding
|
8 |
+
import time
|
9 |
+
|
10 |
+
# Load environment variables
|
11 |
+
load_dotenv()
|
12 |
+
AWS_ACCESS_KEY_ID = os.getenv("AWS_ACCESS_KEY_ID")
|
13 |
+
AWS_SECRET_ACCESS_KEY = os.getenv("AWS_SECRET_ACCESS_KEY")
|
14 |
+
AWS_BUCKET_NAME = os.getenv("AWS_BUCKET_NAME")
|
15 |
+
MONGO_URI = os.getenv("MONGO_URI")
|
16 |
+
DB_NAME = os.getenv("DB_NAME")
|
17 |
+
COLLECTION_NAME = os.getenv("COLLECTION_NAME")
|
18 |
+
COLLECTION_NAME2=os.getenv("COMPANY_COLLECTION_NAME")
|
19 |
+
|
20 |
+
mongo_client = MongoClient(MONGO_URI)
|
21 |
+
db = mongo_client[DB_NAME]
|
22 |
+
collection = db[COLLECTION_NAME]
|
23 |
+
collection2=db[COLLECTION_NAME2]
|
24 |
+
|
25 |
+
def upload():
|
26 |
+
if st.button("Back"):
|
27 |
+
st.session_state.page = "upload_main"
|
28 |
+
st.rerun()
|
29 |
+
|
30 |
+
# File uploader (image files only)
|
31 |
+
uploaded_image = st.file_uploader("Choose an image file to upload", type=["png", "jpg", "jpeg"],
|
32 |
+
accept_multiple_files=False)
|
33 |
+
|
34 |
+
# Fetch tags and categories from MongoDB
|
35 |
+
tags_doc = collection2.find_one({"type": "tags"})
|
36 |
+
categories_doc = collection2.find_one({"type": "categories"})
|
37 |
+
|
38 |
+
tags_options = tags_doc["tags"] if tags_doc and "tags" in tags_doc else []
|
39 |
+
categories_options = categories_doc["categories"] if categories_doc and "categories" in categories_doc else []
|
40 |
+
|
41 |
+
# Multi-select dropdowns for tags and categories
|
42 |
+
selected_tags = st.multiselect("Select Tags", options=tags_options)
|
43 |
+
selected_categories = st.multiselect("Select Categories", options=categories_options)
|
44 |
+
|
45 |
+
if uploaded_image and selected_tags and selected_categories:
|
46 |
+
flag=False
|
47 |
+
if st.button("Submit"):
|
48 |
+
|
49 |
+
with st.spinner(text="Uploading and Processing Image"):
|
50 |
+
# Upload file to S3
|
51 |
+
metadata = upload_file(uploaded_image,"Image")
|
52 |
+
if metadata:
|
53 |
+
object_url = metadata.get("object_url")
|
54 |
+
filename = metadata.get("name")
|
55 |
+
|
56 |
+
# Process image with LLM for description
|
57 |
+
llm_processed = process_image_using_llm(object_url)
|
58 |
+
if llm_processed:
|
59 |
+
# Create embedding with tags and categories in metadata
|
60 |
+
embedding_created = create_embedding(
|
61 |
+
object_url,
|
62 |
+
selected_tags,
|
63 |
+
selected_categories
|
64 |
+
)
|
65 |
+
if embedding_created:
|
66 |
+
# Save tags and categories to MongoDB document for the uploaded image
|
67 |
+
collection.update_one(
|
68 |
+
{"object_url": object_url},
|
69 |
+
{"$set": {
|
70 |
+
"tags": selected_tags,
|
71 |
+
"categories": selected_categories,
|
72 |
+
"status":"processed"
|
73 |
+
}}
|
74 |
+
)
|
75 |
+
st.success("Image has been successfully uploaded and processed.")
|
76 |
+
flag=True
|
77 |
+
else:
|
78 |
+
st.error("Could not create embedding. Please try again.")
|
79 |
+
collection.update_one(
|
80 |
+
{"object_url": object_url},
|
81 |
+
{"$set": {
|
82 |
+
|
83 |
+
"status": "failed"
|
84 |
+
}}
|
85 |
+
)
|
86 |
+
else:
|
87 |
+
st.error("Could not process the image description. Please try again.")
|
88 |
+
st.error("Could not create embedding. Please try again.")
|
89 |
+
collection.update_one(
|
90 |
+
{"object_url": object_url},
|
91 |
+
{"$set": {
|
92 |
+
|
93 |
+
"status": "failed"
|
94 |
+
}}
|
95 |
+
)
|
96 |
+
else:
|
97 |
+
st.error("Could not upload the image. Please try again.")
|
98 |
+
|
99 |
+
|
100 |
+
if flag:
|
101 |
+
st.write("Redirecting to View Page to view all uploaded images")
|
102 |
+
time.sleep(2)
|
103 |
+
st.session_state.page = "view_image"
|
104 |
+
st.rerun()
|