Download app.py from basaktamer/trip_advisor_sentiment_analysis: direct link, hf CLI and curl.
- Browser
- Download file 2.7 kB
-
https://huggingface.co/spaces/basaktamer/trip_advisor_sentiment_analysis/resolve/main/app.py
- Command line
-
hf download hf://spaces/basaktamer/trip_advisor_sentiment_analysis/app.py
-
curl -L -o app.py https://huggingface.co/spaces/basaktamer/trip_advisor_sentiment_analysis/resolve/main/app.py
2.7 kB
| 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 --- | |
| 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.") |