YashMK89 commited on
Commit
5c16168
Β·
verified Β·
1 Parent(s): a1997f0

Update pages/4_πŸ–Ό_text_to_Image_Search.py

Browse files
pages/4_πŸ–Ό_text_to_Image_Search.py CHANGED
@@ -27,14 +27,16 @@ def search_pexels(query, per_page=9, page=1):
27
  st.error(f"Error searching Pexels: {str(e)}")
28
  return None
29
 
30
- # Search interface
31
- search_query = st.text_input("πŸ” Search for images", placeholder="Enter search term...")
32
-
33
- # Initialize session state for pagination and selected image
34
  if 'page' not in st.session_state:
35
  st.session_state.page = 1
36
  if 'selected_image' not in st.session_state:
37
  st.session_state.selected_image = None
 
 
 
 
 
38
 
39
  if search_query:
40
  with st.spinner("Searching Pexels..."):
@@ -47,32 +49,23 @@ if search_query:
47
  cols = st.columns(3)
48
  for idx, photo in enumerate(results['photos']):
49
  with cols[idx % 3]:
50
- # Display the image
51
  try:
52
  img_url = photo['src']['medium']
53
  response = requests.get(img_url, timeout=10)
54
  img = Image.open(BytesIO(response.content))
55
 
56
  # Make image clickable
57
- if st.image(
58
  img,
59
  caption=f"{photo['photographer']} - {photo['alt'][:50]}...",
60
  use_container_width=True
61
- ):
62
- pass
63
-
64
- # Download button
65
- with st.expander("βš™οΈ Options"):
66
- st.download_button(
67
- label="Download Original",
68
- data=requests.get(photo['src']['original']).content,
69
- file_name=f"pexels-{photo['id']}.jpg",
70
- mime="image/jpeg",
71
- key=f"dl_{photo['id']}"
72
- )
73
- if st.button("View Details", key=f"view_{photo['id']}"):
74
- st.session_state.selected_image = photo
75
 
 
 
 
 
 
76
  except Exception as e:
77
  st.error(f"Error loading image: {str(e)}")
78
 
@@ -94,46 +87,97 @@ if search_query:
94
  elif results:
95
  st.warning("No results found")
96
 
97
- # Sidebar for selected image details
98
- if st.session_state.selected_image:
99
  photo = st.session_state.selected_image
100
- with st.sidebar:
101
- st.title("Image Details")
102
-
103
- # Display original image
104
- try:
105
- img_url = photo['src']['original']
106
- response = requests.get(img_url, timeout=10)
107
- img = Image.open(BytesIO(response.content))
108
- st.image(img, use_container_width=True)
109
- except Exception as e:
110
- st.error(f"Error loading original image: {str(e)}")
111
-
112
- # Attribution details
113
- st.subheader("Attribution Information")
114
- st.markdown(f"""
115
- **Photographer:** [{photo['photographer']}]({photo['photographer_url']})
116
- **Pexels Profile:** [View Profile]({photo['photographer_url']})
117
- **Original Photo:** [View on Pexels]({photo['url']})
118
- **Image ID:** {photo['id']}
119
- **Dimensions:** {photo['width']} Γ— {photo['height']}
120
- **License:** [Free to use](https://www.pexels.com/license/)
121
- """)
122
-
123
- # Download original in sidebar
124
- st.download_button(
125
- label="Download Original Quality",
126
- data=requests.get(photo['src']['original']).content,
127
- file_name=f"pexels-{photo['id']}-original.jpg",
128
- mime="image/jpeg",
129
- use_container_width=True
130
- )
131
-
132
- if st.button("Close", use_container_width=True):
133
- st.session_state.selected_image = None
134
- st.rerun()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
 
