yukee1992 commited on
Commit
ff189fe
·
verified ·
1 Parent(s): 87a4f9d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -21
app.py CHANGED
@@ -1,45 +1,118 @@
1
  import gradio as gr
 
 
2
  import spaces
3
 
4
  # ============================================================
5
- # STEP 1: Add the required @spaces.GPU decorator
6
- # This satisfies Hugging Face's startup requirement
7
  # ============================================================
8
 
9
  @spaces.GPU(duration=1)
10
  def noop_gpu_function():
11
- """
12
- This dummy function satisfies the Hugging Face Space
13
- startup requirement. It doesn't actually use GPU.
14
- """
15
  pass
16
 
17
  # ============================================================
18
- # STEP 2: Your main function (the "Hello World")
19
  # ============================================================
20
 
21
- def greet(name):
22
- """Simple greeting function"""
23
- return f"Hello {name}! Your scraper is working! 🎉"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  # ============================================================
26
- # STEP 3: Create the Gradio Interface
27
  # ============================================================
28
 
29
  demo = gr.Interface(
30
- fn=greet,
31
- inputs=gr.Textbox(
32
- label="Your Name",
33
- placeholder="Enter your name...",
34
- value="World"
35
- ),
36
- outputs=gr.Textbox(label="Greeting"),
37
- title="🕷️ Market Scraper - Test",
38
- description="This is a test to verify your Space is working."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  )
40
 
41
  # ============================================================
42
- # STEP 4: Launch (MUST be at module level, NOT inside if __name__)
43
  # ============================================================
44
 
45
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
1
  import gradio as gr
2
+ import requests
3
+ from bs4 import BeautifulSoup
4
  import spaces
5
 
6
  # ============================================================
7
+ # REQUIRED: @spaces.GPU decorator for Hugging Face
 
8
  # ============================================================
9
 
10
  @spaces.GPU(duration=1)
11
  def noop_gpu_function():
12
+ """Dummy function to satisfy Hugging Face startup"""
 
 
 
13
  pass
14
 
15
  # ============================================================
16
+ # SCRAPER FUNCTION
17
  # ============================================================
18
 
19
+ def scrape_website(url, keyword, max_results=5):
20
+ """Scrape a website and extract links containing the keyword"""
21
+ results = []
22
+
23
+ # Ensure URL has protocol
24
+ if not url.startswith(('http://', 'https://')):
25
+ url = 'https://' + url
26
+
27
+ try:
28
+ # Fetch the webpage
29
+ headers = {
30
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
31
+ }
32
+ response = requests.get(url, headers=headers, timeout=30)
33
+ response.raise_for_status()
34
+
35
+ # Parse HTML
36
+ soup = BeautifulSoup(response.text, 'html.parser')
37
+
38
+ # Remove unwanted elements
39
+ for element in soup(['script', 'style', 'nav', 'footer', 'header']):
40
+ element.decompose()
41
+
42
+ # Find all links
43
+ links = soup.find_all('a', href=True)
44
+
45
+ keyword_lower = keyword.lower()
46
+ for link in links:
47
+ try:
48
+ href = link.get('href', '')
49
+ text = link.get_text(strip=True)
50
+
51
+ # Skip invalid links
52
+ if not href or href.startswith('#') or href.startswith('javascript:') or not text:
53
+ continue
54
+
55
+ # Check if keyword is in the link text or URL
56
+ if keyword_lower in text.lower() or keyword_lower in href.lower():
57
+ results.append({
58
+ 'title': text[:100],
59
+ 'url': href,
60
+ 'source': url
61
+ })
62
+
63
+ if len(results) >= max_results:
64
+ break
65
+
66
+ except Exception as e:
67
+ continue
68
+
69
+ # Format results for display
70
+ if results:
71
+ output = f"✅ **Found {len(results)} results for '{keyword}':**\n\n"
72
+ for i, item in enumerate(results, 1):
73
+ output += f"**{i}. {item['title']}**\n"
74
+ output += f" 🔗 {item['url']}\n\n"
75
+ return output
76
+ else:
77
+ return f"⚠️ No results found for '{keyword}' at {url}"
78
+
79
+ except requests.exceptions.RequestException as e:
80
+ return f"❌ Network error: {str(e)}"
81
+ except Exception as e:
82
+ return f"❌ Error: {str(e)}"
83
 
84
  # ============================================================
85
+ # GRADIO INTERFACE
86
  # ============================================================
87
 
88
  demo = gr.Interface(
89
+ fn=scrape_website,
90
+ inputs=[
91
+ gr.Textbox(
92
+ label="URL",
93
+ placeholder="https://www.pinterest.com/search/pins/?q=planner",
94
+ value="https://www.pinterest.com/search/pins/?q=planner"
95
+ ),
96
+ gr.Textbox(
97
+ label="Keyword",
98
+ placeholder="planner, notion, template, etc.",
99
+ value="planner"
100
+ ),
101
+ gr.Slider(
102
+ minimum=1,
103
+ maximum=20,
104
+ value=5,
105
+ step=1,
106
+ label="Max Results"
107
+ )
108
+ ],
109
+ outputs=gr.Markdown(label="Results"),
110
+ title="🕷️ Market Scraper",
111
+ description="Scrape websites to find content related to your market research keywords."
112
  )
113
 
114
  # ============================================================
115
+ # LAUNCH (MUST be at module level)
116
  # ============================================================
117
 
118
  demo.launch(server_name="0.0.0.0", server_port=7860)