mgbam commited on
Commit
5db3b89
Β·
verified Β·
1 Parent(s): 724073a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -109
app.py CHANGED
@@ -7,48 +7,34 @@ from langchain_core.messages import HumanMessage
7
  import os
8
  import re
9
 
10
- # --- Configuration & Initialization ---
11
- # For deployment on Hugging Face, set GEMINI_API_KEY in the Space's secrets.
12
- # AHREFS_API_KEY is optional for now, as we will simulate the data.
13
- AHREFS_API_KEY = os.environ.get("AHREFS_API_KEY")
14
-
15
- # --- Core Analysis Functions ---
16
 
 
17
  def fetch_html(url: str) -> str | None:
18
- """Fetches HTML content from a URL with a browser-like user-agent."""
19
  try:
20
- headers = {
21
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
22
- }
23
  response = requests.get(url, headers=headers, timeout=10)
24
- response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
25
  return response.text
26
- except requests.RequestException as e:
27
- print(f"Error fetching {url}: {e}")
28
  return None
29
 
30
  def analyze_onpage_seo(soup: BeautifulSoup) -> dict:
31
- """Analyzes the on-page SEO elements of a webpage."""
32
  title = soup.find('title').get_text(strip=True) if soup.find('title') else "Not found"
33
  description_tag = soup.find('meta', attrs={'name': 'description'})
34
  description = description_tag['content'] if description_tag and description_tag.has_attr('content') else "Not found"
35
-
36
  headings = {'h1': [], 'h2': []}
37
  for h_tag in ['h1', 'h2']:
38
  for tag in soup.find_all(h_tag):
39
  headings[h_tag].append(tag.get_text(strip=True))
40
-
41
  word_count = len(soup.get_text(separator=' ', strip=True).split())
42
-
43
- return {
44
- "title": title,
45
- "description": description,
46
- "headings": headings,
47
- "word_count": word_count
48
- }
49
 
50
  def analyze_tech_stack(html: str) -> list[str]:
51
- """Performs a basic analysis of the technologies used on the page."""
52
  tech = set()
53
  if "react.js" in html or 'data-reactroot' in html: tech.add("React")
54
  if "vue.js" in html: tech.add("Vue.js")
@@ -57,62 +43,32 @@ def analyze_tech_stack(html: str) -> list[str]:
57
  if "GTM-" in html: tech.add("Google Tag Manager")
58
  if "tailwind" in html: tech.add("Tailwind CSS")
59
  if "shopify" in html: tech.add("Shopify")
60
-
61
  return list(tech) if tech else ["Basic HTML/CSS"]
62
 
63
  def analyze_ads_and_keywords(domain: str) -> dict:
64
- """
65
- Simulates fetching paid keywords and ad data from a service like Ahrefs.
66
- This provides a 'wow' demo for specific, well-known sites.
67
- """
68
- # In a real product with a subscription, you would uncomment this block:
69
- # if not AHREFS_API_KEY:
70
- # return {"error": "This feature requires a Pro subscription to an SEO data provider."}
71
- # url = f"https://api.ahrefs.com/v3/...?target={domain}"
72
- # response = requests.get(url, headers={"Authorization": f"Bearer {AHREFS_API_KEY}"})
73
- # return response.json()
74
-
75
  print(f"Simulating Ads & Keywords API call for {domain}")
76
  if "notion" in domain:
77
- return {
78
- "keywords": [
79
- {"keyword": "what is notion", "volume": 65000, "cpc": 0.50},
80
- {"keyword": "notion templates", "volume": 45000, "cpc": 1.20},
81
- {"keyword": "second brain app", "volume": 12000, "cpc": 2.50},
82
- {"keyword": "project management software", "volume": 25000, "cpc": 8.00},
83
- ],
84
- "ads": [
85
- {"title": "Notion – Your All-in-One Workspace", "text": "Organize your life and work. From notes and docs, to projects and wikis, Notion is all you need."},
86
- {"title": "The Best Second Brain App | Notion", "text": "Stop juggling tools. Notion combines everything you need to think, write, and plan in one place."},
87
- ]
88
- }
89
  if "mailchimp" in domain:
