aliabd HF staff commited on
Commit
76b8969
·
1 Parent(s): dca6084

Create new file

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # URL: https://huggingface.co/spaces/gradio/text_analysis
2
+ # imports
3
+ import gradio as gr
4
+ import spacy
5
+ from spacy import displacy
6
+
7
+ # load the model
8
+ nlp = spacy.load("en_core_web_sm")
9
+
10
+ # define the core function
11
+ def text_analysis(text):
12
+ doc = nlp(text)
13
+ html = displacy.render(doc, style="dep", page=True)
14
+ html = (
15
+ "<div style='max-width:100%; max-height:360px; overflow:auto'>"
16
+ + html
17
+ + "</div>"
18
+ )
19
+ pos_count = {
20
+ "char_count": len(text),
21
+ "token_count": 0,
22
+ }
23
+ pos_tokens = []
24
+
25
+ for token in doc:
26
+ pos_tokens.extend([(token.text, token.pos_), (" ", None)])
27
+
28
+ return pos_tokens, pos_count, html
29
+
30
+ # define the interface, with a textbox input, and three outputs: HighlightedText, JSON, and HTML
31
+ demo = gr.Interface(
32
+ text_analysis,
33
+ gr.Textbox(placeholder="Enter sentence here..."),
34
+ ["highlight", "json", "html"],
35
+ examples=[
36
+ ["What a beautiful morning for a walk!"],
37
+ ["It was the best of times, it was the worst of times."],
38
+ ],
39
+ )
40
+
41
+ # launch
42
+ demo.launch()