Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
from bs4 import BeautifulSoup
|
| 4 |
+
import nltk
|
| 5 |
+
from nltk.corpus import stopwords
|
| 6 |
+
from nltk.tokenize import word_tokenize
|
| 7 |
+
import string
|
| 8 |
+
|
| 9 |
+
nltk.download('punkt')
|
| 10 |
+
nltk.download('stopwords')
|
| 11 |
+
|
| 12 |
+
def get_text_from_url(url):
|
| 13 |
+
try:
|
| 14 |
+
response = requests.get(url, timeout=10)
|
| 15 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 16 |
+
text = soup.get_text()
|
| 17 |
+
return text
|
| 18 |
+
except:
|
| 19 |
+
return ""
|
| 20 |
+
|
| 21 |
+
def extract_keywords(text):
|
| 22 |
+
text = text.lower()
|
| 23 |
+
tokens = word_tokenize(text)
|
| 24 |
+
words = [word for word in tokens if word.isalnum()]
|
| 25 |
+
stop_words = set(stopwords.words('english'))
|
| 26 |
+
keywords = [word for word in words if word not in stop_words]
|
| 27 |
+
return set(keywords)
|
| 28 |
+
|
| 29 |
+
def compare_keywords(url_a, url_b):
|
| 30 |
+
text_a = get_text_from_url(url_a)
|
| 31 |
+
text_b = get_text_from_url(url_b)
|
| 32 |
+
|
| 33 |
+
if not text_a or not text_b:
|
| 34 |
+
return "❌ Failed to fetch one or both websites. Please check URLs."
|
| 35 |
+
|
| 36 |
+
keywords_a = extract_keywords(text_a)
|
| 37 |
+
keywords_b = extract_keywords(text_b)
|
| 38 |
+
|
| 39 |
+
diff_keywords = keywords_b - keywords_a
|
| 40 |
+
|
| 41 |
+
sorted_keywords = sorted(diff_keywords)
|
| 42 |
+
return "\n".join(sorted_keywords)
|
| 43 |
+
|
| 44 |
+
interface = gr.Interface(
|
| 45 |
+
fn=compare_keywords,
|
| 46 |
+
inputs=[
|
| 47 |
+
gr.Textbox(label="Your Website (A)"),
|
| 48 |
+
gr.Textbox(label="Competitor Website (B)")
|
| 49 |
+
],
|
| 50 |
+
outputs="text",
|
| 51 |
+
title="🔍 Competitor Keyword Finder",
|
| 52 |
+
description="Enter your website and a competitor's. This tool finds keywords the competitor uses but you don’t."
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
interface.launch()
|