Update app.py
Browse files
app.py
CHANGED
@@ -1,52 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from flask import jsonify
|
3 |
from newspaper import Article
|
4 |
|
5 |
-
# Function to scrape the article
|
6 |
def scrape_article(url):
|
7 |
try:
|
8 |
-
# Create an Article object
|
9 |
article = Article(url)
|
10 |
-
|
11 |
-
# Download and parse the article
|
12 |
article.download()
|
13 |
article.parse()
|
14 |
|
15 |
-
# Extract the title and text content
|
16 |
title = article.title
|
|
|
17 |
content = article.text
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
"title": title,
|
25 |
-
"content": content
|
26 |
-
}), 200
|
27 |
-
# return f"Title: {title}\n\nContent:\n{content}"
|
28 |
except Exception as e:
|
29 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
# Gradio interface
|
32 |
def main():
|
33 |
-
|
34 |
-
|
35 |
-
gr.
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
# Button functionality
|
46 |
-
scrape_button.click(scrape_article, inputs=[url_input], outputs=[output])
|
47 |
-
|
48 |
-
return interface
|
49 |
|
50 |
if __name__ == "__main__":
|
51 |
-
|
52 |
-
interface.launch()
|
|
|
1 |
+
# import gradio as gr
|
2 |
+
# from newspaper import Article
|
3 |
+
|
4 |
+
# # Function to scrape the article from a given URL
|
5 |
+
# def scrape_article(url):
|
6 |
+
# try:
|
7 |
+
# # Create an Article object
|
8 |
+
# article = Article(url)
|
9 |
+
|
10 |
+
# # Download and parse the article
|
11 |
+
# article.download()
|
12 |
+
# article.parse()
|
13 |
+
|
14 |
+
# # Extract the title and text content
|
15 |
+
# title = article.title
|
16 |
+
# content = article.text
|
17 |
+
|
18 |
+
# if not content:
|
19 |
+
# return "Failed to scrape content. Please check the URL."
|
20 |
+
|
21 |
+
|
22 |
+
# return f"Title: {title}\n\nContent:\n{content}"
|
23 |
+
# except Exception as e:
|
24 |
+
# return f"Error: {str(e)}"
|
25 |
+
|
26 |
+
# # Gradio interface
|
27 |
+
# def main():
|
28 |
+
# # Define the Gradio input-output interface
|
29 |
+
# with gr.Blocks() as interface:
|
30 |
+
# gr.Markdown("""## Article Scraper using Newspaper3k
|
31 |
+
# Enter a URL to scrape the article content.""")
|
32 |
+
|
33 |
+
# # Input and output widgets
|
34 |
+
# url_input = gr.Textbox(label="Enter the Article URL")
|
35 |
+
# output = gr.Textbox(label="Scraped Article", lines=15)
|
36 |
+
|
37 |
+
# # Button to trigger scraping
|
38 |
+
# scrape_button = gr.Button("Scrape Article")
|
39 |
+
|
40 |
+
# # Button functionality
|
41 |
+
# scrape_button.click(scrape_article, inputs=[url_input], outputs=[output])
|
42 |
+
|
43 |
+
# return interface
|
44 |
+
|
45 |
+
# if __name__ == "__main__":
|
46 |
+
# interface = main()
|
47 |
+
# interface.launch()
|
48 |
+
|
49 |
+
|
50 |
+
|
51 |
import gradio as gr
|
|
|
52 |
from newspaper import Article
|
53 |
|
54 |
+
# Function to scrape the article
|
55 |
def scrape_article(url):
|
56 |
try:
|
|
|
57 |
article = Article(url)
|
|
|
|
|
58 |
article.download()
|
59 |
article.parse()
|
60 |
|
|
|
61 |
title = article.title
|
62 |
+
authors = ", ".join(article.authors)
|
63 |
content = article.text
|
64 |
|
65 |
+
return {
|
66 |
+
"Title": title,
|
67 |
+
"Authors": authors if authors else "Not available",
|
68 |
+
"Content": content if content else "No content available",
|
69 |
+
}
|
|
|
|
|
|
|
|
|
70 |
except Exception as e:
|
71 |
+
return {"Error": f"Failed to scrape the article: {str(e)}"}
|
72 |
+
|
73 |
+
# Define Gradio interface
|
74 |
+
def gradio_scraper(url):
|
75 |
+
result = scrape_article(url)
|
76 |
+
if "Error" in result:
|
77 |
+
return result["Error"], "", ""
|
78 |
+
return result["Title"], result["Authors"], result["Content"]
|
79 |
|
80 |
# Gradio interface
|
81 |
def main():
|
82 |
+
interface = gr.Interface(
|
83 |
+
fn=gradio_scraper,
|
84 |
+
inputs=gr.Textbox(label="Enter Article URL"),
|
85 |
+
outputs=[
|
86 |
+
gr.Textbox(label="Title"),
|
87 |
+
gr.Textbox(label="Authors"),
|
88 |
+
gr.Textarea(label="Content"),
|
89 |
+
],
|
90 |
+
title="Article Scraper",
|
91 |
+
description="Enter the URL of a news article to scrape its title, authors, and content.",
|
92 |
+
)
|
93 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
94 |
|
95 |
if __name__ == "__main__":
|
96 |
+
main()
|
|