Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,7 @@ import requests
|
|
| 8 |
import gradio as gr
|
| 9 |
|
| 10 |
PRINTIFY_BASE = "https://api.printify.com"
|
| 11 |
-
DEFAULT_BASE_PRICE =
|
| 12 |
|
| 13 |
|
| 14 |
def _now() -> str:
|
|
@@ -192,6 +192,27 @@ def run(currency: str) -> Generator[Tuple[str, str], None, None]:
|
|
| 192 |
_log(logs, f"SHOP_LIST {json.dumps(shops)}")
|
| 193 |
yield flush()
|
| 194 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
fd, path = tempfile.mkstemp(prefix="shops_", suffix=".json")
|
| 196 |
os.close(fd)
|
| 197 |
with open(path, "w", encoding="utf-8") as f:
|
|
@@ -212,14 +233,65 @@ def run(currency: str) -> Generator[Tuple[str, str], None, None]:
|
|
| 212 |
yield flush()
|
| 213 |
|
| 214 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
with gr.Blocks(title="Printify Catalog Probe") as demo:
|
| 216 |
gr.Markdown("Extract and normalize ONE Printify blueprint/provider into a structured JSON object.")
|
| 217 |
|
| 218 |
currency = gr.Textbox(label="Currency", value="USD")
|
| 219 |
btn = gr.Button("Run")
|
|
|
|
| 220 |
logs = gr.Textbox(label="Logs", lines=18)
|
| 221 |
out = gr.Textbox(label="Output JSON", lines=18)
|
| 222 |
|
| 223 |
btn.click(run, inputs=[currency], outputs=[logs, out])
|
|
|
|
| 224 |
|
| 225 |
demo.queue().launch()
|
|
|
|
| 8 |
import gradio as gr
|
| 9 |
|
| 10 |
PRINTIFY_BASE = "https://api.printify.com"
|
| 11 |
+
DEFAULT_BASE_PRICE = 0.00
|
| 12 |
|
| 13 |
|
| 14 |
def _now() -> str:
|
|
|
|
| 192 |
_log(logs, f"SHOP_LIST {json.dumps(shops)}")
|
| 193 |
yield flush()
|
| 194 |
|
| 195 |
+
uploads = _req("GET", "/v1/uploads.json?limit=1")
|
| 196 |
+
latest = uploads[0] if isinstance(uploads, list) and uploads else None
|
| 197 |
+
|
| 198 |
+
if latest:
|
| 199 |
+
_log(
|
| 200 |
+
logs,
|
| 201 |
+
f"LATEST_UPLOAD id={latest.get('id')} "
|
| 202 |
+
f"file={latest.get('file_name')} "
|
| 203 |
+
f"w={latest.get('width')} h={latest.get('height')}"
|
| 204 |
+
)
|
| 205 |
+
result["latestUpload"] = {
|
| 206 |
+
"id": latest.get("id"),
|
| 207 |
+
"file_name": latest.get("file_name"),
|
| 208 |
+
"width": latest.get("width"),
|
| 209 |
+
"height": latest.get("height"),
|
| 210 |
+
}
|
| 211 |
+
else:
|
| 212 |
+
_log(logs, "LATEST_UPLOAD none found")
|
| 213 |
+
|
| 214 |
+
yield flush()
|
| 215 |
+
|
| 216 |
fd, path = tempfile.mkstemp(prefix="shops_", suffix=".json")
|
| 217 |
os.close(fd)
|
| 218 |
with open(path, "w", encoding="utf-8") as f:
|
|
|
|
| 233 |
yield flush()
|
| 234 |
|
| 235 |
|
| 236 |
+
def phase_b(currency: str) -> Generator[Tuple[str, str], None, None]:
|
| 237 |
+
logs: List[str] = []
|
| 238 |
+
result: Dict[str, Any] = {}
|
| 239 |
+
|
| 240 |
+
def flush():
|
| 241 |
+
return "\n".join(logs), json.dumps(result, indent=2)
|
| 242 |
+
|
| 243 |
+
try:
|
| 244 |
+
_log(logs, "PHASE_B_START")
|
| 245 |
+
yield flush()
|
| 246 |
+
|
| 247 |
+
shops = _req("GET", "/v1/shops.json")
|
| 248 |
+
_log(logs, f"SHOP_LIST {json.dumps(shops)}")
|
| 249 |
+
yield flush()
|
| 250 |
+
|
| 251 |
+
uploads = _req("GET", "/v1/uploads.json?limit=1")
|
| 252 |
+
latest = uploads[0] if isinstance(uploads, list) and uploads else None
|
| 253 |
+
|
| 254 |
+
if latest:
|
| 255 |
+
_log(
|
| 256 |
+
logs,
|
| 257 |
+
f"LATEST_UPLOAD id={latest.get('id')} "
|
| 258 |
+
f"file={latest.get('file_name')} "
|
| 259 |
+
f"w={latest.get('width')} h={latest.get('height')}"
|
| 260 |
+
)
|
| 261 |
+
result["latestUpload"] = {
|
| 262 |
+
"id": latest.get("id"),
|
| 263 |
+
"file_name": latest.get("file_name"),
|
| 264 |
+
"width": latest.get("width"),
|
| 265 |
+
"height": latest.get("height"),
|
| 266 |
+
}
|
| 267 |
+
else:
|
| 268 |
+
_log(logs, "LATEST_UPLOAD none found")
|
| 269 |
+
|
| 270 |
+
yield flush()
|
| 271 |
+
|
| 272 |
+
blob = _find_first_valid_pair(logs)
|
| 273 |
+
yield flush()
|
| 274 |
+
|
| 275 |
+
result = _build_product(blob, currency or "USD", logs)
|
| 276 |
+
_log(logs, "PHASE_B_READY")
|
| 277 |
+
yield flush()
|
| 278 |
+
|
| 279 |
+
except Exception as e:
|
| 280 |
+
_log(logs, f"ERROR: {e}")
|
| 281 |
+
result = {"error": str(e)}
|
| 282 |
+
yield flush()
|
| 283 |
+
|
| 284 |
+
|
| 285 |
with gr.Blocks(title="Printify Catalog Probe") as demo:
|
| 286 |
gr.Markdown("Extract and normalize ONE Printify blueprint/provider into a structured JSON object.")
|
| 287 |
|
| 288 |
currency = gr.Textbox(label="Currency", value="USD")
|
| 289 |
btn = gr.Button("Run")
|
| 290 |
+
btn_b = gr.Button("Phase B (Test)")
|
| 291 |
logs = gr.Textbox(label="Logs", lines=18)
|
| 292 |
out = gr.Textbox(label="Output JSON", lines=18)
|
| 293 |
|
| 294 |
btn.click(run, inputs=[currency], outputs=[logs, out])
|
| 295 |
+
btn_b.click(phase_b, inputs=[currency], outputs=[logs, out])
|
| 296 |
|
| 297 |
demo.queue().launch()
|