Spaces:
Build error
Build error
danseith
commited on
Commit
•
8c0b646
1
Parent(s):
b43aa3d
initial commit
Browse files- app.py +57 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
from transformers import pipeline
|
4 |
+
|
5 |
+
unmasker = pipeline("fill-mask", model="anferico/bert-for-patents")
|
6 |
+
|
7 |
+
example = 'A crustless sandwich made from two slices of baked bread'
|
8 |
+
|
9 |
+
def unmask(text):
|
10 |
+
res = unmasker(text)
|
11 |
+
out = {item["token_str"]: item["score"] for item in res}
|
12 |
+
return out
|
13 |
+
|
14 |
+
|
15 |
+
textbox = gr.Textbox(label="Type language here", lines=5)
|
16 |
+
import gradio as gr
|
17 |
+
from transformers import pipeline
|
18 |
+
|
19 |
+
unmasker = pipeline("fill-mask", model="anferico/bert-for-patents")
|
20 |
+
|
21 |
+
|
22 |
+
def add_mask(text, size=3):
|
23 |
+
split_text = text.split()
|
24 |
+
idx = np.random.randint(len(split_text), size=size)
|
25 |
+
for i in idx:
|
26 |
+
split_text[i] = '[MASK]'
|
27 |
+
return ' '.join(split_text)
|
28 |
+
|
29 |
+
|
30 |
+
def unmask(text):
|
31 |
+
text = add_mask(text)
|
32 |
+
res = unmasker(text)
|
33 |
+
out = {item["token_str"]: item["score"] for item in res}
|
34 |
+
return out
|
35 |
+
|
36 |
+
|
37 |
+
textbox = gr.Textbox(label="Type language here", lines=5)
|
38 |
+
|
39 |
+
demo = gr.Interface(
|
40 |
+
fn=unmask,
|
41 |
+
inputs=textbox,
|
42 |
+
outputs="label",
|
43 |
+
examples=[
|
44 |
+
|
45 |
+
],
|
46 |
+
)
|
47 |
+
|
48 |
+
demo.launch()
|
49 |
+
demo = gr.Interface(
|
50 |
+
fn=unmask,
|
51 |
+
inputs=textbox,
|
52 |
+
outputs="label",
|
53 |
+
examples=[
|
54 |
+
],
|
55 |
+
)
|
56 |
+
|
57 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
torch
|
3 |
+
transformers
|
4 |
+
numpy
|