Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from multiprocessing import Process
|
3 |
+
import json
|
4 |
+
import requests
|
5 |
+
import time
|
6 |
+
import os
|
7 |
+
|
8 |
+
def start_server():
|
9 |
+
os.system("uvicorn InferenceServer:app --port 8080 --host 0.0.0.0 --workers 2")
|
10 |
+
|
11 |
+
def load_models():
|
12 |
+
if not is_port_in_use(8080):
|
13 |
+
with st.spinner(text="The model is loading."):
|
14 |
+
proc = Process(target=start_server, args=(), daemon=True)
|
15 |
+
proc.start()
|
16 |
+
while not is_port_in_use(8080):
|
17 |
+
time.sleep(1)
|
18 |
+
st.success("Model server started.")
|
19 |
+
else:
|
20 |
+
st.success("Model server already running...")
|
21 |
+
st.session_state['models_loaded'] = True
|
22 |
+
|
23 |
+
def is_port_in_use(port):
|
24 |
+
import socket
|
25 |
+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
26 |
+
return s.connect_ex(('0.0.0.0', port)) == 0
|
27 |
+
|
28 |
+
if 'models_loaded' not in st.session_state:
|
29 |
+
st.session_state['models_loaded'] = False
|
30 |
+
|
31 |
+
def get_correction(input_text):
|
32 |
+
st.markdown(f'##### Corrected text:')
|
33 |
+
st.write('')
|
34 |
+
correct_request = "http://0.0.0.0:8080/restore?input_sentence="+input_text
|
35 |
+
with st.spinner('Wait for it...'):
|
36 |
+
correct_response = requests.get(correct_request)
|
37 |
+
correct_json = json.loads(correct_response.text)
|
38 |
+
corrected_sentence = correct_json["corrected_sentence"]
|
39 |
+
result = diff_strings(corrected_sentence,input_text)
|
40 |
+
st.markdown(result, unsafe_allow_html=True)
|
41 |
+
|
42 |
+
def diff_strings(output_text, input_text):
|
43 |
+
c_text = ""
|
44 |
+
for x in output_text.split(" "):
|
45 |
+
if x in input_text.split(" "):
|
46 |
+
c_text = c_text + x + " "
|
47 |
+
else:
|
48 |
+
c_text = c_text + '<span style="font-weight:bold; color:rgb(142, 208, 129);">' + x + '</span>' + " "
|
49 |
+
return c_text
|
50 |
+
|
51 |
+
if __name__ == "__main__":
|
52 |
+
|
53 |
+
st.title('Punctuation and Capitalization Corrector -- BERT')
|
54 |
+
st.subheader('For Punctuation and Upper Case restoration')
|
55 |
+
st.markdown("Model restores the following punctuations -- [! ? . , - : ; ' ] and also the upper-casing of words.")
|
56 |
+
examples = [
|
57 |
+
"my name is clara and i live in berkeley california",
|
58 |
+
"in 2018 cornell researchers built a high-powered detector",
|
59 |
+
"lorem ipsum has been the industrys standard dummy text ever since the 1500s when an unknown printer took a galley of type and scrambled it to make a type specimen book"
|
60 |
+
]
|
61 |
+
if not st.session_state['models_loaded']:
|
62 |
+
load_models()
|
63 |
+
|
64 |
+
input_text = st.selectbox(
|
65 |
+
label="Examples",
|
66 |
+
options=examples
|
67 |
+
)
|
68 |
+
input_text = st.text_input(
|
69 |
+
label="Write or paste text",
|
70 |
+
value=input_text
|
71 |
+
)
|
72 |
+
if input_text.strip():
|
73 |
+
get_correction(input_text)
|