Spaces:
Build error
Build error
Update app.py
Browse filesadd extra file
app.py
CHANGED
|
@@ -1,59 +1,63 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import joblib
|
| 3 |
-
import json
|
| 4 |
-
import re
|
| 5 |
-
import string
|
| 6 |
-
import numpy as np
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
from
|
| 10 |
-
from nltk.
|
| 11 |
-
from nltk.
|
| 12 |
-
from
|
| 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 |
st.write("Please enter text to classify.")
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import joblib
|
| 3 |
+
import json
|
| 4 |
+
import re
|
| 5 |
+
import string
|
| 6 |
+
import numpy as np
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
from tensorflow.keras.models import load_model
|
| 10 |
+
from nltk.corpus import stopwords
|
| 11 |
+
from nltk.stem import WordNetLemmatizer
|
| 12 |
+
from nltk.tokenize import word_tokenize, sent_tokenize
|
| 13 |
+
from sklearn.feature_extraction.text import CountVectorizer
|
| 14 |
+
|
| 15 |
+
if not os.path.exist('/root/nltk_data'):
|
| 16 |
+
os.system("python download_nltk_data.py")
|
| 17 |
+
|
| 18 |
+
model = load_model('model_improved.keras')
|
| 19 |
+
vectorizer = joblib.load('vectorizer.joblib')
|
| 20 |
+
|
| 21 |
+
with open('product_mapping.json', 'r') as file1:
|
| 22 |
+
product_mapping = json.load(file1)
|
| 23 |
+
reverse_mapping = {v: k for k, v in product_mapping.items()}
|
| 24 |
+
|
| 25 |
+
lemmatizer = WordNetLemmatizer()
|
| 26 |
+
stop_words = set(stopwords.words('english'))
|
| 27 |
+
|
| 28 |
+
def clean_text(text):
|
| 29 |
+
if text is None:
|
| 30 |
+
return ""
|
| 31 |
+
text = re.sub(r'\bx+\b', '', text)
|
| 32 |
+
text = re.sub(r'\b(\w+)( \1){2,}\b', r'\1', text)
|
| 33 |
+
sentences = sent_tokenize(text)
|
| 34 |
+
cleaned_sentences = [sentence.strip().capitalize() + '.' for sentence in sentences if sentence]
|
| 35 |
+
return ' '.join(cleaned_sentences)
|
| 36 |
+
|
| 37 |
+
def preprocessing_text(text):
|
| 38 |
+
text = clean_text(text)
|
| 39 |
+
text = text.lower()
|
| 40 |
+
text = text.translate(str.maketrans('', '', string.punctuation))
|
| 41 |
+
words = word_tokenize(text)
|
| 42 |
+
words = [lemmatizer.lemmatize(word) for word in words if word not in stop_words]
|
| 43 |
+
words = list(dict.fromkeys(words))
|
| 44 |
+
return ' '.join(words)
|
| 45 |
+
|
| 46 |
+
def make_prediction(input_text):
|
| 47 |
+
preprocessed_text = preprocessing_text(input_text)
|
| 48 |
+
vectorized_input = vectorizer.transform([preprocessed_text])
|
| 49 |
+
predictions = model.predict(vectorized_input)
|
| 50 |
+
predicted_class = np.argmax(predictions, axis=1)
|
| 51 |
+
predicted_label = reverse_mapping[predicted_class[0]]
|
| 52 |
+
return predicted_label
|
| 53 |
+
|
| 54 |
+
st.title("Text Classification with NLP")
|
| 55 |
+
st.write("Enter text to classify into predefined categories")
|
| 56 |
+
|
| 57 |
+
user_input = st.text_area("Input Text", "")
|
| 58 |
+
if st.button("Classify"):
|
| 59 |
+
if user_input:
|
| 60 |
+
result = make_prediction(user_input)
|
| 61 |
+
st.write(f"Predicted Category: {result}")
|
| 62 |
+
else:
|
| 63 |
st.write("Please enter text to classify.")
|