90
- return {
91
- "keywords": [
92
- {"keyword": "email marketing", "volume": 90500, "cpc": 15.50},
93
- {"keyword": "free email marketing tools", "volume": 14800, "cpc": 12.00},
94
- {"keyword": "newsletter software", "volume": 8100, "cpc": 9.50},
95
- ],
96
- "ads": [
97
- {"title": "Mailchimp: Marketing & Email", "text": "Grow your business with Mailchimp's All-in-One marketing, automation & email marketing platform."},
98
- ]
99
- }
100
  return {"keywords": [], "ads": []}
101
 
 
102
  def generate_ai_summary(url: str, seo_data: dict, ads_data: dict) -> str:
103
- """Generates a high-level strategic summary using an LLM."""
104
- api_key = os.environ.get("GEMINI_API_KEY")
105
- if not api_key:
106
- return "⚠️ **AI Summary Unavailable:** The `GEMINI_API_KEY` is not set in the Space secrets. Please ask the Space owner to add it."
107
 
108
  try:
109
- llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash", google_api_key=api_key)
110
 
111
  ads_summary = "They do not appear to be running any significant Google Ads campaigns."
112
  if ads_data and ads_data.get('keywords'):
113
  top_keyword = ads_data['keywords'][0]
114
  ads_summary = f"They are actively running Google Ads, primarily bidding on high-intent keywords like **'{top_keyword['keyword']}'**."
115
 
 
 
116
  prompt = f"""
117
  As a world-class marketing strategist, analyze the data for the website `{url}` and provide a concise, actionable summary in markdown format.
118
 
@@ -123,27 +79,20 @@ def generate_ai_summary(url: str, seo_data: dict, ads_data: dict) -> str:
123
  - **🎯 Core Marketing Angle:** What is their main value proposition and selling point?
124
  - **πŸ“ˆ Customer Acquisition Focus:** Based on the data, are they focused more on organic SEO or paid advertising?
125
  - **πŸ’‘ One Actionable Insight:** What is one clever tactic they're using, or one key opportunity they are missing?
 
 
126
  """
127
  response = llm.invoke([HumanMessage(content=prompt)])
128
  return response.content
129
  except Exception as e:
130
  return f"⚠️ **AI Summary Failed:** The API call could not be completed. Error: {e}"
131
 
132
- # --- Main Orchestrator ---
133
-
134
  def competitor_teardown(url: str):
135
- """The main function that runs the entire analysis pipeline."""
136
- # Define the initial state for all outputs
137
- outputs = {
138
- "summary": " ", "seo": " ", "tech": " ", "ads": " ",
139
- "btn": gr.Button("Analyzing...", interactive=False)
140
- }
141
- # Immediately update the UI to show the "Analyzing..." state
142
  yield list(outputs.values())
143
-
144
- # --- 1. Data Fetching ---
145
- if not url.startswith(('http://', 'https://')):
146
- url = 'https://' + url
147
  domain_match = re.search(r'https?://(?:www\.)?([^/]+)', url)
148
  if not domain_match:
149
  outputs["summary"] = "❌ **Invalid URL:** Please enter a valid website address like `notion.so`."
@@ -151,56 +100,43 @@ def competitor_teardown(url: str):
151
  yield list(outputs.values())
152
  return
153
  domain = domain_match.group(1)
154
-
155
  html = fetch_html(url)
156
  if not html:
157
  outputs["summary"] = f"❌ **Fetch Failed:** Could not retrieve content from `{url}`. The site may be down or blocking scrapers."
158
  outputs["btn"] = gr.Button("Analyze", interactive=True)
159
  yield list(outputs.values())
160
  return
161
-
162
  soup = BeautifulSoup(html, 'html.parser')
163
-
164
- # --- 2. Run All Analyses ---
165
  seo_data = analyze_onpage_seo(soup)
166
  tech_data = analyze_tech_stack(html)
167
  ads_data = analyze_ads_and_keywords(domain)
168
  ai_summary = generate_ai_summary(url, seo_data, ads_data)
169
-
170
- # --- 3. Prepare Rich Markdown Outputs ---
171
  outputs["summary"] = ai_summary
172
-
173
- outputs["seo"] = f"""
174
- ### πŸ‘‘ SEO & Content Analysis
175
- | Metric | Value |
176
- | :--- | :--- |
177
- | **Page Title** | `{seo_data['title']}` |
178
- | **Meta Description** | `{seo_data['description']}` |
179
- | **Word Count** | `{seo_data['word_count']:,}` |
180
-
181
- #### Heading Structure
182
- - **H1 Tags ({len(seo_data['headings']['h1'])}):** {', '.join(f'`{h}`' for h in seo_data['headings']['h1']) if seo_data['headings']['h1'] else 'None Found'}
183
- - **H2 Tags ({len(seo_data['headings']['h2'])}):** {len(seo_data['headings']['h2'])} found
184
- """
185
-
186
  outputs["tech"] = "### βš™οΈ Technology Stack\n\n" + "\n".join([f"- `{t}`" for t in tech_data])
