Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,74 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
user_input = st.text_input("")
|
7 |
if user_input:
|
8 |
-
result =
|
9 |
-
|
10 |
-
confidence = ''
|
11 |
-
|
12 |
-
st.write(f"Sentiment: {sentiment}")
|
13 |
-
st.write(f"Confidence: {confidence:.2f}")
|
|
|
1 |
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as ny
|
4 |
+
from keras.preprocessing.text import Tokenizer
|
5 |
+
from keras.preprocessing.sequence import pad_sequences
|
6 |
+
from sklearn.preprocessing import LabelEncoder
|
7 |
+
from keras.layers import *
|
8 |
+
from keras import Model
|
9 |
|
10 |
+
map_id = {
|
11 |
+
0: "sadness",
|
12 |
+
1: "anger",
|
13 |
+
2: "love",
|
14 |
+
3: "surprise",
|
15 |
+
4: "fear",
|
16 |
+
5: "joy"
|
17 |
+
}
|
18 |
+
map_emotion = {
|
19 |
+
"sadness": 0,
|
20 |
+
"anger": 1,
|
21 |
+
"love": 2,
|
22 |
+
"surprise": 3,
|
23 |
+
"fear": 4,
|
24 |
+
"joy": 5
|
25 |
+
}
|
26 |
+
|
27 |
+
train = pd.read_csv('/train.csv')
|
28 |
+
for index, row in train.iterrows():
|
29 |
+
row['emotion'] = map_emotion[row['emotion']]
|
30 |
+
tokenizer = Tokenizer()
|
31 |
+
tokenizer.fit_on_texts(train.text)
|
32 |
+
Length = len(tokenizer.word_index) + 1
|
33 |
+
x_train = pad_sequences(tokenizer.texts_to_sequences(train.text), maxlen=30)
|
34 |
+
encoder = LabelEncoder()
|
35 |
+
encoder.fit(train["emotion"].to_list())
|
36 |
+
y_train = encoder.transform(train["emotion"].to_list())
|
37 |
+
y_train = y_train.reshape(-1, 1)
|
38 |
+
|
39 |
+
embedding_layer = Embedding(Length,
|
40 |
+
64,
|
41 |
+
input_length=30)
|
42 |
+
|
43 |
+
input_seq = Input(shape=(x_train.shape[1],))
|
44 |
+
x = embedding_layer(input_seq)
|
45 |
+
x = LSTM(10, return_sequences=True) (x)
|
46 |
+
x = Flatten() (x)
|
47 |
+
output = Dense(encoder.classes_.shape[0], activation="softmax") (x)
|
48 |
+
|
49 |
+
model = Model(input_seq, output)
|
50 |
+
model.compile(optimizer='adam',
|
51 |
+
loss="sparse_categorical_crossentropy",
|
52 |
+
metrics=["accuracy"])
|
53 |
+
model.fit(x_train, y_train, epochs=20, batch_size=32,
|
54 |
+
validation_data=(x_val, y_val))
|
55 |
+
class Predict:
|
56 |
+
def __init__(self, model, tokenizer):
|
57 |
+
self.model = model
|
58 |
+
self.tokenizer = tokenizer
|
59 |
+
|
60 |
+
def predict(self, txt):
|
61 |
+
x = pad_sequences(self.tokenizer.texts_to_sequences([txt]), maxlen=30)
|
62 |
+
x = self.model(x)
|
63 |
+
x = ny.argmax(x)
|
64 |
+
return map_id[x]
|
65 |
+
predict = Predict(model, tokenizer)
|
66 |
+
|
67 |
+
|
68 |
+
st.title("TONE DETECTION | BCS WINTER PROJECT")
|
69 |
+
st.write("Enter a sentence to analyze text's Tone:")
|
70 |
|
71 |
user_input = st.text_input("")
|
72 |
if user_input:
|
73 |
+
result = predict.predict(user_input)
|
74 |
+
st.write(f"TONE: {result}")
|
|
|
|
|
|
|
|