BrightData commited on
Commit
b3ecc7a
·
verified ·
1 Parent(s): 96ed627

Add Bright Data Dataset Tool

Browse files
Files changed (3) hide show
  1. app.py +33 -2
  2. requirements.txt +3 -1
  3. tool.py +49 -6
app.py CHANGED
@@ -1,5 +1,36 @@
1
- from smolagents import launch_gradio_demo
2
  from tool import BrightDataDatasetTool
3
 
4
  tool = BrightDataDatasetTool()
5
- launch_gradio_demo(tool)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
  from tool import BrightDataDatasetTool
3
 
4
  tool = BrightDataDatasetTool()
5
+
6
+ def run(dataset, url, keyword, first_name, last_name, days_limit, num_of_reviews, num_of_comments):
7
+ return tool(
8
+ dataset=dataset,
9
+ url=url,
10
+ keyword=keyword,
11
+ first_name=first_name,
12
+ last_name=last_name,
13
+ days_limit=days_limit,
14
+ num_of_reviews=num_of_reviews,
15
+ num_of_comments=num_of_comments,
16
+ )
17
+
18
+ with gr.Blocks() as demo:
19
+ gr.Markdown("### Bright Data dataset fetch")
20
+ dataset = gr.Dropdown(choices=['amazon_product', 'amazon_product_reviews', 'amazon_product_search', 'apple_app_store', 'bestbuy_products', 'booking_hotel_listings', 'crunchbase_company', 'ebay_product', 'etsy_products', 'facebook_company_reviews', 'facebook_events', 'facebook_marketplace_listings', 'facebook_posts', 'github_repository_file', 'google_maps_reviews', 'google_play_store', 'google_shopping', 'homedepot_products', 'instagram_comments', 'instagram_posts', 'instagram_profiles', 'instagram_reels', 'linkedin_company_profile', 'linkedin_job_listings', 'linkedin_people_search', 'linkedin_person_profile', 'linkedin_posts', 'reddit_posts', 'reuter_news', 'tiktok_comments', 'tiktok_posts', 'tiktok_profiles', 'tiktok_shop', 'walmart_product', 'walmart_seller', 'x_posts', 'yahoo_finance_business', 'youtube_comments', 'youtube_profiles', 'youtube_videos', 'zara_products', 'zillow_properties_listing', 'zoominfo_company_profile'], label="Dataset", multiselect=False)
21
+ url = gr.Textbox(label="URL", placeholder="https://...")
22
+ keyword = gr.Textbox(label="Keyword")
23
+ first_name = gr.Textbox(label="First name")
24
+ last_name = gr.Textbox(label="Last name")
25
+ days_limit = gr.Textbox(label="Days limit (e.g. 3)")
26
+ num_of_reviews = gr.Textbox(label="Number of reviews")
27
+ num_of_comments = gr.Textbox(label="Number of comments")
28
+ run_btn = gr.Button("Run")
29
+ output = gr.Textbox(label="Output", lines=12)
30
+ run_btn.click(
31
+ run,
32
+ inputs=[dataset, url, keyword, first_name, last_name, days_limit, num_of_reviews, num_of_comments],
33
+ outputs=output,
34
+ )
35
+
36
+ demo.launch()
requirements.txt CHANGED
@@ -1,2 +1,4 @@
 
1
  requests
2
- smolagents
 
 
1
+ gradio
2
  requests
3
+ smolagents
4
+ {tool_module_name}
tool.py CHANGED
@@ -1,9 +1,9 @@
1
  from typing import Any, Optional
2
  from smolagents.tools import Tool
3
- import json
4
  import time
5
  import os
6
- import requests
7
 
8
  class BrightDataDatasetTool(Tool):
9
  name = "brightdata_dataset_fetch"
@@ -78,7 +78,6 @@ class BrightDataDatasetTool(Tool):
78
  else:
79
  raise ValueError(f"Missing required field '{field}' for dataset '{dataset_key}'")
80
 
81
- # Apply fixed values that should always be sent
82
  payload.update(fixed_values)
83
  return payload
84
 
@@ -160,7 +159,6 @@ class BrightDataDatasetTool(Tool):
160
  if not snapshot_id:
161
  raise RuntimeError("No snapshot ID returned from Bright Data.")
162
 
