Spaces:
Sleeping
Sleeping
Rename pages/text_to_Image_Search.py to pages/4_๐ผ_text_to_Image_Search.py
Browse files
pages/{text_to_Image_Search.py โ 4_๐ผ_text_to_Image_Search.py}
RENAMED
@@ -30,9 +30,15 @@ def search_pexels(query, per_page=9, page=1):
|
|
30 |
# Search interface
|
31 |
search_query = st.text_input("๐ Search for images", placeholder="Enter search term...")
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
if search_query:
|
34 |
with st.spinner("Searching Pexels..."):
|
35 |
-
results = search_pexels(search_query)
|
36 |
|
37 |
if results and 'photos' in results:
|
38 |
st.success(f"Found {results['total_results']} results")
|
@@ -46,20 +52,87 @@ if search_query:
|
|
46 |
img_url = photo['src']['medium']
|
47 |
response = requests.get(img_url, timeout=10)
|
48 |
img = Image.open(BytesIO(response.content))
|
49 |
-
st.image(img, caption=photo['alt'], use_column_width=True)
|
50 |
|
51 |
-
#
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
except Exception as e:
|
59 |
st.error(f"Error loading image: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
elif results:
|
61 |
st.warning("No results found")
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
# Add some CSS styling
|
64 |
st.markdown("""
|
65 |
<style>
|
@@ -67,6 +140,7 @@ st.markdown("""
|
|
67 |
border-radius: 8px;
|
68 |
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.1);
|
69 |
transition: transform 0.3s;
|
|
|
70 |
}
|
71 |
.stImage>img:hover {
|
72 |
transform: scale(1.02);
|
@@ -75,6 +149,17 @@ st.markdown("""
|
|
75 |
background-color: #f8f9fa;
|
76 |
border-radius: 8px;
|
77 |
padding: 10px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
}
|
79 |
</style>
|
80 |
""", unsafe_allow_html=True)
|
|
|
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..."):
|
41 |
+
results = search_pexels(search_query, page=st.session_state.page)
|
42 |
|
43 |
if results and 'photos' in results:
|
44 |
st.success(f"Found {results['total_results']} results")
|
|
|
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 |
+
|
79 |
+
# Pagination controls
|
80 |
+
col1, col2, col3 = st.columns([1, 2, 1])
|
81 |
+
with col1:
|
82 |
+
if st.session_state.page > 1:
|
83 |
+
if st.button("โฌ
๏ธ Previous Page"):
|
84 |
+
st.session_state.page -= 1
|
85 |
+
st.rerun()
|
86 |
+
with col3:
|
87 |
+
if results['total_results'] > st.session_state.page * 9:
|
88 |
+
if st.button("Next Page โก๏ธ"):
|
89 |
+
st.session_state.page += 1
|
90 |
+
st.rerun()
|
91 |
+
with col2:
|
92 |
+
st.write(f"Page {st.session_state.page} of {(results['total_results']//9)+1}")
|
93 |
+
|
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>
|
|
|
140 |
border-radius: 8px;
|
141 |
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.1);
|
142 |
transition: transform 0.3s;
|
143 |
+
cursor: pointer;
|
144 |
}
|
145 |
.stImage>img:hover {
|
146 |
transform: scale(1.02);
|
|
|
149 |
background-color: #f8f9fa;
|
150 |
border-radius: 8px;
|
151 |
padding: 10px;
|
152 |
+
margin-top: 5px;
|
153 |
+
}
|
154 |
+
.st-emotion-cache-16txtl3 {
|
155 |
+
padding: 2rem 1rem;
|
156 |
+
}
|
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)
|