187
-
188
  if ads_data.get("keywords"):
189
  df = pd.DataFrame(ads_data["keywords"])
190
  df['cpc'] = df['cpc'].apply(lambda x: f"${x:.2f}")
191
  ads_md = "### πŸ“’ Paid Ads & Keywords\nThis competitor is actively bidding on Google Search ads. Here are their top keywords:\n\n"
192
  ads_md += df.to_markdown(index=False)
193
  ads_md += "\n\n### ✍️ Sample Ad Copy\n\n"
194
- for ad in ads_data["ads"]:
195
- ads_md += f"**{ad['title']}**\n\n>{ad['text']}\n\n---\n\n"
196
  outputs["ads"] = ads_md
197
  else:
198
  outputs["ads"] = "### πŸ“’ Paid Ads & Keywords\n\nNo significant paid advertising activity was detected for this domain."
199
-
200
  outputs["btn"] = gr.Button("Analyze", interactive=True)
201
  yield list(outputs.values())
202
 
203
- # --- Gradio UI Definition ---
204
  with gr.Blocks(theme=gr.themes.Soft(), css="footer {display: none !important;}") as demo:
205
  gr.Markdown("# πŸ•΅οΈ Gumbo Board: The Instant Competitor Teardown")
206
  gr.Markdown("Enter a competitor's website to get an instant analysis of their online strategy. *Powered by Gumbo (BeautifulSoup) & AI.*")
@@ -219,17 +155,20 @@ with gr.Blocks(theme=gr.themes.Soft(), css="footer {display: none !important;}")
219
  with gr.TabItem("πŸ“’ Ads & Keywords", id=3):
220
  ads_output = gr.Markdown()
221
 
222
- # Define the list of outputs in the correct order.
223
  outputs_list = [summary_output, seo_output, tech_output, ads_output, submit_btn]
224
-
225
- submit_btn.click(
226
- fn=competitor_teardown,
227
- inputs=[url_input],
228
- outputs=outputs_list
229
- )
230
 
231
  gr.Markdown("---")
232
- gr.Markdown("### Ready for More?\nGet unlimited reports, save projects, and export to PDF with our Pro plan.\n**[πŸš€ Click Here to Go Pro on Gumroad!](https://gumroad.com/)**")
 
 
 
 
 
 
 
 
 
233
 
234
  if __name__ == "__main__":
235
  demo.launch()
 
7
  import os
8
  import re
9
 
10
+ # --- Configuration ---
11
+ GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
 
 
 
 
12
 
13
+ # --- Core Analysis Functions (Unchanged) ---
14
  def fetch_html(url: str) -> str | None:
15
+ # ... (code is the same as before) ...
16
  try:
17
+ headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}
 
 
18
  response = requests.get(url, headers=headers, timeout=10)
19
+ response.raise_for_status()
20
  return response.text
21
+ except requests.RequestException:
 
22
  return None
23
 
24
  def analyze_onpage_seo(soup: BeautifulSoup) -> dict:
25
+ # ... (code is the same as before) ...
26
  title = soup.find('title').get_text(strip=True) if soup.find('title') else "Not found"
27
  description_tag = soup.find('meta', attrs={'name': 'description'})
28
  description = description_tag['content'] if description_tag and description_tag.has_attr('content') else "Not found"
 
29
  headings = {'h1': [], 'h2': []}
30
  for h_tag in ['h1', 'h2']:
31
  for tag in soup.find_all(h_tag):
32
  headings[h_tag].append(tag.get_text(strip=True))
 
33
  word_count = len(soup.get_text(separator=' ', strip=True).split())
34
+ return {"title": title, "description": description, "headings": headings, "word_count": word_count}
 
 
 
 
 
 
35
 
36
  def analyze_tech_stack(html: str) -> list[str]:
37
+ # ... (code is the same as before) ...
38
  tech = set()
39
  if "react.js" in html or 'data-reactroot' in html: tech.add("React")
40
  if "vue.js" in html: tech.add("Vue.js")
 
