bugrahans's picture
Upload 4 files
a86ae8d verified
Raw History Blame Contribute Delete
38.8 kB
from __future__ import annotations
import csv
import html
import tempfile
import time
from datetime import datetime, timezone
from typing import Any, Dict, List, Optional, Tuple
import gradio as gr
from tool import (
COUNTRIES,
MAX_COMPARE,
SCRAPE_DO_SIGNUP_URL,
TRENDING_CATEGORIES,
ScrapeDoGoogleTrendsTool,
)
# 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
COUNTRY_CHOICES = [(label, code) for label, code in COUNTRIES]
TRENDING_COUNTRY_CHOICES = [(label, code) for label, code in COUNTRIES if code]
DATE_CHOICES = [
("Past 7 days", "now 7-d"),
("Past 30 days", "today 1-m"),
("Past 90 days", "today 3-m"),
("Past 12 months", "today 12-m"),
("Past 5 years", "today 5-y"),
("2004 – present", "all"),
]
SEARCH_TYPE_CHOICES = [
("Web search", "web"),
("YouTube search", "youtube"),
("News search", "news"),
("Image search", "images"),
("Google Shopping", "shopping"),
]
RESOLUTION_CHOICES = [
("Automatic", ""),
("Countries", "country"),
("States / regions", "region"),
("Metro areas (US)", "metro"),
("Cities", "city"),
]
HOURS_CHOICES = [("Past 4 hours", 4), ("Past 24 hours", 24), ("Past 48 hours", 48), ("Past 7 days", 168)]
CATEGORY_CHOICES = [(name, cid) for cid, name in TRENDING_CATEGORIES.items()]
SORT_CHOICES = [("Relevance", "relevance"), ("Search volume", "search_volume"), ("Most recent", "recency"), ("Title", "title")]
SERIES_COLORS = ["#4285f4", "#db4437", "#f4b400", "#0f9d58", "#ab47bc"]
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. Each Google Trends call 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(str(value).replace(",", ""))
except (TypeError, ValueError):
return None
def _tool_or_none(token: str) -> Optional[ScrapeDoGoogleTrendsTool]:
if not token or not token.strip():
return None
return ScrapeDoGoogleTrendsTool(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 _place_name(geo: str) -> str:
for label, code in COUNTRIES:
if code == geo:
if not code:
return "worldwide"
return f"the {label}" if label in ("United States", "United Kingdom", "Netherlands") else label
return "worldwide" if geo in ("", "worldwide") else geo
def _period_name(date: str) -> str:
for label, value in DATE_CHOICES:
if value == date:
return label.lower()
return date
def _fmt_compact(value: Any) -> str:
number = _num(value)
if number is None:
return ""
if number >= 1_000_000:
return f"{number / 1_000_000:.0f}M+" if number % 1_000_000 == 0 else f"{number / 1_000_000:.1f}M+"
if number >= 1_000:
return f"{number / 1_000:.0f}K+"
return f"{int(number)}+"
def _ago(stamp: Any) -> str:
seconds = _num(stamp)
if not seconds:
return ""
diff = time.time() - seconds
if diff < 0:
return "just now"
if diff < 3600:
return f"{int(diff // 60)} min ago"
if diff < 86400:
hours = int(diff // 3600)
return f"{hours} hour{'s' if hours != 1 else ''} ago"
days = int(diff // 86400)
return f"{days} day{'s' if days != 1 else ''} ago"
def _iso(stamp: Any) -> str:
seconds = _num(stamp)
if not seconds:
return ""
return datetime.fromtimestamp(seconds, tz=timezone.utc).strftime("%Y-%m-%d %H:%M UTC")
# ------------------------------------------------------------------- chart
def _axis_label(stamp: int, span_days: float, fallback: Any) -> str:
if not stamp:
return str(fallback or "")
moment = datetime.fromtimestamp(stamp, tz=timezone.utc)
if span_days > 400:
return moment.strftime("%b %Y")
if span_days > 3:
return moment.strftime("%b %d").replace(" 0", " ")
return moment.strftime("%H:%M")
def render_chart(timeline: List[Dict[str, Any]], series: List[str], note: str = "") -> str:
"""A Google Trends-style line chart as inline SVG, with hover details per point."""
if not timeline or not series:
return '<div class="gt-wrap"><div class="gt-empty">Not enough data to draw a chart.</div></div>'
width, height = 960, 300
left, right, top, bottom = 40, 16, 14, 36
plot_w, plot_h = width - left - right, height - top - bottom
count = len(timeline)
step = plot_w / max(1, count - 1)
def x_at(index: int) -> float:
return left + (index * step if count > 1 else plot_w / 2)
def y_at(value: float) -> float:
return top + (100 - max(0.0, min(100.0, value))) / 100 * plot_h
parts: List[str] = [f'<svg class="gt-chart" viewBox="0 0 {width} {height}" role="img">']
for level in (0, 25, 50, 75, 100):
y = y_at(level)
parts.append(f'<line x1="{left}" x2="{width - right}" y1="{y:.1f}" y2="{y:.1f}" class="gt-grid"/>')
parts.append(f'<text x="{left - 8}" y="{y + 4:.1f}" class="gt-axis" text-anchor="end">{level}</text>')
stamps = [row.get("timestamp") or 0 for row in timeline]
span_days = ((max(stamps) - min(stamps)) / 86400) if any(stamps) else 0
ticks = sorted({round(i * (count - 1) / 5) for i in range(6)}) if count > 1 else [0]
for index in ticks:
label = _axis_label(stamps[index], span_days, timeline[index].get("date"))
anchor = "start" if index == 0 else ("end" if index == count - 1 else "middle")
parts.append(f'<text x="{x_at(index):.1f}" y="{height - 12}" class="gt-axis" text-anchor="{anchor}">{_esc(label)}</text>')
for position, name in enumerate(series):
color = SERIES_COLORS[position % len(SERIES_COLORS)]
points = [
(x_at(i), y_at(row["values"][name]))
for i, row in enumerate(timeline) if row["values"].get(name) is not None
]
if not points:
continue
path = " ".join(f"{x:.1f},{y:.1f}" for x, y in points)
if len(series) == 1:
area = f"{points[0][0]:.1f},{y_at(0):.1f} {path} {points[-1][0]:.1f},{y_at(0):.1f}"
parts.append(f'<polygon points="{area}" fill="{color}" fill-opacity="0.08"/>')
parts.append(
f'<polyline points="{path}" fill="none" stroke="{color}" stroke-width="2.4" '
'stroke-linejoin="round" stroke-linecap="round" vector-effect="non-scaling-stroke"/>'
)
# Invisible hover strips: one per point, each with a native tooltip.
for index, row in enumerate(timeline):
x0 = max(left, x_at(index) - step / 2)
w = min(step, width - right - x0) if count > 1 else plot_w
detail = "; ".join(
f"{name}: {row['values'][name]:.0f}" for name in series if row["values"].get(name) is not None
)
parts.append(
f'<rect x="{x0:.1f}" y="{top}" width="{max(w, 1):.1f}" height="{plot_h}" class="gt-hover">'
f"<title>{_esc(row.get('date'))} — {_esc(detail)}</title></rect>"
)
parts.append("</svg>")
legend = "".join(
f'<span class="gt-legend-item"><i style="background:{SERIES_COLORS[i % len(SERIES_COLORS)]}"></i>{_esc(name)}</span>'
for i, name in enumerate(series)
)
note_html = f'<div class="gt-note-small">{_esc(note)}</div>' if note else ""
return (
'<div class="gt-wrap"><div class="gt-card">'
'<div class="gt-card-head"><div class="gt-card-title">Interest over time</div>'
f'<div class="gt-legend">{legend}</div></div>'
f'{"".join(parts)}{note_html}'
'<div class="gt-note-small">100 is the busiest point in this period and place. Hover the chart for exact values.</div>'
"</div></div>"
)
# ------------------------------------------------------------ explore view
def _region_bars(regions: List[Dict[str, Any]], series: List[str], limit: int = 15) -> str:
rows = []
for item in regions:
if not isinstance(item, dict):
continue
values = item.get("values")
if isinstance(values, list) and values:
numbers = [(_num(v.get("extracted_value", v.get("value"))) or 0) for v in values if isinstance(v, dict)]
score = max(numbers) if numbers else 0
index = numbers.index(score) if numbers else 0
else:
score = _num(item.get("extracted_value", item.get("value"))) or 0
index = item.get("max_value_index") or 0
rows.append((item, score, int(index) if str(index).isdigit() else 0))
rows.sort(key=lambda r: r[1], reverse=True)
rows = [r for r in rows if r[1] > 0][:limit]
if not rows:
return ""
lines = []
for item, score, index in rows:
color = SERIES_COLORS[index % len(SERIES_COLORS)] if len(series) > 1 else SERIES_COLORS[0]
leader = (
f'<span class="gt-region-leader" style="color:{color} !important">{_esc(series[index])}</span>'
if len(series) > 1 and index < len(series) else ""
)
lines.append(
f'<div class="gt-bar-row"><span class="gt-bar-label">{_esc(item.get("location") or item.get("geo"))}</span>'
f'<span class="gt-bar-track"><span class="gt-bar-fill" style="width:{score:.0f}%;background:{color}"></span></span>'
f'<span class="gt-bar-value">{score:.0f}</span>{leader}</div>'
)
title = "Where each keyword leads" if len(series) > 1 else "Interest by region"
return f'<div class="gt-card"><div class="gt-card-title">{title}</div>{"".join(lines)}</div>'
def _related_list(items: List[Dict[str, Any]], rising: bool, kind: str) -> str:
if not items:
return '<div class="gt-muted">Not enough data.</div>'
rows = []
for position, item in enumerate(items[:15], start=1):
if kind == "topics":
topic = item.get("topic") or {}
label = _esc(topic.get("title") or item.get("title"))
sub = topic.get("type")
label += f' <span class="gt-muted">{_esc(sub)}</span>' if sub else ""
else:
label = _esc(item.get("query"))
value = str(item.get("value") or "")
if rising and value.replace(",", "").isdigit():
value = f"+{value}%"
cls = "gt-breakout" if value.lower() == "breakout" else ("gt-rise" if rising else "gt-top")
link = _safe_link(item.get("link"))
label_html = f'<a class="gt-link" href="{_esc(link)}" target="_blank" rel="noopener noreferrer">{label}</a>' if link else label
rows.append(
f'<div class="gt-rel-row"><span class="gt-rel-pos">{position}</span>'
f'<span class="gt-rel-label">{label_html}</span><span class="gt-rel-value {cls}">{_esc(value)}</span></div>'
)
return "".join(rows)
def _related_card(block: Any, kind: str) -> str:
if not isinstance(block, dict):
return ""
title = "Related searches" if kind == "queries" else "Related topics"
return (
f'<div class="gt-card"><div class="gt-card-title">{title}</div><div class="gt-two">'
f'<div><div class="gt-col-title">Top</div>{_related_list(block.get("top") or [], False, kind)}</div>'
f'<div><div class="gt-col-title">Rising</div>{_related_list(block.get("rising") or [], True, kind)}</div>'
"</div></div>"
)
def _series_names(timeline: List[Dict[str, Any]], preferred: List[str]) -> List[str]:
found: List[str] = []
for row in timeline:
for name in row["values"]:
if name not in found:
found.append(name)
ordered = [p for p in preferred if p in found]
return ordered + [f for f in found if f not in ordered]
def _timeline_rows(timeline: List[Dict[str, Any]], series: List[str]) -> List[Dict[str, Any]]:
rows = []
for row in timeline:
entry: Dict[str, Any] = {"date": row.get("date"), "timestamp": row.get("timestamp")}
for name in series:
value = row["values"].get(name)
entry[name] = int(value) if isinstance(value, float) and value.is_integer() else value
rows.append(entry)
return rows
def _extras_rows(data: Dict[str, Any]) -> List[Dict[str, Any]]:
rows: List[Dict[str, Any]] = []
for item in data.get("interest_by_region") or []:
if isinstance(item, dict):
rows.append({"section": "region", "name": item.get("location"), "code": item.get("geo"),
"value": item.get("extracted_value", item.get("value")), "link": ""})
for key, kind in (("related_queries", "query"), ("related_topics", "topic")):
block = data.get(key) or {}
for bucket in ("top", "rising"):
for item in block.get(bucket) or []:
if not isinstance(item, dict):
continue
topic = item.get("topic") or {}
rows.append({
"section": f"{bucket}_{kind}",
"name": item.get("query") or topic.get("title"),
"code": topic.get("type", ""),
"value": item.get("value"),
"link": item.get("link"),
})
return rows
# ---------------------------------------------------------------- handlers
def run_explore(
token: str, keyword: str, geo: str, date: str, search_type: str, resolution: str,
want_queries: bool, want_topics: bool,
) -> Tuple[str, str, str, Dict[str, Any], Optional[str], Optional[str]]:
tool = _tool_or_none(token)
if tool is None:
return NO_TOKEN_MD, "", "", {}, None, None
if not keyword or not keyword.strip():
return "Type a keyword to explore, e.g. `bitcoin`.", "", "", {}, None, None
data = tool.explore(keyword, geo, date, search_type, bool(want_queries), bool(want_topics), resolution or None)
if data.get("status") != "success":
return _error_md(data), "", "", data, None, None
timeline = data.get("timeline") or []
series = _series_names(timeline, [data.get("query", "")])
stats = data.get("stats") or {}
place, period = _place_name(geo or ""), _period_name(data.get("date", ""))
lines = []
if stats:
line = (
f"Interest in **{data['query']}** {('in ' + place) if place != 'worldwide' else 'worldwide'}, "
f"{period}: peaked at **{stats.get('peak'):.0f}** around {stats.get('peak_date')}, now at **{stats.get('latest'):.0f}**, "
f"average **{stats.get('average'):.0f}**."
)
change = stats.get("change_percent")
if change is not None:
direction = "up" if change > 5 else ("down" if change < -5 else "about flat")
line += f" Compared with the start of the period it's **{direction}**" + (
f" ({change:+.0f}%)." if direction != "about flat" else "."
)
lines.append(line)
regions = [r for r in data.get("interest_by_region") or [] if isinstance(r, dict)]
leaders = [r.get("location") for r in sorted(regions, key=lambda r: _num(r.get("extracted_value")) or 0, reverse=True)[:3]]
if leaders:
lines.append("Most popular in " + ", ".join(f"**{n}**" for n in leaders if n) + ".")
lines.append(f"{data.get('credits_used')} credits used.")
if data.get("warnings"):
lines.append("\n".join(f"- {w}" for w in data["warnings"]))
chart = render_chart(timeline, series)
extras = (
'<div class="gt-wrap"><div class="gt-grid2">'
+ _region_bars(regions, series)
+ _related_card(data.get("related_queries"), "queries")
+ "</div>"
+ _related_card(data.get("related_topics"), "topics")
+ "</div>"
)
timeline_csv = _write_csv(_timeline_rows(timeline, series), "google-trends-timeline")
extras_csv = _write_csv(_extras_rows(data), "google-trends-regions-related")
return "\n\n".join(lines), chart, extras, data, timeline_csv, extras_csv
def run_compare(
token: str, keywords: str, geo: str, date: str, search_type: str
) -> Tuple[str, str, str, List[List[Any]], Dict[str, Any], Optional[str]]:
tool = _tool_or_none(token)
if tool is None:
return NO_TOKEN_MD, "", "", [], {}, None
if not keywords or not keywords.strip():
return "Enter two to five keywords separated by commas, e.g. `chatgpt, claude, gemini`.", "", "", [], {}, None
data = tool.compare(keywords, geo, date, search_type)
if data.get("status") != "success":
return _error_md(data), "", "", [], data, None
timeline = data.get("timeline") or []
series = _series_names(timeline, data.get("keywords") or [])
stats = data.get("stats") or {}
lines = []
if data.get("leader") and not data.get("scaled_separately"):
ranked = sorted(series, key=lambda s: (stats.get(s) or {}).get("average") or 0, reverse=True)
line = (
f"**{ranked[0]}** gets the most searches {('in ' + _place_name(geo or '')) if geo else 'worldwide'}, "
f"{_period_name(data.get('date', ''))}."
)
if len(ranked) > 1:
first = (stats.get(ranked[0]) or {}).get("average") or 0
second = (stats.get(ranked[1]) or {}).get("average") or 0
if second >= 1:
line += f" On average it's searched **{first / second:.1f}×** as much as **{ranked[1]}**."
lines.append(line)
lines.append(f"{data.get('credits_used')} credits used.")
if data.get("warnings"):
lines.append("\n".join(f"- {w}" for w in data["warnings"]))
table = []
for name in series:
s = stats.get(name) or {}
change = s.get("change_percent")
table.append([
name, s.get("average"), s.get("latest"), s.get("peak_date"),
f"{change:+.0f}%" if isinstance(change, (int, float)) else "",
])
table.sort(key=lambda r: r[1] or 0, reverse=True)
regions = [r for r in data.get("interest_by_region") or [] if isinstance(r, dict)]
extras = f'<div class="gt-wrap">{_region_bars(regions, series, limit=20)}</div>' if regions else ""
csv_path = _write_csv(_timeline_rows(timeline, series), "google-trends-compare")
return "\n\n".join(lines), render_chart(timeline, series), extras, table, data, csv_path
def render_trending(data: Dict[str, Any]) -> str:
cards = []
for position, trend in enumerate(data.get("trends") or [], start=1):
link = _safe_link(trend.get("trend_url"))
title = _esc(trend.get("title"))
title_html = (
f'<a class="gt-trend-title" href="{_esc(link)}" target="_blank" rel="noopener noreferrer">{title}</a>'
if link else f'<span class="gt-trend-title">{title}</span>'
)
active = str(trend.get("status") or "").lower() == "active"
status = (
'<span class="gt-status gt-active">● Active</span>' if active
else '<span class="gt-status gt-ended">Ended</span>'
)
growth = _num(trend.get("growth_percentage"))
growth_html = f'<span class="gt-growth">↑ {growth:,.0f}%</span>' if growth else ""
chips = "".join(f'<span class="gt-chip">{_esc(q)}</span>' for q in (trend.get("related_queries") or [])[:6])
cats = " · ".join(_esc(c) for c in trend.get("categories") or [])
cards.append(
'<div class="gt-trend">'
f'<div class="gt-trend-pos">{position}</div>'
'<div class="gt-trend-body">'
f"{title_html}"
f'<div class="gt-trend-meta"><span class="gt-volume">{_esc(_fmt_compact(trend.get("search_volume")))} searches</span>'
f"{growth_html}{status}"
f'<span class="gt-muted">Started {_esc(_ago(trend.get("started_at")))}</span></div>'
+ (f'<div class="gt-muted">{cats}</div>' if cats else "")
+ (f'<div class="gt-chips">{chips}</div>' if chips else "")
+ "</div></div>"
)
if not cards:
cards.append('<div class="gt-empty">Nothing is trending for these filters right now.</div>')
return f'<div class="gt-wrap"><div class="gt-trend-list">{"".join(cards)}</div></div>'
def run_trending(
token: str, geo: str, hours: int, category: int, sort: str, active_only: bool
) -> Tuple[str, str, Dict[str, Any], Optional[str]]:
tool = _tool_or_none(token)
if tool is None:
return NO_TOKEN_MD, "", {}, None
data = tool.trending(geo or "US", int(hours or 24), int(category or 0), sort or "relevance", bool(active_only))
if data.get("status") != "success":
return _error_md(data), "", data, None
trends = data.get("trends") or []
active = sum(1 for t in trends if str(t.get("status") or "").lower() == "active")
window = dict((v, k) for k, v in HOURS_CHOICES).get(data.get("hours"), f"past {data.get('hours')} hours").lower()
summary = (
f"**{len(trends)}** searches trending in **{_place_name(data.get('geo', ''))}**, {window}"
+ (f", in {data.get('category')}" if data.get("category") != "All categories" else "")
+ f" · **{active}** still active · {data.get('credits_used')} credits used"
)
rows = [
{
"rank": i,
"title": t.get("title"),
"search_volume": t.get("search_volume"),
"growth_percentage": t.get("growth_percentage"),
"status": t.get("status"),
"started_at": _iso(t.get("started_at")),
"ended_at": _iso(t.get("ended_at")),
"categories": ", ".join(t.get("categories") or []),
"related_queries": " | ".join(t.get("related_queries") or []),
"trend_url": t.get("trend_url"),
}
for i, t in enumerate(trends, start=1)
]
return summary, render_trending(data), data, _write_csv(rows, "google-trending-now")
# ----------------------------------------------------------------------- UI
STYLE = """
<style>
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap');
.gradio-container, .gradio-container button, .gradio-container input,
.gradio-container textarea, .gradio-container select {
font-family: 'Roboto', Arial, sans-serif;
}
/* hero */
.gt-hero { font-family: 'Roboto', Arial, sans-serif; position: relative; overflow: hidden; background: #ffffff;
border: 1px solid #dadce0; border-radius: 16px; padding: 28px 32px 26px 32px; margin-bottom: 16px; }
.gt-hero-inner { position: relative; z-index: 1; max-width: 600px; }
.gt-hero h1 { color: #202124 !important; font-size: 2rem !important; font-weight: 500 !important;
margin: 0 0 10px 0 !important; letter-spacing: -0.01em; display: flex; align-items: center; gap: 10px; }
.gt-hero p { color: #3c4043 !important; margin: 0 0 8px 0 !important; font-size: 1rem !important; line-height: 1.55 !important; }
.gt-hero strong { color: #202124 !important; font-weight: 500 !important; }
.gt-hero a { color: #1a73e8 !important; }
.gt-hero a.gt-cta { display: inline-block; margin-top: 10px; padding: 11px 24px; background: #1a73e8 !important;
color: #ffffff !important; border-radius: 999px; font-weight: 500 !important; text-decoration: none !important; }
.gt-hero-art { position: absolute; right: 28px; top: 50%; transform: translateY(-50%); width: 330px; height: 190px; }
@media (max-width: 900px) { .gt-hero-art { display: none; } }
.gt-note { font-family: 'Roboto', Arial, sans-serif; border-left: 3px solid #f4b400; background: #fef7e0;
padding: 12px 16px; border-radius: 0 8px 8px 0; font-size: 0.9rem; margin-bottom: 12px; color: #202124 !important; }
.gradio-container .primary, .gradio-container button.primary,
.gradio-container button[variant="primary"] {
background: #1a73e8 !important; background-image: none !important; border-color: #1a73e8 !important;
color: #ffffff !important; border-radius: 999px !important; font-weight: 500 !important;
}
.gradio-container .primary:hover, .gradio-container button.primary:hover,
.gradio-container button[variant="primary"]:hover { background: #1765cc !important; }
.gradio-container .tab-nav button.selected { color: #1a73e8 !important; border-bottom-color: #1a73e8 !important; }
/* shared */
.gt-wrap { color: #202124 !important; font-family: 'Roboto', Arial, sans-serif; }
.gt-card { background: #ffffff; border: 1px solid #dadce0; border-radius: 12px; padding: 16px 18px; margin-bottom: 12px; }
.gt-card-head { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 8px; margin-bottom: 6px; }
.gt-card-title { font-size: 1.05rem; font-weight: 500; color: #202124 !important; margin-bottom: 10px; }
.gt-card-head .gt-card-title { margin-bottom: 0; }
.gt-empty { background: #ffffff; border: 1px solid #dadce0; border-radius: 12px; padding: 20px; color: #5f6368 !important; }
.gt-muted { color: #70757a !important; font-size: 0.82rem; }
.gt-note-small { color: #70757a !important; font-size: 0.78rem; margin-top: 4px; }
.gt-link { color: #202124 !important; text-decoration: none !important; }
.gt-link:hover { color: #1a73e8 !important; text-decoration: underline !important; }
.gt-grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 820px) { .gt-grid2 { grid-template-columns: 1fr; } }
.gt-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.gt-chip { background: #f1f3f4; color: #3c4043 !important; border-radius: 999px; padding: 3px 10px; font-size: 0.8rem; }
/* chart */
.gt-chart { width: 100%; height: auto; display: block; }
.gt-grid { stroke: #e8eaed; stroke-width: 1; }
.gt-axis { fill: #70757a; font-size: 12px; font-family: 'Roboto', Arial, sans-serif; }
.gt-hover { fill: transparent; }
.gt-hover:hover { fill: rgba(60, 64, 67, 0.06); }
.gt-legend { display: flex; flex-wrap: wrap; gap: 14px; }
.gt-legend-item { display: inline-flex; align-items: center; gap: 6px; font-size: 0.88rem; color: #3c4043 !important; }
.gt-legend-item i { display: inline-block; width: 14px; height: 4px; border-radius: 2px; }
/* bars and lists */
.gt-bar-row { display: flex; align-items: center; gap: 10px; margin: 6px 0; font-size: 0.88rem; }
.gt-bar-label { width: 150px; color: #202124 !important; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.gt-bar-track { flex: 1; height: 10px; background: #f1f3f4; border-radius: 999px; overflow: hidden; }
.gt-bar-fill { display: block; height: 100%; border-radius: 999px; }
.gt-bar-value { width: 28px; text-align: right; color: #5f6368 !important; font-size: 0.8rem; }
.gt-region-leader { width: 110px; font-size: 0.78rem; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.gt-two { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
@media (max-width: 600px) { .gt-two { grid-template-columns: 1fr; } }
.gt-col-title { font-size: 0.8rem; font-weight: 500; color: #5f6368 !important; text-transform: uppercase; letter-spacing: .04em; margin-bottom: 6px; }
.gt-rel-row { display: flex; align-items: center; gap: 10px; padding: 6px 0; border-bottom: 1px solid #f1f3f4; font-size: 0.88rem; }
.gt-rel-pos { width: 18px; color: #70757a !important; font-size: 0.78rem; }
.gt-rel-label { flex: 1; color: #202124 !important; min-width: 0; overflow-wrap: anywhere; }
.gt-rel-value { font-size: 0.8rem; font-weight: 500; white-space: nowrap; }
.gt-top { color: #5f6368 !important; }
.gt-rise { color: #188038 !important; }
.gt-breakout { color: #ffffff !important; background: #db4437; border-radius: 4px; padding: 1px 6px; font-size: 0.72rem; }
/* trending now */
.gt-trend-list { display: flex; flex-direction: column; gap: 8px; }
.gt-trend { display: flex; gap: 14px; background: #ffffff; border: 1px solid #dadce0; border-radius: 12px; padding: 12px 16px; }
.gt-trend-pos { width: 26px; height: 26px; border-radius: 50%; background: #e8f0fe; color: #1a73e8 !important; font-size: 0.8rem;
font-weight: 500; display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
.gt-trend-body { min-width: 0; flex: 1; }
.gt-trend-title { color: #202124 !important; font-size: 1.02rem; font-weight: 500; text-decoration: none !important; }
a.gt-trend-title:hover { color: #1a73e8 !important; text-decoration: underline !important; }
.gt-trend-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; margin-top: 4px; font-size: 0.85rem; }
.gt-volume { color: #202124 !important; font-weight: 500; }
.gt-growth { color: #188038 !important; font-weight: 500; }
.gt-status { font-size: 0.8rem; font-weight: 500; }
.gt-active { color: #188038 !important; }
.gt-ended { color: #70757a !important; }
footer { display: none !important; }
</style>
"""
HERO_ART = """
<svg class="gt-hero-art" viewBox="0 0 330 190" aria-hidden="true">
<rect x="0" y="0" width="330" height="190" rx="14" fill="#f8f9fa"/>
<g stroke="#e8eaed" stroke-width="1">
<line x1="20" x2="310" y1="40" y2="40"/><line x1="20" x2="310" y1="80" y2="80"/>
<line x1="20" x2="310" y1="120" y2="120"/><line x1="20" x2="310" y1="160" y2="160"/>
</g>
<polyline points="20,140 50,132 80,136 110,118 140,124 170,96 200,104 230,70 260,82 290,48 310,56" fill="none" stroke="#4285f4" stroke-width="3" stroke-linejoin="round" stroke-linecap="round"/>
<polyline points="20,120 50,126 80,112 110,128 140,116 170,130 200,118 230,124 260,110 290,120 310,114" fill="none" stroke="#db4437" stroke-width="3" stroke-linejoin="round" stroke-linecap="round"/>
<polyline points="20,154 50,150 80,152 110,146 140,150 170,144 200,140 230,146 260,138 290,142 310,134" fill="none" stroke="#f4b400" stroke-width="3" stroke-linejoin="round" stroke-linecap="round"/>
<circle cx="290" cy="48" r="5" fill="#4285f4"/>
</svg>
"""
TREND_ICON = (
'<svg viewBox="0 0 24 24" width="30" height="30" aria-hidden="true">'
'<path fill="none" stroke="#4285f4" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round" d="M3 17l6-6 4 4 8-8"/>'
'<path fill="none" stroke="#4285f4" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round" d="M15 7h6v6"/></svg>'
)
HERO = f"""
<div class="gt-hero">
{HERO_ART}
<div class="gt-hero-inner">
<h1>{TREND_ICON} Google Trends Scraper</h1>
<p><strong>Google Trends data as clean JSON and CSV</strong>: interest over time, interest by country or city,
rising and top related searches, keyword comparisons, and what's trending right now in any country.</p>
<p>The unofficial pytrends library is no longer maintained and Google's own Trends API is still invite-only.
This Space runs on the <a href="https://scrape.do/documentation/google-scraper-api/trends/" target="_blank" rel="noopener noreferrer">Scrape.do Google Trends API</a>,
which handles Google's rate limits for you.</p>
<a class="gt-cta" href="{SCRAPE_DO_SIGNUP_URL}" target="_blank" rel="noopener noreferrer">Get a free API token →</a>
</div>
</div>
"""
CREDIT_NOTE = """
<div class="gt-note">
Every call costs 10 credits. Exploring a keyword is one call, plus one more each for related searches and
related topics. A comparison is one call, and trending searches are one call. The free 1,000 credits cover
about 100 calls.
</div>
"""
FOOTER_MD = f"""
---
### Use it as a smolagents Tool
```python
from smolagents import CodeAgent, InferenceClientModel
from tool import ScrapeDoGoogleTrendsTool
agent = CodeAgent(
tools=[ScrapeDoGoogleTrendsTool(api_token="YOUR_SCRAPEDO_TOKEN")],
model=InferenceClientModel(),
)
agent.run("Compare interest in ChatGPT, Claude and Gemini in the US over the past 12 months, then list what's trending in Technology right now.")
```
Or set `SCRAPE_DO_API_TOKEN` in the environment and instantiate with no arguments.
[Scrape.do](https://scrape.do/) · [Google Trends API docs](https://scrape.do/documentation/google-scraper-api/trends/) · [Sign up free]({SCRAPE_DO_SIGNUP_URL})
"""
def build_demo() -> gr.Blocks:
with gr.Blocks(title="Google Trends 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, about 100 calls.",
)
gr.HTML(CREDIT_NOTE)
with gr.Tabs():
with gr.Tab("Explore a keyword"):
keyword = gr.Textbox(label="Keyword", placeholder="bitcoin")
with gr.Row():
ex_geo = gr.Dropdown(
label="Where", choices=COUNTRY_CHOICES, value="", allow_custom_value=True,
info="Or type a region code such as US-CA.",
)
ex_date = gr.Dropdown(
label="When", choices=DATE_CHOICES, value="today 12-m", allow_custom_value=True,
info="Or type a range: 2024-01-01 2024-12-31",
)
ex_type = gr.Dropdown(label="Search type", choices=SEARCH_TYPE_CHOICES, value="web")
with gr.Row():
ex_resolution = gr.Dropdown(label="Break down by", choices=RESOLUTION_CHOICES, value="")
want_queries = gr.Checkbox(label="Related searches (+10 credits)", value=True)
want_topics = gr.Checkbox(label="Related topics (+10 credits)", value=False)
explore_btn = gr.Button("Explore", variant="primary")
explore_summary = gr.Markdown()
explore_chart = gr.HTML()
explore_extras = gr.HTML()
with gr.Row():
explore_csv = gr.File(label="Interest over time (CSV)")
explore_extras_csv = gr.File(label="Regions and related searches (CSV)")
with gr.Accordion("Raw JSON response", open=False):
explore_json = gr.JSON()
with gr.Tab("Compare keywords"):
compare_terms = gr.Textbox(
label=f"Keywords to compare (2–{MAX_COMPARE}, separated by commas)",
placeholder="chatgpt, claude, gemini",
)
with gr.Row():
cmp_geo = gr.Dropdown(label="Where", choices=COUNTRY_CHOICES, value="", allow_custom_value=True)
cmp_date = gr.Dropdown(label="When", choices=DATE_CHOICES, value="today 12-m", allow_custom_value=True)
cmp_type = gr.Dropdown(label="Search type", choices=SEARCH_TYPE_CHOICES, value="web")
compare_btn = gr.Button("Compare", variant="primary")
compare_summary = gr.Markdown()
compare_chart = gr.HTML()
compare_table = gr.Dataframe(
headers=["Keyword", "Average", "Latest", "Peak", "Change"], wrap=True, label="Side by side"
)
compare_extras = gr.HTML()
compare_csv = gr.File(label="Download as CSV")
with gr.Accordion("Raw JSON response", open=False):
compare_json = gr.JSON()
with gr.Tab("Trending now"):
with gr.Row():
tr_geo = gr.Dropdown(label="Country", choices=TRENDING_COUNTRY_CHOICES, value="US", allow_custom_value=True)
tr_hours = gr.Dropdown(label="Time window", choices=HOURS_CHOICES, value=24)
tr_cat = gr.Dropdown(label="Category", choices=CATEGORY_CHOICES, value=0)
with gr.Row():
tr_sort = gr.Dropdown(label="Sort by", choices=SORT_CHOICES, value="relevance")
tr_active = gr.Checkbox(label="Only trends that are still active", value=False)
trending_btn = gr.Button("Show trending searches", variant="primary")
trending_summary = gr.Markdown()
trending_view = gr.HTML()
trending_csv = gr.File(label="Download as CSV")
with gr.Accordion("Raw JSON response", open=False):
trending_json = gr.JSON()
explore_inputs = [api_token, keyword, ex_geo, ex_date, ex_type, ex_resolution, want_queries, want_topics]
explore_outputs = [explore_summary, explore_chart, explore_extras, explore_json, explore_csv, explore_extras_csv]
explore_btn.click(fn=run_explore, inputs=explore_inputs, outputs=explore_outputs)
keyword.submit(fn=run_explore, inputs=explore_inputs, outputs=explore_outputs)
compare_inputs = [api_token, compare_terms, cmp_geo, cmp_date, cmp_type]
compare_outputs = [compare_summary, compare_chart, compare_extras, compare_table, compare_json, compare_csv]
compare_btn.click(fn=run_compare, inputs=compare_inputs, outputs=compare_outputs)
compare_terms.submit(fn=run_compare, inputs=compare_inputs, outputs=compare_outputs)
trending_inputs = [api_token, tr_geo, tr_hours, tr_cat, tr_sort, tr_active]
trending_outputs = [trending_summary, trending_view, trending_json, trending_csv]
trending_btn.click(fn=run_trending, inputs=trending_inputs, outputs=trending_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()
if __name__ == "__main__":
demo.launch()