simran40 commited on
Commit
58db982
·
verified ·
1 Parent(s): d33b81f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -22
app.py CHANGED
@@ -1,23 +1,93 @@
1
  import gradio as gr
2
- from scraper import scrape_blog # your existing scraper function
3
-
4
- # Define a function to scrape and return results
5
- def scrape(url, tag='h2'):
6
- data = scrape_blog(url, tag)
7
- # Join the results into a string for display in Gradio
8
- return "\n".join(data)
9
-
10
- # Create Gradio interface
11
- iface = gr.Interface(
12
- fn=scrape,
13
- inputs=[
14
- gr.Textbox(label="Enter URL"),
15
- gr.Textbox(label="Enter HTML Tag (default: h2)", value="h2")
16
- ],
17
- outputs=gr.Textbox(label="Scraped Titles"),
18
- title="Blog Scraper",
19
- description="Enter a URL and an HTML tag to scrape titles or headings from a blog."
20
- )
21
-
22
- # Launch the interface
23
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from scraper import scrape_blog
3
+
4
+ # Scrape function for multiple tags
5
+ def scrape(url, tags):
6
+ if not url:
7
+ return "⚠️ Please enter a valid URL"
8
+ tags_list = [t.strip() for t in tags.split(",") if t.strip()]
9
+ all_data = []
10
+ for tag in tags_list:
11
+ data = scrape_blog(url, tag)
12
+ if data:
13
+ all_data.append(f"Results for <{tag}>:\n" + "\n".join(data))
14
+ return "\n\n".join(all_data) if all_data else "No data found!"
15
+
16
+ # Gradio Blocks with enhanced UI
17
+ with gr.Blocks(css="""
18
+ body {
19
+ background: linear-gradient(135deg, #89f7fe, #66a6ff);
20
+ font-family: 'Arial', sans-serif;
21
+ }
22
+ .glass-card {
23
+ background: rgba(255, 255, 255, 0.15);
24
+ backdrop-filter: blur(20px);
25
+ border-radius: 25px;
26
+ padding: 25px;
27
+ box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.37);
28
+ border: 1px solid rgba(255, 255, 255, 0.18);
29
+ }
30
+ h1 {
31
+ color: #fff;
32
+ text-align: center;
33
+ text-shadow: 1px 1px 8px rgba(0,0,0,0.3);
34
+ }
35
+ .gr-button {
36
+ background: rgba(255, 255, 255, 0.3) !important;
37
+ color: #000 !important;
38
+ font-weight: bold;
39
+ border-radius: 12px !important;
40
+ margin: 5px;
41
+ transition: 0.3s;
42
+ }
43
+ .gr-button:hover {
44
+ background: rgba(255, 255, 255, 0.6) !important;
45
+ color: #222 !important;
46
+ transform: scale(1.05);
47
+ }
48
+ .gr-textbox {
49
+ border-radius: 15px !important;
50
+ padding: 10px !important;
51
+ }
52
+ .output-box {
53
+ max-height: 400px;
54
+ overflow-y: auto;
55
+ white-space: pre-wrap;
56
+ }
57
+ """) as demo:
58
+
59
+ gr.HTML("<h1>✨ Professional Blog Scraper ✨</h1>")
60
+
61
+ with gr.Row():
62
+ with gr.Column():
63
+ with gr.Box(css_class="glass-card"):
64
+ url_input = gr.Textbox(label="Enter Blog URL", placeholder="https://example.com")
65
+ tag_input = gr.Textbox(label="Enter Tags (comma separated, e.g., h1,h2,h3)", value="h2")
66
+
67
+ # Tag buttons for quick selection
68
+ with gr.Row():
69
+ h1_btn = gr.Button("h1")
70
+ h2_btn = gr.Button("h2")
71
+ h3_btn = gr.Button("h3")
72
+ all_btn = gr.Button("h1,h2,h3")
73
+
74
+ scrape_btn = gr.Button("Scrape Blog")
75
+
76
+ with gr.Column():
77
+ with gr.Box(css_class="glass-card output-box"):
78
+ output = gr.Textbox(label="Scraped Titles", interactive=False)
79
+ copy_btn = gr.Button("Copy to Clipboard")
80
+
81
+ # Tag button clicks
82
+ h1_btn.click(lambda: "h1", inputs=None, outputs=tag_input)
83
+ h2_btn.click(lambda: "h2", inputs=None, outputs=tag_input)
84
+ h3_btn.click(lambda: "h3", inputs=None, outputs=tag_input)
85
+ all_btn.click(lambda: "h1,h2,h3", inputs=None, outputs=tag_input)
86
+
87
+ # Scrape button click
88
+ scrape_btn.click(scrape, inputs=[url_input, tag_input], outputs=output)
89
+
90
+ # Copy button (Gradio native copy support)
91
+ copy_btn.click(lambda x: x, inputs=output, outputs=None)
92
+
93
+ demo.launch()