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

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

Browse files
Files changed (1) hide show
  1. pages/4_πŸ–Ό_text_to_Image_Search.py +104 -128
pages/4_πŸ–Ό_text_to_Image_Search.py CHANGED
@@ -4,8 +4,58 @@ from PIL import Image
4
  from io import BytesIO
5
  import os
6
 
7
- # Set up the page
8
  st.set_page_config(page_title="Pexels Image Search", layout="wide")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  st.title("πŸ“· Pexels Image Search with Attribution")
10
  st.write("Search for images and properly credit the photographers")
11
 
@@ -27,16 +77,14 @@ def search_pexels(query, per_page=9, page=1):
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..."):
@@ -54,18 +102,26 @@ if search_query:
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,119 +143,39 @@ if search_query:
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 {
184
- border-radius: 8px;
185
- box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.1);
186
- transition: transform 0.3s;
187
- cursor: pointer;
188
- }
189
- .stImage>img:hover {
190
- transform: scale(1.02);
191
- }
192
- [data-testid="stExpander"] {
193
- background-color: #f8f9fa;
194
- border-radius: 8px;
195
- padding: 10px;
196
- margin-top: 5px;
197
- }
198
- .st-emotion-cache-16txtl3 {
199
- padding: 2rem 1rem;
200
- }
201
- .stButton>button {
202
- width: 100%;
203
- }
204
- </style>
205
- """, unsafe_allow_html=True)
 
4
  from io import BytesIO
5
  import os
6
 
7
+ # Set up the page with custom CSS to preserve background
8
  st.set_page_config(page_title="Pexels Image Search", layout="wide")
9
+
10
+ # Custom CSS to maintain background
11
+ st.markdown("""
12
+ <style>
13
+ /* Main container styling */
14
+ .main .block-container {
15
+ background-color: transparent !important;
16
+ padding-top: 2rem;
17
+ }
18
+
19
+ /* Image styling */
20
+ .stImage>img {
21
+ border-radius: 8px;
22
+ box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.1);
23
+ transition: transform 0.3s;
24
+ cursor: pointer;
25
+ }
26
+ .stImage>img:hover {
27
+ transform: scale(1.02);
28
+ }
29
+
30
+ /* Expander styling */
31
+ [data-testid="stExpander"] {
32
+ background-color: rgba(248, 249, 250, 0.9);
33
+ border-radius: 8px;
34
+ padding: 10px;
35
+ margin-top: 5px;
36
+ }
37
+
38
+ /* Sidebar styling - make it blend with background */
39
+ [data-testid="stSidebar"] {
40
+ background-color: rgba(248, 249, 250, 0.9) !important;
41
+ padding: 20px;
42
+ border-radius: 10px;
43
+ box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.1);
44
+ }
45
+
46
+ /* Title styling */
47
+ h1, h2, h3 {
48
+ color: inherit !important;
49
+ }
50
+
51
+ /* Button styling */
52
+ .stButton>button {
53
+ width: 100%;
54
+ border: 1px solid #ddd !important;
55
+ }
56
+ </style>
57
+ """, unsafe_allow_html=True)
58
+
59
  st.title("πŸ“· Pexels Image Search with Attribution")
60
  st.write("Search for images and properly credit the photographers")
61
 
 
77
  st.error(f"Error searching Pexels: {str(e)}")
78
  return None
79
 
80
+ # Search interface
81
+ search_query = st.text_input("πŸ” Search for images", placeholder="Enter search term...")
82
+
83
  # Initialize session state
84
  if 'page' not in st.session_state:
85
  st.session_state.page = 1
86
  if 'selected_image' not in st.session_state:
87
  st.session_state.selected_image = None
 
 
 
 
 
88
 
89
  if search_query:
90
  with st.spinner("Searching Pexels..."):
 
102
  response = requests.get(img_url, timeout=10)
103
  img = Image.open(BytesIO(response.content))
104
 
105
+ # Display clickable image
106
+ st.image(
107
  img,
108
  caption=f"{photo['photographer']} - {photo['alt'][:50]}...",
109
  use_container_width=True
110
  )
111
 
112
+ # Options expander
113
+ with st.expander("βš™οΈ Options"):
114
+ st.download_button(
115
+ label="Download Original",
116
+ data=requests.get(photo['src']['original']).content,
117
+ file_name=f"pexels-{photo['id']}.jpg",
118
+ mime="image/jpeg",
119
+ key=f"dl_{photo['id']}"
120
+ )
121
+ if st.button("View Details", key=f"view_{photo['id']}"):
122
+ st.session_state.selected_image = photo
123
+ st.rerun()
124
+
125
  except Exception as e:
126
  st.error(f"Error loading image: {str(e)}")
127
 
 
143
  elif results:
144
  st.warning("No results found")
145
 
146
+ # Image details sidebar (now more transparent)
147
+ if st.session_state.selected_image:
148
  photo = st.session_state.selected_image
149
+ with st.sidebar:
150
+ st.title("Image Details")
151
+
152
+ try:
153
+ img_url = photo['src']['large'] # Using large instead of original for faster loading
154
+ response = requests.get(img_url, timeout=10)
155
+ img = Image.open(BytesIO(response.content))
156
+ st.image(img, use_container_width=True)
157
+ except Exception as e:
158
+ st.error(f"Error loading image: {str(e)}")
159
+
160
+ # Attribution details
161
+ st.subheader("Attribution")
162
+ st.markdown(f"""
163
+ **Photographer:** [{photo['photographer']}]({photo['photographer_url']})
164
+ **Profile:** [View on Pexels]({photo['photographer_url']})
165
+ **Photo:** [View Original]({photo['url']})
166
+ **Size:** {photo['width']} Γ— {photo['height']}
167
+ **License:** [Free to use](https://www.pexels.com/license/)
168
+ """)
169
+
170
+ # Download button
171
+ st.download_button(
172
+ label="Download Original Quality",
173
+ data=requests.get(photo['src']['original']).content,
174
+ file_name=f"pexels-{photo['id']}-original.jpg",
175
+ mime="image/jpeg",
176
+ use_container_width=True
177
+ )
178
+
179
+ if st.button("Close Details", use_container_width=True):
180
+ st.session_state.selected_image = None
181
+ st.rerun()