Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -17,8 +17,6 @@ download("en_core_web_sm")
|
|
17 |
# Load the model
|
18 |
nlp = spacy.load("en_core_web_sm")
|
19 |
|
20 |
-
# Your existing code here
|
21 |
-
|
22 |
# Download NLTK WordNet data
|
23 |
import nltk
|
24 |
nltk.download('wordnet')
|
@@ -73,29 +71,24 @@ with st.sidebar:
|
|
73 |
query = st.text_input("Search images by caption:")
|
74 |
|
75 |
# Right side for folder path input and displaying images
|
76 |
-
option = st.selectbox("Select input method:", ["
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
else:
|
95 |
-
if uploaded_file.name.lower().endswith(('png', 'jpg', 'jpeg')):
|
96 |
-
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(uploaded_file.name)[1])
|
97 |
-
temp_file.write(uploaded_file.read())
|
98 |
-
image_files.append(temp_file.name)
|
99 |
|
100 |
captions = {}
|
101 |
if st.button("Generate Captions", key='generate_captions'):
|
@@ -103,13 +96,7 @@ if st.button("Generate Captions", key='generate_captions'):
|
|
103 |
try:
|
104 |
image = Image.open(image_file)
|
105 |
caption = generate_caption(image)
|
106 |
-
|
107 |
-
captions[os.path.join(folder_path, os.path.basename(image_file))] = caption
|
108 |
-
else:
|
109 |
-
if image_file.startswith("uploaded_images"):
|
110 |
-
captions[image_file.replace("uploaded_images/", "")] = caption
|
111 |
-
else:
|
112 |
-
captions[os.path.basename(image_file)] = caption
|
113 |
except Exception as e:
|
114 |
st.error(f"Error processing {image_file}: {e}")
|
115 |
|
|
|
17 |
# Load the model
|
18 |
nlp = spacy.load("en_core_web_sm")
|
19 |
|
|
|
|
|
20 |
# Download NLTK WordNet data
|
21 |
import nltk
|
22 |
nltk.download('wordnet')
|
|
|
71 |
query = st.text_input("Search images by caption:")
|
72 |
|
73 |
# Right side for folder path input and displaying images
|
74 |
+
option = st.selectbox("Select input method:", ["Upload Images or Zip File"])
|
75 |
+
|
76 |
+
uploaded_files = st.file_uploader("Upload images or a zip file containing images:", type=['png', 'jpg', 'jpeg', 'zip'], accept_multiple_files=True)
|
77 |
+
image_files = []
|
78 |
+
if uploaded_files:
|
79 |
+
for uploaded_file in uploaded_files:
|
80 |
+
if uploaded_file.name.endswith('.zip'):
|
81 |
+
with zipfile.ZipFile(uploaded_file, 'r') as zip_ref:
|
82 |
+
temp_dir = tempfile.mkdtemp()
|
83 |
+
zip_ref.extractall(temp_dir)
|
84 |
+
for file in zip_ref.namelist():
|
85 |
+
if file.lower().endswith(('png', 'jpg', 'jpeg')):
|
86 |
+
image_files.append(os.path.join(temp_dir, file))
|
87 |
+
else:
|
88 |
+
if uploaded_file.name.lower().endswith(('png', 'jpg', 'jpeg')):
|
89 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(uploaded_file.name)[1])
|
90 |
+
temp_file.write(uploaded_file.read())
|
91 |
+
image_files.append(temp_file.name)
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
captions = {}
|
94 |
if st.button("Generate Captions", key='generate_captions'):
|
|
|
96 |
try:
|
97 |
image = Image.open(image_file)
|
98 |
caption = generate_caption(image)
|
99 |
+
captions[image_file] = caption
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
except Exception as e:
|
101 |
st.error(f"Error processing {image_file}: {e}")
|
102 |
|