import streamlit as st import pickle import numpy as np import pandas as pd from scipy.sparse import hstack from sklearn.feature_extraction.text import CountVectorizer import nltk import re from nltk.corpus import stopwords from nltk.stem import PorterStemmer # Ensure NLTK data files are downloaded nltk.download('punkt') nltk.download('stopwords') # Define the preprocessing function def preprocess_text(text): # Remove special characters and digits text = re.sub(r'[^a-zA-Z\s]', '', text) # Convert to lowercase text = text.lower() # Tokenize the text tokens = nltk.word_tokenize(text) # Remove stopwords stop_words = set(stopwords.words('english')) tokens = [word for word in tokens if word not in stop_words] # Perform stemming stemmer = PorterStemmer() tokens = [stemmer.stem(word) for word in tokens] # Join the tokens back into a single string preprocessed_text = ' '.join(tokens) return preprocessed_text # Load the saved model model_filename = 'logistic_regression_model.pkl' with open(model_filename, 'rb') as file: model = pickle.load(file) # Load the fitted CountVectorizer vectorizer_filename = 'count_vectorizer.pkl' with open(vectorizer_filename, 'rb') as file: vect = pickle.load(file) st.title("Goodreads Book Review Rating Predictor 📖") st.image('https://s26162.pcdn.co/wp-content/uploads/2023/11/6.png') st.write('Input a book review info and let the model predict its score!') # Prefilled review text prefilled_review = """This is definitely one of my favorites among the "food books" I've read! I loved the characters (Raggedy Ann and Ken Carson included)! Even though Kayla is a pessimist (which I usually dislike in a character), her personality is the perfect formula in the story. Kayla is the school rebel, she wears unusual clothes, makes fun of the "popular" crowd in school, and she only has one friend in school---Nicole. Lately, things have been changing around her. Her mother barely talks to her and her best friend is dating her long-time crush Ben! On her disastrous 16th birthday party, she made a wish to make all her birthday wishes come true. The following day she wakes up with a pink pony in her backyard! The next day it was a room full of gumballs, and the next her Raggedy Ann doll was brought to life! She figured out that her birthday wishes are coming true and she has to find a way to stop it before her 15th birthday wish comes true---which is a kiss from Ben! I really had fun reading it. It reminded me a lot of my childhood and my silly wishes. I might have wished for a one year supply of chocolates or a new BMX bike before, LOL! Also, there are a lot of hilarious events that occurred in the story. My favorite is definitely Ken's dancing pecs! haha! The story ended abruptly though. I wish we found out what happened to Ann, Ken, the pony and the rest of her wishes...instead of Kayla waking up the following day with them gone. Overall it was a quick read. If you are looking for something light and fun, this is perfect for you!""" review_text = st.text_area("Enter the review text:", value=prefilled_review, height=410) n_votes = st.number_input("Enter the number of votes:", min_value=0, value=5) n_comments = st.number_input("Enter the number of comments:", min_value=0, value=1) if st.button("Predict"): if review_text: # Preprocess the text preprocessed_text = preprocess_text(review_text) # Vectorize the text X_text_vect = vect.transform([preprocessed_text]) # Combine with numerical features X_num = np.array([[n_votes, n_comments]]) X = hstack([X_text_vect, X_num]) # Make prediction prediction = model.predict(X) prediction = prediction.round().astype(int).clip(min=0, max=5) st.header(f"Predicted Rating: {prediction[0]} ⭐") else: st.write("Please enter the review text.")