Commit ·
e20283b
1
Parent(s): 2f5a914
Pexels: select FIRST most relevant photo instead of random
Browse files
modules/fact_image/services/fact_creator.py
CHANGED
|
@@ -163,21 +163,51 @@ class FactCreator:
|
|
| 163 |
width=1080,
|
| 164 |
height=1920
|
| 165 |
)
|
| 166 |
-
elif job["model"] == ImageModel.pexels
|
| 167 |
-
# Pexels:
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
)
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
else:
|
| 182 |
# Fallback to any available client
|
| 183 |
if self.nvidia:
|
|
|
|
| 163 |
width=1080,
|
| 164 |
height=1920
|
| 165 |
)
|
| 166 |
+
elif job["model"] == ImageModel.pexels:
|
| 167 |
+
# Pexels: Direct API call to get FIRST (most relevant) photo
|
| 168 |
+
import requests as pexels_requests
|
| 169 |
+
import os
|
| 170 |
+
pexels_key = os.getenv("PEXELS_API_KEY")
|
| 171 |
+
|
| 172 |
+
if not pexels_key:
|
| 173 |
+
raise Exception("PEXELS_API_KEY not configured")
|
| 174 |
+
|
| 175 |
+
logger.info(f"[{job_id}] Searching Pexels for: {job['image_prompt']}")
|
| 176 |
+
|
| 177 |
+
resp = pexels_requests.get(
|
| 178 |
+
"https://api.pexels.com/v1/search",
|
| 179 |
+
headers={"Authorization": pexels_key},
|
| 180 |
+
params={
|
| 181 |
+
"query": job["image_prompt"],
|
| 182 |
+
"orientation": "portrait",
|
| 183 |
+
"per_page": 5,
|
| 184 |
+
"size": "large"
|
| 185 |
+
},
|
| 186 |
+
timeout=15
|
| 187 |
)
|
| 188 |
+
|
| 189 |
+
if resp.status_code != 200:
|
| 190 |
+
raise Exception(f"Pexels API error: {resp.status_code}")
|
| 191 |
+
|
| 192 |
+
photos = resp.json().get("photos", [])
|
| 193 |
+
if not photos:
|
| 194 |
+
raise Exception(f"No Pexels photos found for: {job['image_prompt']}")
|
| 195 |
+
|
| 196 |
+
# Select FIRST (most relevant) photo, NOT random
|
| 197 |
+
photo = photos[0]
|
| 198 |
+
photo_url = photo.get("src", {}).get("original") or photo.get("src", {}).get("large")
|
| 199 |
+
|
| 200 |
+
if not photo_url:
|
| 201 |
+
raise Exception("No valid photo URL from Pexels")
|
| 202 |
+
|
| 203 |
+
logger.info(f"[{job_id}] Selected Pexels photo ID {photo['id']}")
|
| 204 |
+
|
| 205 |
+
# Download photo
|
| 206 |
+
img_resp = pexels_requests.get(photo_url, timeout=30)
|
| 207 |
+
img_resp.raise_for_status()
|
| 208 |
+
image_path.parent.mkdir(parents=True, exist_ok=True)
|
| 209 |
+
image_path.write_bytes(img_resp.content)
|
| 210 |
+
logger.info(f"[{job_id}] Downloaded Pexels photo")
|
| 211 |
else:
|
| 212 |
# Fallback to any available client
|
| 213 |
if self.nvidia:
|