43
  if "GTM-" in html: tech.add("Google Tag Manager")
44
  if "tailwind" in html: tech.add("Tailwind CSS")
45
  if "shopify" in html: tech.add("Shopify")
 
46
  return list(tech) if tech else ["Basic HTML/CSS"]
47
 
48
  def analyze_ads_and_keywords(domain: str) -> dict:
49
+ # ... (code is the same as before, with simulated data) ...
 
 
 
 
 
 
 
 
 
 
50
  print(f"Simulating Ads & Keywords API call for {domain}")
51
  if "notion" in domain:
52
+ return { "keywords": [{"keyword": "what is notion", "volume": 65000, "cpc": 0.50}, {"keyword": "notion templates", "volume": 45000, "cpc": 1.20}, {"keyword": "second brain app", "volume": 12000, "cpc": 2.50}, {"keyword": "project management software", "volume": 25000, "cpc": 8.00},], "ads": [{"title": "Notion – Your All-in-One Workspace", "text": "Organize your life and work. From notes and docs, to projects and wikis, Notion is all you need."}, {"title": "The Best Second Brain App | Notion", "text": "Stop juggling tools. Notion combines everything you need to think, write, and plan in one place."},] }
 
 
 
 
 
 
 
 
 
 
 
53
  if "mailchimp" in domain:
54
+ return { "keywords": [{"keyword": "email marketing", "volume": 90500, "cpc": 15.50}, {"keyword": "free email marketing tools", "volume": 14800, "cpc": 12.00}, {"keyword": "newsletter software", "volume": 8100, "cpc": 9.50},], "ads": [{"title": "Mailchimp: Marketing & Email", "text": "Grow your business with Mailchimp's All-in-One marketing, automation & email marketing platform."},] }
 
 
 
 
 
 
 
 
 
55
  return {"keywords": [], "ads": []}
56
 
57
+ # --- AI Summary (Now with a Conversion Trigger) ---
58
  def generate_ai_summary(url: str, seo_data: dict, ads_data: dict) -> str:
59
+ if not GEMINI_API_KEY:
60
+ return "⚠️ **AI Summary Unavailable:** The `GEMINI_API_KEY` is not set in the Space secrets."
 
 
61
 
62
  try:
63
+ llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash", google_api_key=GEMINI_API_KEY)
64
 
65
  ads_summary = "They do not appear to be running any significant Google Ads campaigns."
66
  if ads_data and ads_data.get('keywords'):
67
  top_keyword = ads_data['keywords'][0]
68
  ads_summary = f"They are actively running Google Ads, primarily bidding on high-intent keywords like **'{top_keyword['keyword']}'**."
69
 
70
+ # *** NEW: CONVERSION TRIGGER IN THE PROMPT ***
71
+ # We are telling the AI to hint at deeper insights that the Pro version would provide.
72
  prompt = f"""
73
  As a world-class marketing strategist, analyze the data for the website `{url}` and provide a concise, actionable summary in markdown format.
74
 
 
79
  - **🎯 Core Marketing Angle:** What is their main value proposition and selling point?
80
  - **πŸ“ˆ Customer Acquisition Focus:** Based on the data, are they focused more on organic SEO or paid advertising?
81
  - **πŸ’‘ One Actionable Insight:** What is one clever tactic they're using, or one key opportunity they are missing?
82
+
83
+ **Finally, add a "Go Deeper" section that hints at what a full analysis could uncover, like this example: "Go Deeper: A full Pro analysis could reveal their top-performing ad copy and entire keyword portfolio."**
84
  """
85
  response = llm.invoke([HumanMessage(content=prompt)])
86
  return response.content
87
  except Exception as e:
88
  return f"⚠️ **AI Summary Failed:** The API call could not be completed. Error: {e}"
89
 
90
+ # --- Main Orchestrator (Unchanged) ---
 
91
  def competitor_teardown(url: str):
92
+ # ... (code is the same as before) ...
93
+ outputs = { "summary": " ", "seo": " ", "tech": " ", "ads": " ", "btn": gr.Button("Analyzing...", interactive=False) }
 
 
 
 
 
94
  yield list(outputs.values())
95
+ if not url.startswith(('http://', 'https://')): url = 'https://' + url
 
 
 
96
  domain_match = re.search(r'https?://(?:www\.)?([^/]+)', url)
97
  if not domain_match:
98
  outputs["summary"] = "❌ **Invalid URL:** Please enter a valid website address like `notion.so`."
 