163
- # Poll for completion (up to 10 minutes, matching MCP logic)
164
  snapshot_url = f"https://api.brightdata.com/datasets/v3/snapshot/{snapshot_id}"
165
  max_attempts = 600
166
  attempts = 0
@@ -174,7 +172,6 @@ class BrightDataDatasetTool(Tool):
174
  timeout=30,
175
  )
176
 
177
- # If Bright Data returns an error response we don't want to loop forever
178
  if response.status_code == 400:
179
  response.raise_for_status()
180
 
@@ -190,10 +187,56 @@ class BrightDataDatasetTool(Tool):
190
  time.sleep(1)
191
 
192
  except requests.exceptions.RequestException as exc:
193
- # Mirror JS logic: tolerate transient failures, but break on 400
194
  if getattr(getattr(exc, "response", None), "status_code", None) == 400:
195
  raise
196
  attempts += 1
197
  time.sleep(1)
198
 
199
  raise TimeoutError(f"Timeout waiting for snapshot {snapshot_id} after {max_attempts} seconds")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from typing import Any, Optional
2
  from smolagents.tools import Tool
3
+ import requests
4
  import time
5
  import os
6
+ import json
7
 
8
  class BrightDataDatasetTool(Tool):
9
  name = "brightdata_dataset_fetch"
 
78
  else:
79
  raise ValueError(f"Missing required field '{field}' for dataset '{dataset_key}'")
80
 
 
81
  payload.update(fixed_values)
82
  return payload
83
 
 
159
  if not snapshot_id:
160
  raise RuntimeError("No snapshot ID returned from Bright Data.")
161
 
 
162
  snapshot_url = f"https://api.brightdata.com/datasets/v3/snapshot/{snapshot_id}"
163
  max_attempts = 600
164
  attempts = 0
 
172
  timeout=30,
173
  )
174
 
 
175
  if response.status_code == 400:
176
  response.raise_for_status()
177
 
 
187
  time.sleep(1)
188
 
189
  except requests.exceptions.RequestException as exc:
 
190
  if getattr(getattr(exc, "response", None), "status_code", None) == 400:
191
  raise
192
  attempts += 1
193
  time.sleep(1)
194
 
195
  raise TimeoutError(f"Timeout waiting for snapshot {snapshot_id} after {max_attempts} seconds")
196
+
197
+ def _get_gradio_app_code(self, tool_module_name: str = "tool") -> str:
198
+ """
199
+ Override the default app to render a dropdown for dataset selection
200
+ instead of a long text field. Kept minimal: single-select dropdown,
201
+ rest are simple text boxes.
202
+ """
203
+ choices = sorted(self.datasets.keys())
204
+ class_name = self.__class__.__name__
205
+ return f"""\
206
+ import gradio as gr
207
+ from {tool_module_name} import {class_name}
208
+
209
+ tool = {class_name}()
210
+
211
+ def run(dataset, url, keyword, first_name, last_name, days_limit, num_of_reviews, num_of_comments):
212
+ return tool(
213
+ dataset=dataset,
214
+ url=url,
215
+ keyword=keyword,
216
+ first_name=first_name,
217
+ last_name=last_name,
218
+ days_limit=days_limit,
219
+ num_of_reviews=num_of_reviews,
220
+ num_of_comments=num_of_comments,
221
+ )
222
+
223
+ with gr.Blocks() as demo:
224
+ gr.Markdown("### Bright Data dataset fetch")
225
+ dataset = gr.Dropdown(choices={choices}, label="Dataset", multiselect=False)
226
+ url = gr.Textbox(label="URL", placeholder="https://...")
227
+ keyword = gr.Textbox(label="Keyword")
228
+ first_name = gr.Textbox(label="First name")
229
+ last_name = gr.Textbox(label="Last name")
230
+ days_limit = gr.Textbox(label="Days limit (e.g. 3)")
231
+ num_of_reviews = gr.Textbox(label="Number of reviews")
232
+ num_of_comments = gr.Textbox(label="Number of comments")
233
+ run_btn = gr.Button("Run")
234
+ output = gr.Textbox(label="Output", lines=12)
235
+ run_btn.click(
236
+ run,
237
+ inputs=[dataset, url, keyword, first_name, last_name, days_limit, num_of_reviews, num_of_comments],
238
+ outputs=output,
239
+ )
240
+
241
+ demo.launch()
242
+ """