import streamlit as st import tensorflow as tf import pickle import numpy as np import os from PIL import Image # --- CONFIGURATION --- st.set_page_config(page_title="Hotel Sentiment Analysis", page_icon="🏨") # --- FILE PATH SETUP --- # This ensures the app finds files regardless of whether it's on your Mac or Hugging Face current_dir = os.path.dirname(os.path.abspath(__file__)) model_path = os.path.join(current_dir, 'hotel_sentiment_model.keras') vectorizer_path = os.path.join(current_dir, 'vectorizer.pkl') image_path = os.path.join(current_dir, 'hotel_shaped_cloud.png') # --- ASSET LOADING --- @st.cache_resource def load_resources(): # Load the Deep Learning model model = tf.keras.models.load_model(model_path) # Load the CountVectorizer with open(vectorizer_path, 'rb') as f: vect = pickle.load(f) return model, vect try: model, vect = load_resources() assets_loaded = True except Exception as e: st.error(f"Error loading model or vectorizer: {e}") assets_loaded = False # --- UI SETUP --- # Attempt to load and display the hotel-shaped WordCloud if os.path.exists(image_path): try: header_img = Image.open(image_path) st.image(header_img, use_column_width=True) except: st.title("🏨 Hotel Sentiment Analyzer") else: st.title("🏨 Hotel Sentiment Analyzer") st.write("### How does the guest feel?") st.write("Paste a review below, and our Neural Network will determine if it represents a 1-Star or 5-Star experience.") # --- PREDICTION LOGIC --- user_input = st.text_area("Paste a review here:", placeholder="Type something like: 'The room was clean and the staff was amazing'...") if st.button("Analyze Sentiment"): if assets_loaded and user_input.strip(): # 1. Preprocess and Vectorize # We wrap [user_input] in a list because the vectorizer expects an iterable input_data = vect.transform([user_input]).toarray() # 2. Model Prediction prediction = model.predict(input_data) score = float(prediction[0][0]) # 3. Display Results st.divider() if score > 0.5: st.success(f"### 🌟 5-Star Experience (Confidence: {score:.2%})") st.balloons() else: # For 1-star, the confidence is 1 minus the score st.error(f"### 🛑 1-Star Experience (Confidence: {(1-score):.2%})") elif not user_input.strip(): st.warning("Please enter some text first!") # --- FOOTER --- st.sidebar.markdown("---") st.sidebar.info("Developed by **Basak Tamer**") st.sidebar.write("This model uses an ANN architecture to classify sentiment with 98.22% accuracy.")