100
  yield list(outputs.values())
101
  return
102
  domain = domain_match.group(1)
 
103
  html = fetch_html(url)
104
  if not html:
105
  outputs["summary"] = f"❌ **Fetch Failed:** Could not retrieve content from `{url}`. The site may be down or blocking scrapers."
106
  outputs["btn"] = gr.Button("Analyze", interactive=True)
107
  yield list(outputs.values())
108
  return
 
109
  soup = BeautifulSoup(html, 'html.parser')
 
 
110
  seo_data = analyze_onpage_seo(soup)
111
  tech_data = analyze_tech_stack(html)
112
  ads_data = analyze_ads_and_keywords(domain)
113
  ai_summary = generate_ai_summary(url, seo_data, ads_data)
 
 
114
  outputs["summary"] = ai_summary
115
+ outputs["seo"] = f"""### πŸ‘‘ SEO & Content Analysis
116
+ | Metric | Value |
117
+ | :--- | :--- |
118
+ | **Page Title** | `{seo_data['title']}` |
119
+ | **Meta Description** | `{seo_data['description']}` |
120
+ | **Word Count** | `{seo_data['word_count']:,}` |
121
+ #### Heading Structure
122
+ - **H1 Tags ({len(seo_data['headings']['h1'])}):** {', '.join(f'`{h}`' for h in seo_data['headings']['h1']) if seo_data['headings']['h1'] else 'None Found'}
123
+ - **H2 Tags ({len(seo_data['headings']['h2'])}):** {len(seo_data['headings']['h2'])} found
124
+ """
 
 
 
 
125
  outputs["tech"] = "### βš™οΈ Technology Stack\n\n" + "\n".join([f"- `{t}`" for t in tech_data])
 
126
  if ads_data.get("keywords"):
127
  df = pd.DataFrame(ads_data["keywords"])
128
  df['cpc'] = df['cpc'].apply(lambda x: f"${x:.2f}")
129
  ads_md = "### πŸ“’ Paid Ads & Keywords\nThis competitor is actively bidding on Google Search ads. Here are their top keywords:\n\n"
130
  ads_md += df.to_markdown(index=False)
131
  ads_md += "\n\n### ✍️ Sample Ad Copy\n\n"
132
+ for ad in ads_data["ads"]: ads_md += f"**{ad['title']}**\n\n>{ad['text']}\n\n---\n\n"
 
133
  outputs["ads"] = ads_md
134
  else:
135
  outputs["ads"] = "### πŸ“’ Paid Ads & Keywords\n\nNo significant paid advertising activity was detected for this domain."
 
136
  outputs["btn"] = gr.Button("Analyze", interactive=True)
137
  yield list(outputs.values())
138
 
139
+ # --- Gradio UI (Now with your Gumroad link and better conversion copy) ---
140
  with gr.Blocks(theme=gr.themes.Soft(), css="footer {display: none !important;}") as demo:
141
  gr.Markdown("# πŸ•΅οΈ Gumbo Board: The Instant Competitor Teardown")
142
  gr.Markdown("Enter a competitor's website to get an instant analysis of their online strategy. *Powered by Gumbo (BeautifulSoup) & AI.*")
 
155
  with gr.TabItem("πŸ“’ Ads & Keywords", id=3):
156
  ads_output = gr.Markdown()
157
 
 
158
  outputs_list = [summary_output, seo_output, tech_output, ads_output, submit_btn]
159
+ submit_btn.click(fn=competitor_teardown, inputs=[url_input], outputs=outputs_list)
 
 
 
 
 
160
 
161
  gr.Markdown("---")
162
+ # *** YOUR GUMROAD LINK AND CONVERSION-FOCUSED COPY ***
163
+ gr.Markdown("""
164
+ ### You're scratching the surface. Ready to go deeper?
165
+ The Pro version gives you the unfair advantage:
166
+ - **βœ… Unlimited Reports:** Analyze everyone in your market.
167
+ - **πŸ”‘ Live API Data:** Get real-time Ads & Keyword data for *any* domain.
168
+ - **πŸ“„ PDF Exports:** Create beautiful reports for clients or your team.
169
+
170
+ **[πŸš€ Unlock Gumbo Board Pro Now!](https://gumroadian864.gumroad.com/l/zvgxv)**
171
+ """)
172
 
173
  if __name__ == "__main__":
174
  demo.launch()