136
- # Add some CSS styling
137
  st.markdown("""
138
  <style>
139
  .stImage>img {
@@ -157,9 +201,5 @@ st.markdown("""
157
  .stButton>button {
158
  width: 100%;
159
  }
160
- [data-testid="stSidebar"] {
161
- background-color: #f8f9fa;
162
- padding: 20px;
163
- }
164
  </style>
165
  """, unsafe_allow_html=True)
 
27
  st.error(f"Error searching Pexels: {str(e)}")
28
  return None
29
 
30
+ # Initialize session state
 
 
 
31
  if 'page' not in st.session_state:
32
  st.session_state.page = 1
33
  if 'selected_image' not in st.session_state:
34
  st.session_state.selected_image = None
35
+ if 'show_modal' not in st.session_state:
36
+ st.session_state.show_modal = False
37
+
38
+ # Search interface
39
+ search_query = st.text_input("πŸ” Search for images", placeholder="Enter search term...", key="search_input")
40
 
41
  if search_query:
42
  with st.spinner("Searching Pexels..."):
 
49
  cols = st.columns(3)
50
  for idx, photo in enumerate(results['photos']):
51
  with cols[idx % 3]:
 
52
  try:
53
  img_url = photo['src']['medium']
54
  response = requests.get(img_url, timeout=10)
55
  img = Image.open(BytesIO(response.content))
56
 
57
  # Make image clickable
58
+ clicked = st.image(
59
  img,
60
  caption=f"{photo['photographer']} - {photo['alt'][:50]}...",
61
  use_container_width=True
62
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
+ if clicked:
65
+ st.session_state.selected_image = photo
66
+ st.session_state.show_modal = True
67
+ st.rerun()
68
+
69
  except Exception as e:
70
  st.error(f"Error loading image: {str(e)}")
71
 
 
87
  elif results:
88
  st.warning("No results found")
89
 
90
+ # Modal for image details
91
+ if st.session_state.show_modal and st.session_state.selected_image:
92
  photo = st.session_state.selected_image
93
+
94
+ # Custom CSS for the modal
95
+ st.markdown("""
96
+ <style>
97
+ .modal {
98
+ position: fixed;
99
+ top: 50%;
100
+ left: 50%;
101
+ transform: translate(-50%, -50%);
102
+ width: 80%;
103
+ max-width: 800px;
104
+ background: white;
105
+ padding: 20px;
106
+ border-radius: 10px;
107
+ box-shadow: 0 4px 20px rgba(0,0,0,0.3);
108
+ z-index: 1000;
109
+ }
110
+ .modal-overlay {
111
+ position: fixed;
112
+ top: 0;
113
+ left: 0;
114
+ right: 0;
115
+ bottom: 0;
116
+ background: rgba(0,0,0,0.5);
117
+ z-index: 999;
118
+ }
119
+ .close-btn {
120
+ position: absolute;
121
+ top: 10px;
122
+ right: 10px;
123
+ background: #ff4b4b;
124
+ color: white;
125
+ border: none;
126
+ border-radius: 50%;
127
+ width: 30px;
128
+ height: 30px;
129
+ font-size: 16px;
130
+ cursor: pointer;
131
+ }
132
+ .download-btn {
133
+ background: #4CAF50;
134
+ color: white;
135
+ padding: 10px 20px;
136
+ border: none;
137
+ border-radius: 5px;
138
+ cursor: pointer;
139
+ font-size: 16px;
140
+ margin-top: 10px;
141
+ }
142
+ </style>
143
+ """, unsafe_allow_html=True)
144
+
145
+ # Modal content
146
+ st.markdown(f"""
147
+ <div class="modal-overlay" onclick="document.querySelector('div.modal-overlay').style.display = 'none'"></div>
148
+ <div class="modal">
149
+ <button class="close-btn" onclick="document.querySelector('div.modal-overlay').style.display = 'none'">Γ—</button>
150
+ <h2>{photo['alt']}</h2>
151
+ <div style="display: flex; gap: 20px;">
152
+ <div style="flex: 2;">
153
+ <img src="{photo['src']['large']}" style="width: 100%; border-radius: 8px;"/>
154
+ </div>
155
+ <div style="flex: 1;">
156
+ <h3>Photographer</h3>
157
+ <p><a href="{photo['photographer_url']}" target="_blank">{photo['photographer']}</a></p>
158
+
159
+ <h3>Image Details</h3>
160
+ <p>Size: {photo['width']} Γ— {photo['height']}</p>
161
+ <p>ID: {photo['id']}</p>
162
+
163
+ <a href="{photo['src']['original']}" download="pexels-{photo['id']}.jpg" class="download-btn">
164
+ Download Original
165
+ </a>
166
+
167
+ <p style="margin-top: 20px;"><small>
168
+ <a href="https://www.pexels.com/license/" target="_blank">License Information</a>
169
+ </small></p>
170
+ </div>
171
+ </div>
172
+ </div>
173
+ """, unsafe_allow_html=True)
174
+
175
+ # Handle modal close
176
+ if st.button("Close Modal", key="close_modal"):
177
+ st.session_state.show_modal = False
178
+ st.rerun()
179
 
180
+ # Add some CSS styling for the main content
181
  st.markdown("""
182
  <style>
183
  .stImage>img {
 
201
  .stButton>button {
202
  width: 100%;
203
  }
 
 
 
 
204
  </style>
205
  """, unsafe_allow_html=True)