datasciencedojo commited on
Commit
b60dadd
·
1 Parent(s): 0611d41

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import wikipedia
3
+ import numpy as np
4
+ import pandas as pd
5
+ from os import path
6
+ from PIL import Image
7
+ from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
8
+ import matplotlib.pyplot as plt
9
+
10
+ def wikipediaScrap(article_name, wikipedia_language = "en - English"):
11
+ wikipedia_language = wikipedia_language.split(" - ")[0]
12
+
13
+ if wikipedia_language:
14
+ wikipedia.set_lang(wikipedia_language)
15
+
16
+ # rem_sp = article_name.replace(" ", "")
17
+ et_page = wikipedia.page(article_name)
18
+ title = et_page.title
19
+ content = et_page.content
20
+ page_url = et_page.url
21
+ linked_pages = et_page.links
22
+
23
+ text = content
24
+
25
+ # Create and generate a word cloud image:
26
+ wordcloud = WordCloud(font_path="HelveticaWorld-Regular.ttf").generate(text)
27
+
28
+ # Display the generated image:
29
+ plt.imshow(wordcloud, interpolation='bilinear')
30
+ plt.axis("off")
31
+
32
+ return title, content, page_url, "\n". join(linked_pages), plt
33
+
34
+ css = """
35
+ footer {display:none !important}
36
+ .output-markdown{display:none !important}
37
+ footer {visibility: hidden}
38
+ #dsd_button {background: purple, color: white}
39
+ textarea[data-testid="textbox"] { height: 178px !important}
40
+ #mytext {height: 43px !important;}
41
+ .max-h-[30rem] {max-height: 18rem !important;}
42
+ .hover\:bg-orange-50:hover {
43
+ --tw-bg-opacity: 1 !important;
44
+ background-color: rgb(229,225,255) !important;
45
+ }
46
+ """
47
+
48
+ ini_dict = wikipedia.languages()
49
+
50
+ # split dictionary into keys and values
51
+ keys = []
52
+ values = []
53
+ language=[]
54
+
55
+ items = ini_dict.items()
56
+ for item in items:
57
+ keys.append(item[0]), values.append(item[1])
58
+ language.append(item[0]+" - "+item[1])
59
+
60
+ with gr.Blocks(title="Wikipedia Article Scrape | Data Science Dojo", css = css) as demo:
61
+ with gr.Row():
62
+ inp = gr.Textbox(placeholder="Enter the name of wikipedia article", label="Wikipedia article name")
63
+ lan = gr.Dropdown(label=" Select Language", choices=language, value=language[105], interactive=True)
64
+
65
+ btn = gr.Button("Start Scraping", elem_id="dsd_button")
66
+ with gr.Row():
67
+ with gr.Column():
68
+ gr.Markdown("""## About""")
69
+ title = gr.Textbox(label="Article title")
70
+ url = gr.Textbox(label="Article URL")
71
+ with gr.Column():
72
+ gr.Markdown("""## Wordcloud""")
73
+ wordcloud = gr.Plot()
74
+ gr.Markdown("""### Content""")
75
+ with gr.Row():
76
+ content = gr.Textbox(label="Content")
77
+ gr.Markdown("""### Linked Articles""")
78
+ with gr.Row():
79
+ linked = gr.Textbox(label="Linked Articles")
80
+ with gr.Row():
81
+ gr.Examples(
82
+ examples = [["Eiffel Tower", "en - English"], ["Eiffel tower", 'ur - اردو']], fn=wikipediaScrap, inputs=[inp, lan], outputs=[title, content, url, linked, wordcloud], cache_examples=True)
83
+ btn.click(fn=wikipediaScrap, inputs=[inp, lan], outputs=[title, content, url, linked, wordcloud])
84
+
85
+ demo.launch()