Spaces:
Sleeping
Sleeping
create text_to_Image_Search.py
Browse files
pages/text_to_Image_Search.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
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 |
+
|
12 |
+
# Get API key from Hugging Face secrets
|
13 |
+
api_key = os.environ.get("PEXELS_API_KEY")
|
14 |
+
|
15 |
+
if not api_key:
|
16 |
+
st.error("Pexels API key not found. Please set it in Hugging Face secrets.")
|
17 |
+
st.stop()
|
18 |
+
|
19 |
+
def search_pexels(query, per_page=9, page=1):
|
20 |
+
headers = {"Authorization": api_key}
|
21 |
+
url = f"https://api.pexels.com/v1/search?query={query}&per_page={per_page}&page={page}"
|
22 |
+
try:
|
23 |
+
response = requests.get(url, headers=headers, timeout=10)
|
24 |
+
response.raise_for_status()
|
25 |
+
return response.json()
|
26 |
+
except requests.exceptions.RequestException as e:
|
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 |
+
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")
|
39 |
+
|
40 |
+
# Display in a grid
|
41 |
+
cols = st.columns(3)
|
42 |
+
for idx, photo in enumerate(results['photos']):
|
43 |
+
with cols[idx % 3]:
|
44 |
+
# Display the image
|
45 |
+
try:
|
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 |
+
# Display attribution
|
52 |
+
with st.expander("ℹ️ Attribution"):
|
53 |
+
st.markdown(f"""
|
54 |
+
**Photographer:** [{photo['photographer']}]({photo['photographer_url']})
|
55 |
+
**Pexels Profile:** [{photo['photographer_url']}]({photo['photographer_url']})
|
56 |
+
**License:** [Free to use](https://www.pexels.com/license/)
|
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>
|
66 |
+
.stImage>img {
|
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);
|
73 |
+
}
|
74 |
+
[data-testid="stExpander"] {
|
75 |
+
background-color: #f8f9fa;
|
76 |
+
border-radius: 8px;
|
77 |
+
padding: 10px;
|
78 |
+
}
|
79 |
+
</style>
|
80 |
+
""", unsafe_allow_html=True)
|