anton-l HF staff commited on
Commit
b2b504b
1 Parent(s): db7c020
Files changed (2) hide show
  1. app.py +55 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+
3
+ import gradio as gr
4
+ import requests
5
+ from inscriptis import get_text
6
+ from inscriptis.css_profiles import CSS_PROFILES
7
+ from inscriptis.model.config import ParserConfig
8
+ from readability import Document
9
+
10
+ INSCRIPTIS_CONFIG = ParserConfig(css=CSS_PROFILES["strict"])
11
+
12
+
13
+ def extract_text(url: str):
14
+ html = requests.get(url).content.decode("utf-8")
15
+
16
+ if len(html.strip()) == 0:
17
+ return "", "", "", ""
18
+
19
+ parsed_doc = Document(html)
20
+
21
+ # get the body of the article with readability-lxml
22
+ title = parsed_doc.short_title()
23
+ clean_html = parsed_doc.summary(html_partial=True)
24
+ del parsed_doc
25
+
26
+ # get the formatted plaintext with inscriptis
27
+ text = get_text(clean_html, INSCRIPTIS_CONFIG).strip()
28
+
29
+ if not re.search(r"\w+", text):
30
+ # no words found, only whitespace and punctuation
31
+ return title, "", clean_html, html
32
+
33
+ # remove excessive empty lines
34
+ text = re.sub(r"\n\s*\n", "\n\n", text)
35
+
36
+ return title, text, clean_html, html
37
+
38
+
39
+ title = gr.Textbox(label="Title")
40
+ text = gr.Textbox(label="Text", lines=10)
41
+ clean_html = gr.Textbox(label="Clean HTML", lines=10)
42
+ html = gr.Textbox(label="Raw HTML", lines=10)
43
+ demo = gr.Interface(
44
+ extract_text,
45
+ gr.Textbox(placeholder="https://hf.co/", label="URL"),
46
+ [title, text, clean_html, html],
47
+ examples=[
48
+ ["https://huggingface.co/blog/peft"],
49
+ [
50
+ "https://www.nytimes.com/2023/03/08/technology/chatbots-disrupt-internet-industry.html"
51
+ ],
52
+ ],
53
+ )
54
+
55
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ git+https://github.com/huggingface/python-readability@speedup
2
+ inscriptis==2.3.2