from __future__ import annotations import csv import html import tempfile from typing import Any, Dict, List, Optional, Tuple import gradio as gr from tool import ( DEFAULT_PRODUCT_URL, DEFAULT_SUBCATEGORY_URL, MAX_SUBCATEGORIES, SCRAPE_DO_SIGNUP_URL, ScrapeDoZeptoTool, ) # ZeroGPU hardware refuses to keep a Space alive unless it finds at least one # @spaces.GPU function at startup. This app is pure HTTP and never touches a GPU, # so the probe below exists only to satisfy that check. It is never called. try: import spaces @spaces.GPU(duration=1) def _zerogpu_probe() -> str: return "ok" except ImportError: pass NO_TOKEN_MD = ( "### Paste your Scrape.do API token above\n\n" f"No token yet? [Create a free account]({SCRAPE_DO_SIGNUP_URL}) — " "1,000 free credits a month, no credit card. Every Zepto page costs 10 credits." ) # --------------------------------------------------------------------- helpers def _esc(value: Any) -> str: return html.escape("" if value is None else str(value), quote=True) def _safe_link(url: Any) -> str: url = str(url or "") return url if url.startswith(("http://", "https://")) else "" def _num(value: Any) -> Optional[float]: if isinstance(value, bool): return None try: return float(value) except (TypeError, ValueError): return None def _inr(value: Any) -> str: number = _num(value) if number is None: return "" return f"₹{number:,.0f}" if number == int(number) else f"₹{number:,.2f}" def _fmt_int(value: Any) -> str: number = _num(value) return f"{int(number):,}" if number is not None else _esc(value) def _tool_or_none(token: str) -> Optional[ScrapeDoZeptoTool]: if not token or not token.strip(): return None return ScrapeDoZeptoTool(api_token=token.strip()) def _write_csv(rows: List[Dict[str, Any]], prefix: str) -> Optional[str]: if not rows: return None fieldnames: List[str] = [] for row in rows: for key in row: if key not in fieldnames: fieldnames.append(key) handle = tempfile.NamedTemporaryFile( mode="w", suffix=".csv", prefix=f"{prefix}-", delete=False, newline="", encoding="utf-8-sig", ) writer = csv.DictWriter(handle, fieldnames=fieldnames) writer.writeheader() for row in rows: writer.writerow({key: row.get(key, "") for key in fieldnames}) handle.close() return handle.name def _error_md(data: Dict[str, Any]) -> str: out = f"**That didn't go through.**\n\n{data.get('errorMessage') or 'Unknown error'}" if data.get("hint"): out += f"\n\n{data['hint']}" return out def _stars(rating: Any, count: Any) -> str: value = _num(rating) if value is None: return '
No ratings yet
' count_html = f' ({_fmt_int(count)})' if count not in (None, "") else "" return f'
★ {value:.1f}{count_html}
' def _stock_html(in_stock: Any, qty: Any) -> str: if not in_stock: return 'Out of stock' units = _num(qty) if units is None: return 'In stock' return f'{int(units)} in stock' BOLT_SVG = ( '' ) # ---------------------------------------------------------- category results def render_listing(data: Dict[str, Any], hide_out_of_stock: bool) -> str: rows = data.get("products") or [] shown = [r for r in rows if not (hide_out_of_stock and not r.get("in_stock"))] cards: List[str] = [] for item in shown: link = _safe_link(item.get("url")) name = _esc(item.get("name")) title = ( f'{name}' if link else f'{name}' ) price, mrp = _num(item.get("price_inr")), _num(item.get("mrp_inr")) discount = item.get("discount_pct") or 0 price_html = ( f'
{_esc(_inr(price))}' + (f' {_esc(_inr(mrp))}' if mrp and price is not None and mrp > price else "") + "
" if price is not None else '
No price
' ) cards.append( f'
' + (f'{_esc(discount)}% OFF' if discount else "") + f'
{_esc(item.get("pack_size"))}
' f"{title}" f'
{_esc(item.get("brand"))}
' f"{price_html}" f"{_stars(item.get('rating'), item.get('rating_count'))}" f'
{_stock_html(item.get("in_stock"), item.get("available_qty"))}
' f'
{_esc(item.get("subcategory"))}
' "
" ) if not cards: cards.append('
No products came back for this subcategory.
') store = next((r.get("store_id") for r in rows if r.get("store_id")), None) banner = ( f'
Prices and stock from Zepto dark store {_esc(store)} ' "(Zepto's default store, east Bengaluru)
" if store else "" ) return f'
{banner}
{"".join(cards)}
' LISTING_HEADERS = ["Product", "Brand", "Pack", "Price ₹", "MRP ₹", "Off %", "In stock", "Units", "Rating", "Subcategory"] # ------------------------------------------------------------- product page def render_product(data: Dict[str, Any]) -> str: p = data.get("product") or {} images = [src for src in p.get("images") or [] if _safe_link(src)] main = ( f'" if images else "" ) thumbs = "".join( f'" for src in images[1:6] ) facts = [ ("Seller", p.get("seller")), ("Shelf life", p.get("shelf_life")), ("FSSAI licence", p.get("fssai_licence")), ("Country of origin", p.get("country_of_origin")), ("Pack size", p.get("pack_size")), ("MRP", _inr(p.get("mrp_inr"))), ("Max per order", p.get("max_per_order")), ("Category", p.get("category")), ] facts_html = "".join( f'
{_esc(k)}
{_esc(v)}
' for k, v in facts if v not in (None, "") ) link = _safe_link(p.get("url")) button = ( f'View on Zepto' if link else "" ) desc = _esc(p.get("description") or "") return ( '
' f'
{main}
{thumbs}
' '
' f'
{_esc(p.get("brand"))}
' f'
{_esc(p.get("name"))}
' f"{_stars(p.get('rating'), p.get('rating_count'))}" f'
{facts_html}
{button}' "
" + (f'
Description
{desc}
' if desc else "") + "
" ) # ---------------------------------------------------------------- handlers def _picker(choices: List[Tuple[str, str]]) -> Any: return gr.Dropdown(choices=choices, value=None, visible=bool(choices)) def run_category( token: str, url: str, walk: bool, max_subs: float, hide_oos: bool ) -> Tuple[str, str, List[List[Any]], Dict[str, Any], Optional[str], Any]: tool = _tool_or_none(token) if tool is None: return NO_TOKEN_MD, "", [], {}, None, _picker([]) if not url or not url.strip(): return "Paste a Zepto subcategory link.", "", [], {}, None, _picker([]) data = tool.category(url, int(max_subs or 1) if walk else 1) if data.get("status") != "success": return _error_md(data), "", [], data, None, _picker([]) rows = data.get("products") or [] oos = sum(1 for r in rows if not r.get("in_stock")) on_sale = sum(1 for r in rows if (r.get("discount_pct") or 0) > 0) deepest = max((r.get("discount_pct") or 0 for r in rows), default=0) parts = [f"**{len(rows)}** products", f"**{oos}** out of stock", f"**{on_sale}** below MRP"] if deepest: parts.append(f"deepest discount **{deepest}%**") summary = " · ".join(parts) pages = data.get("subcategories_read") or [] page_bits = [ f"{p.get('subcategory')}: {p.get('products')}" if "error" not in p else f"{p.get('url')}: failed" for p in pages ] summary += ( f"\n\nRead {len(pages)} of {data.get('subcategories_found')} subcategories ({', '.join(page_bits)}) · " f"{data.get('credits_used')} credits used · Zepto shows at most 32 products per subcategory." ) table = [ [ (r.get("name") or "")[:80], r.get("brand"), r.get("pack_size"), r.get("price_inr"), r.get("mrp_inr"), r.get("discount_pct"), "yes" if r.get("in_stock") else "no", r.get("available_qty"), r.get("rating"), r.get("subcategory"), ] for r in rows ] choices = [ (f"{str(r.get('name') or '')[:70]} — {_inr(r.get('price_inr')) or 'no price'}", r["url"]) for r in rows if r.get("url") ] csv_path = _write_csv(rows, "zepto-category") return summary, render_listing(data, bool(hide_oos)), table, data, csv_path, _picker(choices) def send_to_product(picked: Optional[str], current: str) -> Tuple[str, str]: if not picked: return current, "" return picked, "Sent. Open the **Product details** tab and press the button." def run_product(token: str, url: str) -> Tuple[str, str, Dict[str, Any], Optional[str]]: tool = _tool_or_none(token) if tool is None: return NO_TOKEN_MD, "", {}, None if not url or not url.strip(): return "Paste a Zepto product link.", "", {}, None data = tool.product(url) if data.get("status") != "success": return _error_md(data), "", data, None product = dict(data.get("product") or {}) product["images"] = " | ".join(product.get("images") or []) note = ( f"{data.get('credits_used')} credits used. Price and stock are left out here on purpose: the product " "page caps the stock count. Use the **Category listings** tab for those." ) return note, render_product(data), data, _write_csv([product], "zepto-product") # ----------------------------------------------------------------------- UI STYLE = """ """ HERO = f"""

{BOLT_SVG.format(size=36)} Zepto Scraper

Prices, MRP and live stock for every product in a Zepto category. Paste a subcategory link and get each card's selling price, printed MRP, discount and the units the dark store holds, out-of-stock items included. Open any product for its seller, shelf life and FSSAI licence.

Zepto answers scripts with an empty AWS WAF challenge. This Space fetches the pages through Scrape.do through residential proxies in India (10 credits per page) and reads the product data Zepto streams into the HTML.

Get a free API token →
""" CREDIT_NOTE = """
Every Zepto page costs 10 credits: one subcategory (up to 32 products) or one product page, fetched through Scrape.do's residential proxies in India. Walking a whole category of 9 subcategories costs 90 credits. The free 1,000 credits a month cover about 100 pages.
""" FOOTER_MD = f""" --- ### Use it as a smolagents Tool ```python from smolagents import CodeAgent, InferenceClientModel from tool import ScrapeDoZeptoTool agent = CodeAgent( tools=[ScrapeDoZeptoTool(api_token="YOUR_SCRAPEDO_TOKEN")], model=InferenceClientModel(), ) agent.run("List every out-of-stock product in Zepto's Chips & Crisps subcategory: {DEFAULT_SUBCATEGORY_URL}") ``` Or set `SCRAPE_DO_API_TOKEN` in the environment and instantiate with no arguments. Prefer plain Python? The same scraper is open source: `pip install zepto-scraper` ([GitHub](https://github.com/bugrahansss/zepto-scraper)). [Scrape.do](https://scrape.do/?utm_source=huggingface&utm_medium=zepto-scraper) · [Documentation](https://scrape.do/documentation/) · [Sign up free]({SCRAPE_DO_SIGNUP_URL}) """ def build_demo() -> gr.Blocks: with gr.Blocks(title="Zepto Scraper — Scrape.do") as demo: gr.HTML(STYLE) gr.HTML(HERO) api_token = gr.Textbox( label="Scrape.do API token", placeholder="Paste your token here", type="password", info="Free tier: 1,000 credits a month. 10 credits per Zepto page.", ) gr.HTML(CREDIT_NOTE) with gr.Tabs(): with gr.Tab("Category listings"): cat_url = gr.Textbox( label="Zepto subcategory link", value=DEFAULT_SUBCATEGORY_URL, info="Open a category on zepto.com and click a subcategory in the left sidebar. " "The link contains /cid/ and /scid/.", ) with gr.Row(): walk = gr.Checkbox(label="Also read the sibling subcategories", value=False) max_subs = gr.Slider( minimum=1, maximum=MAX_SUBCATEGORIES, step=1, value=9, label="Max subcategories (10 credits each)", ) hide_oos = gr.Checkbox(label="Hide out-of-stock cards", value=False) cat_btn = gr.Button("Scrape category", variant="primary") cat_summary = gr.Markdown() cat_view = gr.HTML() cat_table = gr.Dataframe(headers=LISTING_HEADERS, wrap=True, label="Products") cat_csv = gr.File(label="Download products as CSV") pick = gr.Dropdown( label="Open one of these products in the Product details tab", choices=[], value=None, visible=False, ) pick_note = gr.Markdown() with gr.Accordion("Raw JSON response", open=False): cat_json = gr.JSON() with gr.Tab("Product details"): prod_url = gr.Textbox(label="Zepto product link", value=DEFAULT_PRODUCT_URL) prod_btn = gr.Button("Get product details", variant="primary") prod_summary = gr.Markdown() prod_view = gr.HTML() prod_csv = gr.File(label="Download as CSV") with gr.Accordion("Raw JSON response", open=False): prod_json = gr.JSON() cat_inputs = [api_token, cat_url, walk, max_subs, hide_oos] cat_outputs = [cat_summary, cat_view, cat_table, cat_json, cat_csv, pick] cat_btn.click(fn=run_category, inputs=cat_inputs, outputs=cat_outputs) cat_url.submit(fn=run_category, inputs=cat_inputs, outputs=cat_outputs) pick.change(fn=send_to_product, inputs=[pick, prod_url], outputs=[prod_url, pick_note]) prod_outputs = [prod_summary, prod_view, prod_json, prod_csv] prod_btn.click(fn=run_product, inputs=[api_token, prod_url], outputs=prod_outputs) prod_url.submit(fn=run_product, inputs=[api_token, prod_url], outputs=prod_outputs) gr.Markdown(FOOTER_MD) return demo # Hugging Face Spaces launches the module-level `demo` object rather than # calling launch() itself, so build it at import time. demo = build_demo() def _network_probe() -> None: """Log once at startup whether api.scrape.do is reachable from this Space (a bad token costs nothing).""" import time as _t import requests as _r started = _t.time() try: resp = _r.get("https://api.scrape.do/", params={"token": "probe", "url": "https://www.zepto.com/"}, timeout=(10, 30)) print(f"[probe] api.scrape.do HTTP {resp.status_code} in {_t.time() - started:.1f}s", flush=True) except Exception as exc: # noqa: BLE001 print(f"[probe] api.scrape.do unreachable after {_t.time() - started:.1f}s: {exc}", flush=True) if __name__ == "__main__": import threading threading.Thread(target=_network_probe, daemon=True).start() demo.queue(default_concurrency_limit=4).launch(ssr_mode=False)