Your Name commited on
Commit
075c27b
1 Parent(s): 1ccf774

Initial Commit

Browse files
Files changed (3) hide show
  1. InferenceServer.py +48 -0
  2. app.py +140 -0
  3. requirements.txt +4 -0
InferenceServer.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
12
+ from neuspell import BertsclstmChecker, ElmosclstmChecker, CnnlstmChecker
13
+ 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()
21
+ print("Dummy run", bl_checker.correct("I luk foward to receving your reply"))
22
+ print("Dummy run", el_checker.correct("I luk foward to receving your reply"))
23
+ print("Dummy run", cl_checker.correct("I luk foward to receving your reply"))
24
+
25
+ import uvicorn
26
+ from fastapi import File
27
+ from fastapi import FastAPI
28
+ import sys
29
+
30
+
31
+ app = FastAPI()
32
+ print("Models loaded !")
33
+
34
+
35
+ @app.get("/")
36
+ def read_root():
37
+ return {"Neuspell !"}
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":
44
+ return {"corrected_sentence": el_checker.correct(input_sentence)}
45
+ else:
46
+ return {"corrected_sentence": cl_checker.correct(input_sentence)}
47
+
48
+
app.py ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from annotated_text import annotated_text
3
+ from multiprocessing import Process
4
+ import math
5
+ import re
6
+ import json
7
+ import difflib
8
+ import requests
9
+ 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)
19
+ proc.start()
20
+ while not is_port_in_use(8080):
21
+ time.sleep(1)
22
+ st.success("Model server started.")
23
+ else:
24
+ st.success("Model server already running...")
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
31
+
32
+ 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)
41
+ corrected_sentence = correct_json["corrected_sentence"]
42
+ diff = diff_strings(input_text, corrected_sentence)
43
+ st.markdown(f'##### Corrected text:')
44
+ st.write('')
45
+ annotated_text(*diff)
46
+
47
+
48
+ def diff_strings(a, b):
49
+ # take from team-writing-assistant/grammar-correction
50
+ result = []
51
+ diff = difflib.Differ().compare(a.split(), b.split())
52
+ replacement = ""
53
+ for line in diff:
54
+ if line.startswith(" "):
55
+ if len(replacement) == 0:
56
+ result.append(" ")
57
+ result.append(line[2:])
58
+ else:
59
+ result.append(" ")
60
+ result.append(("", replacement, "#39ff14"))
61
+ replacement = ""
62
+ result.append(line[2:])
63
+ if line.startswith("- "):
64
+ if len(replacement) == 0:
65
+ replacement = line[2:]
66
+ else:
67
+ result.append(" ")
68
+ result.append(("", replacement, "#39ff14"))
69
+ replacement = ""
70
+ elif line.startswith("+ "):
71
+ if len(replacement) == 0:
72
+ result.append((line[2:], "", "#39ff14"))
73
+ else:
74
+ result.append(" ")
75
+ result.append((line[2:], replacement, "#39ff14"))
76
+ replacement = ""
77
+ return result
78
+
79
+ if __name__ == "__main__":
80
+
81
+ st.title('Neuspell - A Python library')
82
+ st.subheader('For fast & accurate spell correction')
83
+ st.markdown("Added to HuggingFace hub and spaces with 💙 by Prithivi Da", unsafe_allow_html=True)
84
+ st.markdown("<p style='color:blue; display:inline'> Integrate with your app with just few lines of code </p>", unsafe_allow_html=True)
85
+ st.markdown("""
86
+ ```python
87
+ import neuspell
88
+ from neuspell import BertsclstmChecker
89
+ checker = BertsclstmChecker()
90
+ checker.from_pretrained()
91
+
92
+ checker.correct("Oh! I loovee it when it starts to rain, its smells awesomee")
93
+ # (or)
94
+ checker.correct_strings(["sent1", "sent2"])
95
+ # (or)
96
+ checker.correct_from_file(src="typo_ridden_texts.txt")
97
+ ```
98
+ """)
99
+
100
+ models = [
101
+ "BERT-LSTM",
102
+ "ELMo-LSTM",
103
+ "CNN-LSTM"
104
+ ]
105
+
106
+
107
+ examples = [
108
+ "what is the reazon for everyone to laeve the comapny?",
109
+ "I loovee it when it starts to rain, its smells awesomee",
110
+ "Feel free to rech out to me",
111
+ "Life is shart, so live freely",
112
+ "We know the boy actually stol the books",
113
+ " We all ate the foood and then made desert",
114
+ ]
115
+
116
+ if not st.session_state['models_loaded']:
117
+ load_models()
118
+
119
+ st.markdown(f'##### Models:')
120
+ selected_model = st.selectbox(
121
+ label="Choose a model: (Test and integrate the best that suits your needs)",
122
+ options=models
123
+ )
124
+
125
+ st.markdown(f'##### Try it now:')
126
+ input_text = st.selectbox(
127
+ label="Choose an example",
128
+ options=examples
129
+ )
130
+ st.write("(or)")
131
+ input_text = st.text_input(
132
+ label="Bring your own sentence",
133
+ value=input_text
134
+ )
135
+
136
+ if input_text.strip():
137
+ get_correction(input_text, selected_model)
138
+
139
+
140
+
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ st-annotated-text
2
+ fastapi
3
+ uvicorn
4
+ huggingface_hub