Spaces:
Sleeping
Sleeping
File size: 2,038 Bytes
d5b5a8d 179e256 d5b5a8d 179e256 d5b5a8d 179e256 |
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 72 73 74 |
import streamlit as st
import pandas as pd
import numpy as ny
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from sklearn.preprocessing import LabelEncoder
from keras.layers import *
from keras import Model
map_id = {
0: "sadness",
1: "anger",
2: "love",
3: "surprise",
4: "fear",
5: "joy"
}
map_emotion = {
"sadness": 0,
"anger": 1,
"love": 2,
"surprise": 3,
"fear": 4,
"joy": 5
}
train = pd.read_csv('/train.csv')
for index, row in train.iterrows():
row['emotion'] = map_emotion[row['emotion']]
tokenizer = Tokenizer()
tokenizer.fit_on_texts(train.text)
Length = len(tokenizer.word_index) + 1
x_train = pad_sequences(tokenizer.texts_to_sequences(train.text), maxlen=30)
encoder = LabelEncoder()
encoder.fit(train["emotion"].to_list())
y_train = encoder.transform(train["emotion"].to_list())
y_train = y_train.reshape(-1, 1)
embedding_layer = Embedding(Length,
64,
input_length=30)
input_seq = Input(shape=(x_train.shape[1],))
x = embedding_layer(input_seq)
x = LSTM(10, return_sequences=True) (x)
x = Flatten() (x)
output = Dense(encoder.classes_.shape[0], activation="softmax") (x)
model = Model(input_seq, output)
model.compile(optimizer='adam',
loss="sparse_categorical_crossentropy",
metrics=["accuracy"])
model.fit(x_train, y_train, epochs=20, batch_size=32,
validation_data=(x_val, y_val))
class Predict:
def __init__(self, model, tokenizer):
self.model = model
self.tokenizer = tokenizer
def predict(self, txt):
x = pad_sequences(self.tokenizer.texts_to_sequences([txt]), maxlen=30)
x = self.model(x)
x = ny.argmax(x)
return map_id[x]
predict = Predict(model, tokenizer)
st.title("TONE DETECTION | BCS WINTER PROJECT")
st.write("Enter a sentence to analyze text's Tone:")
user_input = st.text_input("")
if user_input:
result = predict.predict(user_input)
st.write(f"TONE: {result}") |