Spaces:
Build error
Build error
Incorporated feedbacks
Browse files- InferenceServer.py +9 -7
- app.py +5 -2
- requirements.txt +3 -0
InferenceServer.py
CHANGED
|
@@ -1,11 +1,13 @@
|
|
| 1 |
import os
|
| 2 |
|
| 3 |
-
print("Installing dependencies...")
|
| 4 |
|
| 5 |
-
os.system("git clone https://github.com/PrithivirajDamodaran/neuspell.git")
|
| 6 |
-
os.chdir('neuspell')
|
| 7 |
-
os.system('pip install -e .[elmo]')
|
| 8 |
-
os.system('pip install -e .[spacy]')
|
|
|
|
|
|
|
| 9 |
os.system("python -m spacy download en_core_web_sm")
|
| 10 |
|
| 11 |
import neuspell
|
|
@@ -14,7 +16,7 @@ bl_checker = BertsclstmChecker()
|
|
| 14 |
el_checker = ElmosclstmChecker()
|
| 15 |
cl_checker = CnnlstmChecker()
|
| 16 |
|
| 17 |
-
print("Loading Models...")
|
| 18 |
bl_checker.from_pretrained()
|
| 19 |
el_checker.from_pretrained()
|
| 20 |
cl_checker.from_pretrained()
|
|
@@ -27,7 +29,6 @@ from fastapi import File
|
|
| 27 |
from fastapi import FastAPI
|
| 28 |
import sys
|
| 29 |
|
| 30 |
-
|
| 31 |
app = FastAPI()
|
| 32 |
print("Models loaded !")
|
| 33 |
|
|
@@ -38,6 +39,7 @@ def read_root():
|
|
| 38 |
|
| 39 |
@app.get("/{correct}")
|
| 40 |
def get_correction(input_sentence, model):
|
|
|
|
| 41 |
if model == "BERT-LSTM":
|
| 42 |
return {"corrected_sentence": bl_checker.correct(input_sentence)}
|
| 43 |
elif model == "ELMo-LSTM":
|
|
|
|
| 1 |
import os
|
| 2 |
|
| 3 |
+
# print("Installing dependencies...")
|
| 4 |
|
| 5 |
+
# os.system("git clone https://github.com/PrithivirajDamodaran/neuspell.git")
|
| 6 |
+
# os.chdir('neuspell')
|
| 7 |
+
# os.system('pip install -e .[elmo]')
|
| 8 |
+
# os.system('pip install -e .[spacy]')
|
| 9 |
+
|
| 10 |
+
print("Loading Spacy Model...")
|
| 11 |
os.system("python -m spacy download en_core_web_sm")
|
| 12 |
|
| 13 |
import neuspell
|
|
|
|
| 16 |
el_checker = ElmosclstmChecker()
|
| 17 |
cl_checker = CnnlstmChecker()
|
| 18 |
|
| 19 |
+
print("Loading Neuspell Models...")
|
| 20 |
bl_checker.from_pretrained()
|
| 21 |
el_checker.from_pretrained()
|
| 22 |
cl_checker.from_pretrained()
|
|
|
|
| 29 |
from fastapi import FastAPI
|
| 30 |
import sys
|
| 31 |
|
|
|
|
| 32 |
app = FastAPI()
|
| 33 |
print("Models loaded !")
|
| 34 |
|
|
|
|
| 39 |
|
| 40 |
@app.get("/{correct}")
|
| 41 |
def get_correction(input_sentence, model):
|
| 42 |
+
'''Returns spell corrected sentence using the model passed in model param.'''
|
| 43 |
if model == "BERT-LSTM":
|
| 44 |
return {"corrected_sentence": bl_checker.correct(input_sentence)}
|
| 45 |
elif model == "ELMo-LSTM":
|
app.py
CHANGED
|
@@ -10,9 +10,11 @@ import time
|
|
| 10 |
import os
|
| 11 |
|
| 12 |
def start_server():
|
|
|
|
| 13 |
os.system("uvicorn InferenceServer:app --port 8080 --host 0.0.0.0 --workers 1")
|
| 14 |
|
| 15 |
def load_models():
|
|
|
|
| 16 |
if not is_port_in_use(8080):
|
| 17 |
with st.spinner(text="Loading models, please wait..."):
|
| 18 |
proc = Process(target=start_server, args=(), daemon=True)
|
|
@@ -25,6 +27,7 @@ def load_models():
|
|
| 25 |
st.session_state['models_loaded'] = True
|
| 26 |
|
| 27 |
def is_port_in_use(port):
|
|
|
|
| 28 |
import socket
|
| 29 |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
| 30 |
return s.connect_ex(('0.0.0.0', port)) == 0
|
|
@@ -33,8 +36,8 @@ if 'models_loaded' not in st.session_state:
|
|
| 33 |
st.session_state['models_loaded'] = False
|
| 34 |
|
| 35 |
|
| 36 |
-
|
| 37 |
def get_correction(input_text, model):
|
|
|
|
| 38 |
correct_request = "http://0.0.0.0:8080/correct?input_sentence="+input_text + "&model=" + model
|
| 39 |
correct_response = requests.get(correct_request)
|
| 40 |
correct_json = json.loads(correct_response.text)
|
|
@@ -46,7 +49,7 @@ def get_correction(input_text, model):
|
|
| 46 |
|
| 47 |
|
| 48 |
def diff_strings(a, b):
|
| 49 |
-
|
| 50 |
result = []
|
| 51 |
diff = difflib.Differ().compare(a.split(), b.split())
|
| 52 |
replacement = ""
|
|
|
|
| 10 |
import os
|
| 11 |
|
| 12 |
def start_server():
|
| 13 |
+
'''Helper to start to service through Unicorn '''
|
| 14 |
os.system("uvicorn InferenceServer:app --port 8080 --host 0.0.0.0 --workers 1")
|
| 15 |
|
| 16 |
def load_models():
|
| 17 |
+
'''One time loading/ Init of models and starting server as a seperate process'''
|
| 18 |
if not is_port_in_use(8080):
|
| 19 |
with st.spinner(text="Loading models, please wait..."):
|
| 20 |
proc = Process(target=start_server, args=(), daemon=True)
|
|
|
|
| 27 |
st.session_state['models_loaded'] = True
|
| 28 |
|
| 29 |
def is_port_in_use(port):
|
| 30 |
+
'''Helper to check if service already running'''
|
| 31 |
import socket
|
| 32 |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
| 33 |
return s.connect_ex(('0.0.0.0', port)) == 0
|
|
|
|
| 36 |
st.session_state['models_loaded'] = False
|
| 37 |
|
| 38 |
|
|
|
|
| 39 |
def get_correction(input_text, model):
|
| 40 |
+
'''Invokes the Neuspell inference service'''
|
| 41 |
correct_request = "http://0.0.0.0:8080/correct?input_sentence="+input_text + "&model=" + model
|
| 42 |
correct_response = requests.get(correct_request)
|
| 43 |
correct_json = json.loads(correct_response.text)
|
|
|
|
| 49 |
|
| 50 |
|
| 51 |
def diff_strings(a, b):
|
| 52 |
+
'''Highlights corrections with annotated_text library'''
|
| 53 |
result = []
|
| 54 |
diff = difflib.Differ().compare(a.split(), b.split())
|
| 55 |
replacement = ""
|
requirements.txt
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
st-annotated-text
|
| 2 |
fastapi
|
| 3 |
uvicorn
|
|
|
|
| 1 |
+
git+https://github.com/PrithivirajDamodaran/neuspell.git
|
| 2 |
+
spacy
|
| 3 |
+
allennlp==1.5.0
|
| 4 |
st-annotated-text
|
| 5 |
fastapi
|
| 6 |
uvicorn
|