j-m commited on
Commit
dd60aba
1 Parent(s): 26e1f30

first draft app.py formality tagging

Browse files
Files changed (1) hide show
  1. app.py +66 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ---
2
+ # jupyter:
3
+ # jupytext:
4
+ # formats: ipynb,py:percent
5
+ # text_representation:
6
+ # extension: .py
7
+ # format_name: percent
8
+ # format_version: '1.3'
9
+ # jupytext_version: 1.13.8
10
+ # kernelspec:
11
+ # display_name: Python 3 (ipykernel)
12
+ # language: python
13
+ # name: python3
14
+ # ---
15
+
16
+ # %%
17
+ import gradio as gr
18
+ from fastai.text.all import *
19
+ from nltk import tokenize
20
+
21
+ # %%
22
+ import nltk
23
+ nltk.download('punkt')
24
+
25
+ # %%
26
+ model_pkl = "https://cloudstor.aarnet.edu.au/plus/s/rJzjZMk7ieZGsao/download"
27
+ p = Path("./formality.pkl")
28
+
29
+ # %%
30
+ if not p.exists():
31
+ import urllib.request
32
+ urllib.request.urlretrieve(model_pkl, "./formality.pkl")
33
+
34
+ # %%
35
+ m = load_pickle("./formality.pkl")
36
+
37
+ # %%
38
+ formal_sentences = "The Gram matrix (or Gramian matrix, Gramian) of a set of vectors in an inner product space is the Hermitian matrix of inner products. C'man, man, don't leave holding the bag!"
39
+
40
+ # %%
41
+ L(tokenize.sent_tokenize(formal_sentences)).map(lambda x: m.predict(x)[0])
42
+
43
+
44
+ # %% tags=[]
45
+ def formal_informal(text):
46
+ # a = m.predict(text)
47
+ # assessment = "style: {0}, confidence {1:.2f}%".format( a[0], a[2][0] * 100)
48
+ sentences = L(tokenize.sent_tokenize(text))
49
+ predictions = sentences.map(lambda x: m.predict(x)[0])
50
+ # assessment = "style: {0}".format(a[0])
51
+ assessment = zip(sentences.map(lambda x: x + ' '), predictions)
52
+ return assessment
53
+
54
+ demo = gr.Interface(formal_informal,
55
+ gr.Textbox(label="Your text", lines=5, value=""),
56
+ gr.HighlightedText(label="Stylistic analysis", color_map={'formal': 'blue', 'informal': 'yellow'},
57
+ combine_adjacent=True, show_legend=False),
58
+ examples=["I returned and saw under the sun, that the race is not to the swift, nor the battle to the strong, neither yet bread to the wise, nor yet riches to men of understanding, nor yet favour to men of skill; but time and chance happeneth to them all.",
59
+ "Objective consideration of contemporary phenomena compels the conclusion that success or failure in competitive activities exhibits no tendency to be commensurate with innate capacity, but that a considerable element of the unpredictable must invariably be taken into account."])
60
+ demo.launch()
61
+
62
+ # %%
63
+
64
+ # %%
65
+
66
+ # %%