Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
"""
|
| 2 |
-
B2B Content Strategy Planner (
|
| 3 |
Deployed for Hugging Face Spaces.
|
| 4 |
-
Features: 3-Tab Layout, Multi-Model Ranking, Visual Gap/Strength Analysis, Unlimited Crawling.
|
| 5 |
"""
|
| 6 |
|
| 7 |
import gradio as gr
|
|
@@ -27,18 +26,17 @@ HEADERS = {
|
|
| 27 |
'Connection': 'keep-alive'
|
| 28 |
}
|
| 29 |
|
| 30 |
-
# Extensive Industry Packs
|
| 31 |
TOPIC_PACKS = {
|
| 32 |
-
"cloud": ["cloud", "saas", "paas", "iaas", "migration", "serverless", "aws", "azure", "gcp"
|
| 33 |
-
"devops": ["devops", "kubernetes", "docker", "ci-cd", "terraform", "ansible"
|
| 34 |
-
"
|
| 35 |
-
"
|
| 36 |
-
"ai": ["ai", "machine-learning", "generative-ai", "llm", "nlp", "computer-vision"
|
| 37 |
-
"martech": ["martech", "crm", "seo", "content-marketing", "lead-gen", "hubspot", "salesforce"
|
| 38 |
-
"fintech": ["fintech", "payments", "banking", "blockchain", "crypto", "defi"
|
| 39 |
-
"healthtech": ["healthtech", "telemedicine", "ehr", "hipaa", "medtech"
|
| 40 |
-
"ecommerce": ["ecommerce", "shopify", "magento", "dtc", "omnichannel"
|
| 41 |
-
"web": ["web-dev", "frontend", "backend", "react", "node", "javascript"
|
| 42 |
}
|
| 43 |
|
| 44 |
CATEGORY_HINTS = {
|
|
@@ -49,14 +47,7 @@ CATEGORY_HINTS = {
|
|
| 49 |
"Product": ["product", "platform", "tool", "software"]
|
| 50 |
}
|
| 51 |
|
| 52 |
-
|
| 53 |
-
AI_MODELS = [
|
| 54 |
-
"GPT-4o",
|
| 55 |
-
"GPT-4 Turbo",
|
| 56 |
-
"Claude 3.5 Sonnet",
|
| 57 |
-
"Claude 3 Opus",
|
| 58 |
-
"Gemini 1.5 Pro"
|
| 59 |
-
]
|
| 60 |
|
| 61 |
# ============================================================================
|
| 62 |
# 2. CRAWLER & UTILS
|
|
@@ -198,37 +189,51 @@ def categorize_links(urls):
|
|
| 198 |
return dict(cats)
|
| 199 |
|
| 200 |
# ============================================================================
|
| 201 |
-
# 3. AI IDEA FACTORY (
|
| 202 |
# ============================================================================
|
| 203 |
|
| 204 |
-
def
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
return f"""
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
|
|
|
| 216 |
"""
|
| 217 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
def score_model_output():
|
| 219 |
-
# Generate
|
| 220 |
metrics = {
|
| 221 |
"Topic Relevance": random.randint(8, 10),
|
| 222 |
"Stage Alignment": random.randint(8, 10),
|
| 223 |
-
"Ideas": random.randint(
|
| 224 |
-
"Structure": random.randint(
|
| 225 |
"Specific": random.randint(7, 10),
|
| 226 |
-
"Creative": random.randint(
|
| 227 |
"Action": random.randint(8, 10)
|
| 228 |
}
|
| 229 |
-
# Weighted total
|
| 230 |
total = sum(metrics.values())
|
| 231 |
-
# Scale to 100
|
| 232 |
final_score = int((total / 70) * 100)
|
| 233 |
return final_score, metrics
|
| 234 |
|
|
@@ -237,7 +242,7 @@ def ui_generate_multimodel(topic, stage):
|
|
| 237 |
|
| 238 |
results = []
|
| 239 |
|
| 240 |
-
# Run
|
| 241 |
for model in AI_MODELS:
|
| 242 |
resp = generate_model_response(model, topic, stage)
|
| 243 |
score, metrics = score_model_output()
|
|
@@ -248,7 +253,7 @@ def ui_generate_multimodel(topic, stage):
|
|
| 248 |
"resp": resp
|
| 249 |
})
|
| 250 |
|
| 251 |
-
#
|
| 252 |
results.sort(key=lambda x: x['score'], reverse=True)
|
| 253 |
|
| 254 |
# 1. Comparison Table
|
|
@@ -256,7 +261,7 @@ def ui_generate_multimodel(topic, stage):
|
|
| 256 |
<table style="width:100%; border-collapse: collapse; margin-bottom: 20px; font-family:sans-serif;">
|
| 257 |
<tr style="background: #f3f4f6; border-bottom: 2px solid #ddd;">
|
| 258 |
<th style="padding:10px;">Rank</th>
|
| 259 |
-
<th style="padding:10px;">Model</th>
|
| 260 |
<th style="padding:10px;">Score</th>
|
| 261 |
<th style="padding:10px;">Relevance</th>
|
| 262 |
<th style="padding:10px;">Creative</th>
|
|
@@ -267,11 +272,12 @@ def ui_generate_multimodel(topic, stage):
|
|
| 267 |
for i, r in enumerate(results):
|
| 268 |
medal = "π₯" if i==0 else "π₯" if i==1 else "π₯" if i==2 else f"{i+1}"
|
| 269 |
bg = "#f0fdf4" if i==0 else "white"
|
|
|
|
| 270 |
table_html += f"""
|
| 271 |
-
<tr style="background:{bg}; border-bottom:1px solid #eee;">
|
| 272 |
<td style="padding:10px; text-align:center;">{medal}</td>
|
| 273 |
-
<td style="padding:10px;
|
| 274 |
-
<td style="padding:10px; color:#166534;
|
| 275 |
<td style="padding:10px; text-align:center;">{r['metrics']['Topic Relevance']}</td>
|
| 276 |
<td style="padding:10px; text-align:center;">{r['metrics']['Creative']}</td>
|
| 277 |
<td style="padding:10px; text-align:center;">{r['metrics']['Action']}</td>
|
|
@@ -279,24 +285,38 @@ def ui_generate_multimodel(topic, stage):
|
|
| 279 |
"""
|
| 280 |
table_html += "</table>"
|
| 281 |
|
| 282 |
-
# 2. Detailed Cards
|
| 283 |
-
cards_html = "<div style='display:flex; flex-direction:column; gap:
|
| 284 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
cards_html += f"""
|
| 286 |
-
<div style="border:
|
| 287 |
-
<div style="display:flex; justify-content:space-between; margin-bottom:
|
| 288 |
-
<
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
<
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
<
|
| 296 |
</div>
|
| 297 |
-
|
|
|
|
| 298 |
{r['resp']}
|
| 299 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
</div>
|
| 301 |
"""
|
| 302 |
cards_html += "</div>"
|
|
@@ -304,14 +324,12 @@ def ui_generate_multimodel(topic, stage):
|
|
| 304 |
return table_html, cards_html
|
| 305 |
|
| 306 |
# ============================================================================
|
| 307 |
-
# 4. COMPETITOR INTELLIGENCE (Visual Gaps/Strengths)
|
| 308 |
# ============================================================================
|
| 309 |
|
| 310 |
def get_tokens(urls: List[str]) -> Set[str]:
|
| 311 |
-
"""Extracts readable titles/tokens for comparison"""
|
| 312 |
tokens = set()
|
| 313 |
for u in urls:
|
| 314 |
-
# Use the formatted title as the token
|
| 315 |
slug = u.rstrip('/').split('/')[-1]
|
| 316 |
title = format_url_to_title(slug)
|
| 317 |
if len(title) > 3:
|
|
@@ -321,7 +339,6 @@ def get_tokens(urls: List[str]) -> Set[str]:
|
|
| 321 |
def generate_pill_html(items: List[str], style: str, empty_msg: str):
|
| 322 |
if not items: return f'<p style="color:#666; font-style:italic;">{empty_msg}</p>'
|
| 323 |
|
| 324 |
-
# Visual Styles (Orange for Gaps, Green for Strengths)
|
| 325 |
styles = {
|
| 326 |
"gap": {"bg": "#fff7ed", "border": "#fdba74", "text": "#c2410c", "icon": "β"},
|
| 327 |
"strength": {"bg": "#f0fdf4", "border": "#86efac", "text": "#15803d", "icon": "β"}
|
|
@@ -329,7 +346,7 @@ def generate_pill_html(items: List[str], style: str, empty_msg: str):
|
|
| 329 |
s = styles[style]
|
| 330 |
|
| 331 |
html = '<div style="display:flex; flex-wrap:wrap; gap:8px; margin-top:10px;">'
|
| 332 |
-
for item in sorted(list(items))[:50]:
|
| 333 |
link = f"https://www.google.com/search?q={item.replace(' ', '+')}"
|
| 334 |
html += f"""
|
| 335 |
<a href="{link}" target="_blank" style="text-decoration:none;">
|
|
@@ -349,36 +366,52 @@ def analyze_gaps_strengths(user_urls, competitors, filter_topic=None):
|
|
| 349 |
if not user_urls: return "", ""
|
| 350 |
|
| 351 |
my_tokens = get_tokens(user_urls)
|
| 352 |
-
comp_tokens = set()
|
| 353 |
-
for c in competitors: comp_tokens.update(get_tokens(c['urls']))
|
| 354 |
|
| 355 |
-
#
|
| 356 |
-
|
| 357 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 358 |
|
| 359 |
-
# Filtering
|
| 360 |
if filter_topic:
|
| 361 |
ft = filter_topic.lower()
|
| 362 |
gaps = {g for g in gaps if ft in g.lower()}
|
| 363 |
strengths = {s for s in strengths if ft in s.lower()}
|
| 364 |
|
| 365 |
return (
|
| 366 |
-
generate_pill_html(list(gaps), "gap", "β
No gaps found!"),
|
| 367 |
-
generate_pill_html(list(strengths), "strength", "βͺ No unique strengths
|
| 368 |
)
|
| 369 |
|
| 370 |
-
def
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 382 |
|
| 383 |
# ============================================================================
|
| 384 |
# 5. UI EVENTS
|
|
@@ -391,7 +424,6 @@ def ui_fetch(url, force):
|
|
| 391 |
cats = categorize_links(urls)
|
| 392 |
state['urls'] = urls
|
| 393 |
|
| 394 |
-
# Update Planner Dropdowns AND AI Idea Dropdown (Sync)
|
| 395 |
cat_opts = ["All"] + sorted(list(cats.keys()))
|
| 396 |
return (urls, cats, log,
|
| 397 |
gr.update(choices=cat_opts, value="All"),
|
|
@@ -419,32 +451,38 @@ def ui_analyze_planner(urls, category, topic):
|
|
| 419 |
def ui_add_comp(url, current_comps):
|
| 420 |
urls, _ = fetch_content_data(url, force_crawl=False)
|
| 421 |
domain = urlparse(url if url.startswith('http') else f'https://{url}').netloc
|
| 422 |
-
new_c = {'domain': domain, 'urls': urls}
|
| 423 |
-
current_comps.append(new_c)
|
| 424 |
-
txt = "\n".join([f"β
{c['domain']}: {len(c['urls'])} pages" for c in current_comps])
|
| 425 |
-
return current_comps, txt, ""
|
| 426 |
-
|
| 427 |
-
def ui_run_comp_analysis(user_urls, competitors):
|
| 428 |
-
# Overall Analysis
|
| 429 |
-
g_html, s_html = analyze_gaps_strengths(user_urls, competitors, None)
|
| 430 |
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
strat = generate_strategy(g_count, s_count)
|
| 435 |
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
#
|
| 440 |
-
|
| 441 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 442 |
|
| 443 |
# ============================================================================
|
| 444 |
# LAYOUT
|
| 445 |
# ============================================================================
|
| 446 |
|
| 447 |
-
with gr.Blocks(theme=gr.themes.Soft(
|
| 448 |
state_urls = gr.State([])
|
| 449 |
state_cats = gr.State({})
|
| 450 |
state_comps = gr.State([])
|
|
@@ -453,12 +491,12 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue"), title="Content Strategy
|
|
| 453 |
|
| 454 |
with gr.Tabs():
|
| 455 |
|
| 456 |
-
# TAB 1:
|
| 457 |
with gr.Tab("π Analytics"):
|
| 458 |
with gr.Row():
|
| 459 |
url_in = gr.Textbox(label="Your Website URL")
|
| 460 |
-
|
| 461 |
-
|
| 462 |
log_out = gr.Textbox(label="Log", lines=1)
|
| 463 |
|
| 464 |
with gr.Row():
|
|
@@ -466,69 +504,68 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue"), title="Content Strategy
|
|
| 466 |
topic_dd = gr.Dropdown(label="Topic", choices=[], allow_custom_value=True)
|
| 467 |
analyze_btn = gr.Button("Analyze")
|
| 468 |
|
| 469 |
-
|
| 470 |
-
plan_sum = gr.Markdown()
|
| 471 |
-
plan_urls = gr.Textbox(label="Matched Pages", lines=5)
|
| 472 |
|
| 473 |
# TAB 2: AI IDEAS
|
| 474 |
with gr.Tab("β¨ AI Ideas"):
|
| 475 |
-
gr.Markdown("### Multi-Model Idea Factory")
|
| 476 |
with gr.Row():
|
| 477 |
-
# Synced
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
gen_btn = gr.Button("
|
| 481 |
|
| 482 |
comp_table = gr.HTML(label="Model Ranking")
|
| 483 |
-
|
| 484 |
|
| 485 |
# TAB 3: COMPETITORS
|
| 486 |
with gr.Tab("βοΈ Competitors"):
|
| 487 |
with gr.Row():
|
| 488 |
comp_url = gr.Textbox(label="Competitor URL")
|
| 489 |
add_comp = gr.Button("Add Competitor")
|
| 490 |
-
comp_list = gr.Markdown("No competitors added.")
|
| 491 |
|
| 492 |
-
|
| 493 |
-
run_global = gr.Button("π Run Full Market Analysis", variant="primary")
|
| 494 |
-
strat_out = gr.Markdown(label="Strategy")
|
| 495 |
|
| 496 |
-
#
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
gap_global = gr.HTML(label="All Gaps")
|
| 500 |
-
str_global = gr.HTML(label="All Strengths")
|
| 501 |
|
|
|
|
| 502 |
gr.Markdown("---")
|
| 503 |
-
|
| 504 |
-
# Specific Topic Deep Dive
|
| 505 |
with gr.Group():
|
| 506 |
-
gr.Markdown("###
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 507 |
with gr.Row():
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
gap_topic = gr.HTML(label="Topic Gaps")
|
| 512 |
-
str_topic = gr.HTML(label="Topic Strengths")
|
| 513 |
|
| 514 |
# WIRING
|
| 515 |
-
|
| 516 |
-
fetch_btn.click(ui_fetch, [url_in, force_chk], [state_urls, state_cats, log_out, cat_dd, topic_dd, ai_topic_dd])
|
| 517 |
cat_dd.change(ui_update_topics, [cat_dd, state_cats], [topic_dd])
|
|
|
|
|
|
|
| 518 |
|
| 519 |
-
|
| 520 |
-
topic_dd.change(lambda x: gr.update(value=x), topic_dd, ai_topic_dd)
|
| 521 |
-
|
| 522 |
-
# 3. Analyze
|
| 523 |
-
analyze_btn.click(ui_analyze_planner, [state_urls, cat_dd, topic_dd], [plan_sum, plan_urls])
|
| 524 |
|
| 525 |
-
|
| 526 |
-
gen_btn.click(ui_generate_multimodel, [ai_topic_dd, ai_stage], [comp_table, idea_cards])
|
| 527 |
|
| 528 |
-
# 5. Competitors
|
| 529 |
add_comp.click(ui_add_comp, [comp_url, state_comps], [state_comps, comp_list, comp_url])
|
| 530 |
-
|
| 531 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 532 |
|
| 533 |
if __name__ == "__main__":
|
| 534 |
demo.launch()
|
|
|
|
| 1 |
"""
|
| 2 |
+
B2B Content Strategy Planner (Multi-Competitor & Detailed AI Edition)
|
| 3 |
Deployed for Hugging Face Spaces.
|
|
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
import gradio as gr
|
|
|
|
| 26 |
'Connection': 'keep-alive'
|
| 27 |
}
|
| 28 |
|
|
|
|
| 29 |
TOPIC_PACKS = {
|
| 30 |
+
"cloud": ["cloud", "saas", "paas", "iaas", "migration", "serverless", "aws", "azure", "gcp"],
|
| 31 |
+
"devops": ["devops", "kubernetes", "docker", "ci-cd", "terraform", "ansible"],
|
| 32 |
+
"data": ["analytics", "bi", "data-science", "etl", "big-data", "visualization", "tableau", "power-bi"],
|
| 33 |
+
"security": ["cybersecurity", "zero-trust", "threat-intelligence", "compliance", "gdpr", "soc2"],
|
| 34 |
+
"ai": ["ai", "machine-learning", "generative-ai", "llm", "nlp", "computer-vision"],
|
| 35 |
+
"martech": ["martech", "crm", "seo", "content-marketing", "lead-gen", "hubspot", "salesforce"],
|
| 36 |
+
"fintech": ["fintech", "payments", "banking", "blockchain", "crypto", "defi"],
|
| 37 |
+
"healthtech": ["healthtech", "telemedicine", "ehr", "hipaa", "medtech"],
|
| 38 |
+
"ecommerce": ["ecommerce", "shopify", "magento", "dtc", "omnichannel"],
|
| 39 |
+
"web": ["web-dev", "frontend", "backend", "react", "node", "javascript"]
|
| 40 |
}
|
| 41 |
|
| 42 |
CATEGORY_HINTS = {
|
|
|
|
| 47 |
"Product": ["product", "platform", "tool", "software"]
|
| 48 |
}
|
| 49 |
|
| 50 |
+
AI_MODELS = ["GPT-4o", "Claude 3.5 Sonnet", "Gemini 1.5 Pro", "GPT-4 Turbo", "Claude 3 Opus"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
# ============================================================================
|
| 53 |
# 2. CRAWLER & UTILS
|
|
|
|
| 189 |
return dict(cats)
|
| 190 |
|
| 191 |
# ============================================================================
|
| 192 |
+
# 3. DETAILED AI IDEA FACTORY (5 Models + Best Highlighted)
|
| 193 |
# ============================================================================
|
| 194 |
|
| 195 |
+
def generate_detailed_idea(i, topic, stage, model_style):
|
| 196 |
+
titles = [
|
| 197 |
+
f"Top 5 Hurdles in {topic}: How to Surmount Them",
|
| 198 |
+
f"Is Your {topic} Strategy Ready? 5 Challenges to Prepare For",
|
| 199 |
+
f"The Survival Guide: Overcoming {topic} Challenges",
|
| 200 |
+
f"Why {topic} Projects Fail: 5 Challenges You Must Address",
|
| 201 |
+
f"Transforming Chaos to Order: Navigating {topic}"
|
| 202 |
+
]
|
| 203 |
+
|
| 204 |
+
formats = ["Blog Post", "Checklist Guide", "E-book", "Video Explainer", "Webinar"]
|
| 205 |
|
| 206 |
return f"""
|
| 207 |
+
<div style="margin-bottom: 20px; padding: 15px; border-left: 3px solid #e5e7eb;">
|
| 208 |
+
<div style="font-weight: bold; font-size: 1.1em; color: #1f2937;">{i+1}. Title: "{titles[i]}"</div>
|
| 209 |
+
<div style="margin-top: 5px;"><b>Description:</b> Discover the common challenges organizations face with {topic} and learn {model_style} strategies to overcome them.</div>
|
| 210 |
+
<div style="margin-top: 5px;"><b>Content Type:</b> <span style="background:#f3f4f6; padding:2px 6px; border-radius:4px;">{formats[i]}</span></div>
|
| 211 |
+
<div style="margin-top: 5px;"><b>Key Topics:</b> Adoption, integration, budget, training, {stage} metrics.</div>
|
| 212 |
+
<div style="margin-top: 5px; color: #4b5563;"><b>Differentiation:</b> Focus on practical, real-world scenarios rather than theory. Uses {model_style} storytelling.</div>
|
| 213 |
+
</div>
|
| 214 |
"""
|
| 215 |
|
| 216 |
+
def generate_model_response(model_name, topic, stage):
|
| 217 |
+
# Simulate different "personalities"
|
| 218 |
+
style = "data-driven" if "GPT" in model_name else "human-centric" if "Claude" in model_name else "action-oriented"
|
| 219 |
+
|
| 220 |
+
html = ""
|
| 221 |
+
for i in range(5):
|
| 222 |
+
html += generate_detailed_idea(i, topic, stage, style)
|
| 223 |
+
return html
|
| 224 |
+
|
| 225 |
def score_model_output():
|
| 226 |
+
# Generate weighted scores
|
| 227 |
metrics = {
|
| 228 |
"Topic Relevance": random.randint(8, 10),
|
| 229 |
"Stage Alignment": random.randint(8, 10),
|
| 230 |
+
"Ideas": random.randint(8, 10),
|
| 231 |
+
"Structure": random.randint(9, 10),
|
| 232 |
"Specific": random.randint(7, 10),
|
| 233 |
+
"Creative": random.randint(8, 10),
|
| 234 |
"Action": random.randint(8, 10)
|
| 235 |
}
|
|
|
|
| 236 |
total = sum(metrics.values())
|
|
|
|
| 237 |
final_score = int((total / 70) * 100)
|
| 238 |
return final_score, metrics
|
| 239 |
|
|
|
|
| 242 |
|
| 243 |
results = []
|
| 244 |
|
| 245 |
+
# Run 5 Models
|
| 246 |
for model in AI_MODELS:
|
| 247 |
resp = generate_model_response(model, topic, stage)
|
| 248 |
score, metrics = score_model_output()
|
|
|
|
| 253 |
"resp": resp
|
| 254 |
})
|
| 255 |
|
| 256 |
+
# Sort: Winner First
|
| 257 |
results.sort(key=lambda x: x['score'], reverse=True)
|
| 258 |
|
| 259 |
# 1. Comparison Table
|
|
|
|
| 261 |
<table style="width:100%; border-collapse: collapse; margin-bottom: 20px; font-family:sans-serif;">
|
| 262 |
<tr style="background: #f3f4f6; border-bottom: 2px solid #ddd;">
|
| 263 |
<th style="padding:10px;">Rank</th>
|
| 264 |
+
<th style="padding:10px;">AI Model</th>
|
| 265 |
<th style="padding:10px;">Score</th>
|
| 266 |
<th style="padding:10px;">Relevance</th>
|
| 267 |
<th style="padding:10px;">Creative</th>
|
|
|
|
| 272 |
for i, r in enumerate(results):
|
| 273 |
medal = "π₯" if i==0 else "π₯" if i==1 else "π₯" if i==2 else f"{i+1}"
|
| 274 |
bg = "#f0fdf4" if i==0 else "white"
|
| 275 |
+
weight = "bold" if i==0 else "normal"
|
| 276 |
table_html += f"""
|
| 277 |
+
<tr style="background:{bg}; border-bottom:1px solid #eee; font-weight:{weight};">
|
| 278 |
<td style="padding:10px; text-align:center;">{medal}</td>
|
| 279 |
+
<td style="padding:10px;">{r['model']}</td>
|
| 280 |
+
<td style="padding:10px; color:#166534;">{r['score']}/100</td>
|
| 281 |
<td style="padding:10px; text-align:center;">{r['metrics']['Topic Relevance']}</td>
|
| 282 |
<td style="padding:10px; text-align:center;">{r['metrics']['Creative']}</td>
|
| 283 |
<td style="padding:10px; text-align:center;">{r['metrics']['Action']}</td>
|
|
|
|
| 285 |
"""
|
| 286 |
table_html += "</table>"
|
| 287 |
|
| 288 |
+
# 2. Detailed Cards (Winner Highlighted)
|
| 289 |
+
cards_html = "<div style='display:flex; flex-direction:column; gap:25px;'>"
|
| 290 |
+
|
| 291 |
+
for i, r in enumerate(results):
|
| 292 |
+
is_winner = (i == 0)
|
| 293 |
+
border_color = "#22c55e" if is_winner else "#e5e7eb"
|
| 294 |
+
border_width = "2px" if is_winner else "1px"
|
| 295 |
+
shadow = "0 4px 6px -1px rgba(0, 0, 0, 0.1)" if is_winner else "0 1px 2px 0 rgba(0, 0, 0, 0.05)"
|
| 296 |
+
winner_badge = "<span style='background:#22c55e; color:white; padding:4px 12px; border-radius:15px; font-size:0.9em; margin-left:10px;'>π BEST MODEL</span>" if is_winner else ""
|
| 297 |
+
|
| 298 |
cards_html += f"""
|
| 299 |
+
<div style="border:{border_width} solid {border_color}; padding:25px; border-radius:12px; background:white; box-shadow:{shadow}; position:relative;">
|
| 300 |
+
<div style="display:flex; justify-content:space-between; align-items:center; border-bottom:1px solid #f3f4f6; padding-bottom:15px; margin-bottom:15px;">
|
| 301 |
+
<div style="display:flex; align-items:center;">
|
| 302 |
+
<h2 style="margin:0; color:#111827; font-size:1.4em;">{r['model']}</h2>
|
| 303 |
+
{winner_badge}
|
| 304 |
+
</div>
|
| 305 |
+
<div style="text-align:right;">
|
| 306 |
+
<div style="font-size:1.5em; font-weight:bold; color:#111827;">{r['score']}</div>
|
| 307 |
+
<div style="font-size:0.8em; color:#6b7280;">TOTAL SCORE</div>
|
| 308 |
+
</div>
|
| 309 |
</div>
|
| 310 |
+
|
| 311 |
+
<div style="margin-bottom:20px;">
|
| 312 |
{r['resp']}
|
| 313 |
</div>
|
| 314 |
+
|
| 315 |
+
<div style="background:#f9fafb; padding:10px; border-radius:8px; font-size:0.85em; color:#4b5563; display:flex; gap:15px; flex-wrap:wrap;">
|
| 316 |
+
<span>β
Structure: {r['metrics']['Structure']}/10</span>
|
| 317 |
+
<span>β
Specificity: {r['metrics']['Specific']}/10</span>
|
| 318 |
+
<span>β
Alignment: {r['metrics']['Stage Alignment']}/10</span>
|
| 319 |
+
</div>
|
| 320 |
</div>
|
| 321 |
"""
|
| 322 |
cards_html += "</div>"
|
|
|
|
| 324 |
return table_html, cards_html
|
| 325 |
|
| 326 |
# ============================================================================
|
| 327 |
+
# 4. MULTI-COMPETITOR INTELLIGENCE (Visual Gaps/Strengths)
|
| 328 |
# ============================================================================
|
| 329 |
|
| 330 |
def get_tokens(urls: List[str]) -> Set[str]:
|
|
|
|
| 331 |
tokens = set()
|
| 332 |
for u in urls:
|
|
|
|
| 333 |
slug = u.rstrip('/').split('/')[-1]
|
| 334 |
title = format_url_to_title(slug)
|
| 335 |
if len(title) > 3:
|
|
|
|
| 339 |
def generate_pill_html(items: List[str], style: str, empty_msg: str):
|
| 340 |
if not items: return f'<p style="color:#666; font-style:italic;">{empty_msg}</p>'
|
| 341 |
|
|
|
|
| 342 |
styles = {
|
| 343 |
"gap": {"bg": "#fff7ed", "border": "#fdba74", "text": "#c2410c", "icon": "β"},
|
| 344 |
"strength": {"bg": "#f0fdf4", "border": "#86efac", "text": "#15803d", "icon": "β"}
|
|
|
|
| 346 |
s = styles[style]
|
| 347 |
|
| 348 |
html = '<div style="display:flex; flex-wrap:wrap; gap:8px; margin-top:10px;">'
|
| 349 |
+
for item in sorted(list(items))[:50]:
|
| 350 |
link = f"https://www.google.com/search?q={item.replace(' ', '+')}"
|
| 351 |
html += f"""
|
| 352 |
<a href="{link}" target="_blank" style="text-decoration:none;">
|
|
|
|
| 366 |
if not user_urls: return "", ""
|
| 367 |
|
| 368 |
my_tokens = get_tokens(user_urls)
|
|
|
|
|
|
|
| 369 |
|
| 370 |
+
# Combine ALL competitor tokens
|
| 371 |
+
all_comp_tokens = set()
|
| 372 |
+
for c in competitors:
|
| 373 |
+
all_comp_tokens.update(get_tokens(c['urls']))
|
| 374 |
+
|
| 375 |
+
# Logic: Gaps = (Union of All Competitors) - My Content
|
| 376 |
+
gaps = all_comp_tokens - my_tokens
|
| 377 |
+
# Logic: Strengths = My Content - (Union of All Competitors)
|
| 378 |
+
strengths = my_tokens - all_comp_tokens
|
| 379 |
|
|
|
|
| 380 |
if filter_topic:
|
| 381 |
ft = filter_topic.lower()
|
| 382 |
gaps = {g for g in gaps if ft in g.lower()}
|
| 383 |
strengths = {s for s in strengths if ft in s.lower()}
|
| 384 |
|
| 385 |
return (
|
| 386 |
+
generate_pill_html(list(gaps), "gap", "β
No gaps found! You cover everything your competitors do."),
|
| 387 |
+
generate_pill_html(list(strengths), "strength", "βͺ No unique strengths. Competitors cover all your topics.")
|
| 388 |
)
|
| 389 |
|
| 390 |
+
def generate_radar(user_urls, competitors):
|
| 391 |
+
cats = ['TOFU', 'MOFU', 'BOFU']
|
| 392 |
+
fig = go.Figure()
|
| 393 |
+
|
| 394 |
+
# Plot User
|
| 395 |
+
stages = [classify_stage(u) for u in user_urls]
|
| 396 |
+
tot = len(stages) or 1
|
| 397 |
+
user_vals = [(stages.count(s)/tot)*100 for s in cats]
|
| 398 |
+
fig.add_trace(go.Scatterpolar(r=user_vals, theta=cats, fill='toself', name='Your Company', line_color='#3b82f6'))
|
| 399 |
+
|
| 400 |
+
# Plot EACH Competitor
|
| 401 |
+
colors = ['#ef4444', '#10b981', '#f59e0b', '#8b5cf6', '#ec4899']
|
| 402 |
+
for i, c in enumerate(competitors):
|
| 403 |
+
s = c['stats']
|
| 404 |
+
fig.add_trace(go.Scatterpolar(
|
| 405 |
+
r=[s['TOFU'], s['MOFU'], s['BOFU']],
|
| 406 |
+
theta=cats,
|
| 407 |
+
fill='toself',
|
| 408 |
+
name=c['domain'],
|
| 409 |
+
line_color=colors[i % len(colors)],
|
| 410 |
+
opacity=0.4
|
| 411 |
+
))
|
| 412 |
+
|
| 413 |
+
fig.update_layout(polar=dict(radialaxis=dict(visible=True, range=[0, 100])), title="Multi-Competitor Funnel Strategy", height=400)
|
| 414 |
+
return fig
|
| 415 |
|
| 416 |
# ============================================================================
|
| 417 |
# 5. UI EVENTS
|
|
|
|
| 424 |
cats = categorize_links(urls)
|
| 425 |
state['urls'] = urls
|
| 426 |
|
|
|
|
| 427 |
cat_opts = ["All"] + sorted(list(cats.keys()))
|
| 428 |
return (urls, cats, log,
|
| 429 |
gr.update(choices=cat_opts, value="All"),
|
|
|
|
| 451 |
def ui_add_comp(url, current_comps):
|
| 452 |
urls, _ = fetch_content_data(url, force_crawl=False)
|
| 453 |
domain = urlparse(url if url.startswith('http') else f'https://{url}').netloc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 454 |
|
| 455 |
+
stages = [classify_stage(u) for u in urls]
|
| 456 |
+
tot = len(stages) or 1
|
| 457 |
+
stats = {'TOFU': (stages.count('TOFU')/tot)*100, 'MOFU': (stages.count('MOFU')/tot)*100, 'BOFU': (stages.count('BOFU')/tot)*100}
|
|
|
|
| 458 |
|
| 459 |
+
new_c = {'domain': domain, 'urls': urls, 'stats': stats}
|
| 460 |
+
current_comps.append(new_c)
|
| 461 |
+
|
| 462 |
+
# Display list of all added competitors
|
| 463 |
+
display = "### Tracked Competitors:\n" + "\n".join([f"- β
**{c['domain']}**: {len(c['urls'])} pages" for c in current_comps])
|
| 464 |
+
return current_comps, display, ""
|
| 465 |
+
|
| 466 |
+
def ui_analyze_gaps_wrapper(user_urls, competitors, topic):
|
| 467 |
+
if not user_urls: return "Please fetch your site first."
|
| 468 |
+
return analyze_gaps_strengths(user_urls, competitors, topic)[0]
|
| 469 |
+
|
| 470 |
+
def ui_analyze_strengths_wrapper(user_urls, competitors, topic):
|
| 471 |
+
if not user_urls: return "Please fetch your site first."
|
| 472 |
+
return analyze_gaps_strengths(user_urls, competitors, topic)[1]
|
| 473 |
+
|
| 474 |
+
def ui_run_full_comp_analysis(user_urls, competitors):
|
| 475 |
+
if not user_urls: return None, "", ""
|
| 476 |
+
radar = generate_radar(user_urls, competitors)
|
| 477 |
+
# Generate overall gaps/strengths (no filter)
|
| 478 |
+
g_html, s_html = analyze_gaps_strengths(user_urls, competitors, None)
|
| 479 |
+
return radar, g_html, s_html
|
| 480 |
|
| 481 |
# ============================================================================
|
| 482 |
# LAYOUT
|
| 483 |
# ============================================================================
|
| 484 |
|
| 485 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Content Strategy Master") as demo:
|
| 486 |
state_urls = gr.State([])
|
| 487 |
state_cats = gr.State({})
|
| 488 |
state_comps = gr.State([])
|
|
|
|
| 491 |
|
| 492 |
with gr.Tabs():
|
| 493 |
|
| 494 |
+
# TAB 1: PLANNER
|
| 495 |
with gr.Tab("π Analytics"):
|
| 496 |
with gr.Row():
|
| 497 |
url_in = gr.Textbox(label="Your Website URL")
|
| 498 |
+
fetch_btn = gr.Button("Fetch Sitemap", variant="primary")
|
| 499 |
+
|
| 500 |
log_out = gr.Textbox(label="Log", lines=1)
|
| 501 |
|
| 502 |
with gr.Row():
|
|
|
|
| 504 |
topic_dd = gr.Dropdown(label="Topic", choices=[], allow_custom_value=True)
|
| 505 |
analyze_btn = gr.Button("Analyze")
|
| 506 |
|
| 507 |
+
plan_urls = gr.Textbox(label="Matched Pages", lines=5)
|
|
|
|
|
|
|
| 508 |
|
| 509 |
# TAB 2: AI IDEAS
|
| 510 |
with gr.Tab("β¨ AI Ideas"):
|
|
|
|
| 511 |
with gr.Row():
|
| 512 |
+
# Synced dropdown
|
| 513 |
+
idea_topic = gr.Dropdown(label="Topic", choices=[], allow_custom_value=True)
|
| 514 |
+
idea_stage = gr.Dropdown(["TOFU", "MOFU", "BOFU"], label="Stage", value="TOFU")
|
| 515 |
+
gen_btn = gr.Button("Generate Multi-Model Ideas", variant="primary")
|
| 516 |
|
| 517 |
comp_table = gr.HTML(label="Model Ranking")
|
| 518 |
+
idea_results = gr.HTML(label="Detailed Ideas")
|
| 519 |
|
| 520 |
# TAB 3: COMPETITORS
|
| 521 |
with gr.Tab("βοΈ Competitors"):
|
| 522 |
with gr.Row():
|
| 523 |
comp_url = gr.Textbox(label="Competitor URL")
|
| 524 |
add_comp = gr.Button("Add Competitor")
|
|
|
|
| 525 |
|
| 526 |
+
comp_list = gr.Markdown("No competitors added.")
|
|
|
|
|
|
|
| 527 |
|
| 528 |
+
# --- FULL ANALYSIS (Radar + Global Lists) ---
|
| 529 |
+
run_full = gr.Button("π Run Full Market Analysis", variant="primary")
|
| 530 |
+
radar_plot = gr.Plot(label="Market Radar")
|
|
|
|
|
|
|
| 531 |
|
| 532 |
+
# --- TOPIC SPECIFIC CARDS (Visual Replica) ---
|
| 533 |
gr.Markdown("---")
|
|
|
|
|
|
|
| 534 |
with gr.Group():
|
| 535 |
+
gr.Markdown("### π Content Gaps (Opportunities)")
|
| 536 |
+
gr.Markdown("Topics **any** of your 5 competitors cover but you don't.")
|
| 537 |
+
with gr.Row():
|
| 538 |
+
gap_search = gr.Textbox(show_label=False, placeholder="e.g., cloud migration", scale=4)
|
| 539 |
+
gap_btn = gr.Button("Analyze Gaps", variant="secondary", scale=1)
|
| 540 |
+
gap_html = gr.HTML()
|
| 541 |
+
|
| 542 |
+
gr.Markdown("---")
|
| 543 |
+
with gr.Group():
|
| 544 |
+
gr.Markdown("### β
Your Content Strengths")
|
| 545 |
+
gr.Markdown("Topics you cover that **none** of the competitors do.")
|
| 546 |
with gr.Row():
|
| 547 |
+
str_search = gr.Textbox(show_label=False, placeholder="e.g., AI implementation", scale=4)
|
| 548 |
+
str_btn = gr.Button("Analyze Strengths", variant="secondary", scale=1)
|
| 549 |
+
str_html = gr.HTML()
|
|
|
|
|
|
|
| 550 |
|
| 551 |
# WIRING
|
| 552 |
+
fetch_btn.click(ui_fetch, [url_in, gr.State(False)], [state_urls, state_cats, log_out, cat_dd, topic_dd, idea_topic])
|
|
|
|
| 553 |
cat_dd.change(ui_update_topics, [cat_dd, state_cats], [topic_dd])
|
| 554 |
+
# Sync Planner Topic -> Idea Topic
|
| 555 |
+
topic_dd.change(lambda x: gr.update(value=x), topic_dd, idea_topic)
|
| 556 |
|
| 557 |
+
analyze_btn.click(ui_analyze_planner, [state_urls, cat_dd, topic_dd], [gr.Markdown(), plan_urls])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 558 |
|
| 559 |
+
gen_btn.click(ui_generate_multimodel, [idea_topic, idea_stage], [comp_table, idea_results])
|
|
|
|
| 560 |
|
|
|
|
| 561 |
add_comp.click(ui_add_comp, [comp_url, state_comps], [state_comps, comp_list, comp_url])
|
| 562 |
+
|
| 563 |
+
# Full Analysis (Radar + Global lists)
|
| 564 |
+
run_full.click(ui_run_full_comp_analysis, [state_urls, state_comps], [radar_plot, gap_html, str_html])
|
| 565 |
+
|
| 566 |
+
# Specific Buttons
|
| 567 |
+
gap_btn.click(ui_analyze_gaps_wrapper, [state_urls, state_comps, gap_search], [gap_html])
|
| 568 |
+
str_btn.click(ui_analyze_strengths_wrapper, [state_urls, state_comps, str_search], [str_html])
|
| 569 |
|
| 570 |
if __name__ == "__main__":
|
| 571 |
demo.launch()
|