-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.py
71 lines (54 loc) · 1.81 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import numpy as np
from fastapi import FastAPI, Form
import pandas as pd
from starlette.responses import HTMLResponse
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
# from model import preProcess_data
from pydantic import BaseModel
import tensorflow as tf
import re
def preProcess_data(text):
text = text.lower()
new_text = re.sub('[^a-zA-z0-9\s]','',text)
new_text = re.sub('rt', '', new_text)
return new_text
app = FastAPI()
data = pd.read_csv('archive/Sentiment.csv')
tokenizer = Tokenizer(num_words=2000, split=' ')
tokenizer.fit_on_texts(data['text'].values)
def my_pipeline(text):
text_new = preProcess_data(text)
X = tokenizer.texts_to_sequences(pd.Series(text_new).values)
X = pad_sequences(X, maxlen=28)
return X
class inputToModel(BaseModel):
text:str
@app.get('/')
def basic_view():
return {"WELCOME": "GO TO /docs route, or /post or send post request to /predict "}
@app.get('/predict', response_class=HTMLResponse)
def take_inp():
return '''<form method="post">
<input type="text" maxlength="28" name="text" value="Text Emotion to be tested"/>
<input type="submit"/>
</form>'''
@app.post('/predict')
def predict(text:str = Form(...)):
clean_text = my_pipeline(text)
loaded_model = tf.keras.models.load_model('sentiment.h5')
predictions = loaded_model.predict(clean_text)
sentiment = int(np.argmax(predictions))
probability = max(predictions.tolist()[0])
print(sentiment)
if sentiment==0:
t_sentiment = 'negative'
elif sentiment==1:
t_sentiment = 'neutral'
elif sentiment==2:
t_sentiment='postive'
return {
"ACTUALL SENTENCE": text,
"PREDICTED SENTIMENT": t_sentiment,
"Probability": probability
}