Spaces:
Sleeping
Sleeping
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) | |
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}") |