-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
60 lines (48 loc) · 1.84 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
import os
import pickle
import numpy as np
import warnings
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import load_model
import string
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponse
from pydantic import BaseModel
warnings.filterwarnings('ignore')
app = FastAPI()
templates = Jinja2Templates(directory='templates')
class RecognizeEmotion(BaseModel):
text: str
@app.get('/',response_class=HTMLResponse)
async def home(request:Request):
return templates.TemplateResponse('index.html',{'request':request})
@app.post('/predict/')
async def predict(txt:RecognizeEmotion):
with open('tokenizer.pkl','rb') as f:
tokenizer = pickle.load(f)
model = load_model('emotion_model.keras')
def preprocess_texts(txts):
preprocessed_texts = []
stop_words = set(stopwords.words('english'))
for txt in txts:
words = word_tokenize(txt)
filtered_txt = [word for word in words if word.lower() not in stop_words and word not in string.punctuation]
txt = ' '.join(filtered_txt)
preprocessed_texts.append(txt)
preprocessed_texts = np.array(preprocessed_texts)
return preprocessed_texts
def predict_emotion(txt):
txt = preprocess_texts([txt])
txt1 = tokenizer.texts_to_sequences(txt)
txt1 = pad_sequences(txt1,maxlen=35)
pred = model.predict(txt1)
if np.max(pred) < 0.5:
return "Undetermined"
pred = np.argmax(pred,axis=1)
emotions = ['Anger','Fear','Joy','Love','Sadness','Surprise']
return emotions[pred[0]]
return {'prediction':predict_emotion(txt.text)}