BrightData's picture
Update app.py
c01de1b verified
from __future__ import annotations
import gradio as gr
from tool import BrightDataDatasetTool, DATASET_CHOICES, DATASET_FIELDS
tool = BrightDataDatasetTool()
def toggle_fields(selected: str):
inputs = ["url", "keyword", "first_name", "last_name", "days_limit", "num_of_reviews", "num_of_comments"]
wanted = set(DATASET_FIELDS.get(selected, []))
def visibility(name: str):
return gr.update(visible=name in wanted)
return tuple(visibility(name) for name in inputs)
def run(dataset: str, url: str, keyword: str, first_name: str, last_name: str, days_limit: str, num_of_reviews: str, num_of_comments: str) -> str:
import sys
print(f"[DEBUG app.run] Received url: {url!r} (type: {type(url).__name__})", file=sys.stderr)
return tool(
dataset=dataset,
url=url,
keyword=keyword,
first_name=first_name,
last_name=last_name,
days_limit=days_limit,
num_of_reviews=num_of_reviews,
num_of_comments=num_of_comments,
)
def create_demo() -> gr.Blocks:
with gr.Blocks() as demo:
gr.Markdown("# Bright Data dataset fetch")
gr.Markdown("### [Signup here](https://brightdata.com/?utm_source=tech-partner&utm_medium=link&utm_campaign=huggingface&hs_signup=1)")
dataset = gr.Dropdown(choices=DATASET_CHOICES, label="Dataset", value=DATASET_CHOICES[0])
url = gr.Textbox(label="URL", placeholder="https://...", visible=True)
keyword = gr.Textbox(label="Keyword", visible=False)
first_name = gr.Textbox(label="First name", visible=False)
last_name = gr.Textbox(label="Last name", visible=False)
days_limit = gr.Textbox(label="Days limit (e.g. 3)", visible=False)
num_of_reviews = gr.Textbox(label="Number of reviews", visible=False)
num_of_comments = gr.Textbox(label="Number of comments", visible=False)
dataset.change(
toggle_fields,
inputs=[dataset],
outputs=[url, keyword, first_name, last_name, days_limit, num_of_reviews, num_of_comments],
)
run_btn = gr.Button("Run")
output = gr.Textbox(label="Output", lines=12)
run_btn.click(
run,
inputs=[dataset, url, keyword, first_name, last_name, days_limit, num_of_reviews, num_of_comments],
outputs=output,
)
return demo
if __name__ == "__main__":
create_demo().launch()