Download app.py from sleepy-panda21/Review_sentiment_analysis: direct link, hf CLI and curl.
- Browser
- Download file 3.52 kB
-
https://huggingface.co/spaces/sleepy-panda21/Review_sentiment_analysis/resolve/main/app.py
- Command line
-
hf download hf://spaces/sleepy-panda21/Review_sentiment_analysis/app.py
-
curl -L -o app.py https://huggingface.co/spaces/sleepy-panda21/Review_sentiment_analysis/resolve/main/app.py
3.52 kB
| import os | |
| import pickle | |
| import numpy as np | |
| import tensorflow as tf | |
| from tensorflow.keras.preprocessing.sequence import pad_sequences | |
| from tensorflow.keras.models import Sequential | |
| from tensorflow.keras.layers import Input, Embedding, LSTM, Bidirectional, GlobalAveragePooling1D, Dense, Dropout | |
| import gradio as gr | |
| MAX_LEN = 200 | |
| MODEL_PATH = "sentiment_model.keras" | |
| TOKENIZER_PATH = "tokenizer.pickle" | |
| if os.path.exists(TOKENIZER_PATH): | |
| with open(TOKENIZER_PATH, "rb") as handle: | |
| tokenizer = pickle.load(handle) | |
| print("Tokenizer loaded successfully.") | |
| else: | |
| raise FileNotFoundError(f"Missing critical asset: {TOKENIZER_PATH}") | |
| def build_model(): | |
| model=Sequential() | |
| model.add(Input(shape=(200,))) | |
| model.add(Embedding(5000,128)) | |
| model.add(LSTM(128,dropout=0.2)) | |
| model.add(Dense(1,activation='sigmoid')) | |
| return model | |
| model = build_model() | |
| model.load_weights("sentiment_weights.weights.h5") | |
| def analyze_text(text): | |
| # Handle empty inputs gracefully | |
| if not text or not text.strip(): | |
| return {"Invalid Input: Please type some text.": 1.0} | |
| # Preprocessing pipeline | |
| sequences = tokenizer.texts_to_sequences([text]) | |
| padded = pad_sequences(sequences, maxlen=MAX_LEN) | |
| # Model inference | |
| prediction = float(model.predict(padded)[0][0]) | |
| return { | |
| "Positive ๐": prediction, | |
| "Negative ๐": 1.0 - prediction | |
| } | |
| # GRADIO BLOCKS LAYOUT | |
| with gr.Blocks(theme=gr.themes.Soft(), title="Sentiment Analyzer Pro") as demo: | |
| # Header Section | |
| gr.Markdown( | |
| """ | |
| # ๐ง Deep Learning Sentiment Classifier | |
| Analyze the emotional polarity of text using an optimized **Bidirectional LSTM** recurrent neural network. | |
| """ | |
| ) | |
| # Workspace Layout ( | |
| with gr.Row(): | |
| with gr.Column(scale=2): | |
| input_text = gr.Textbox( | |
| label="Input Review / Text Passage", | |
| placeholder="Type or paste your text here (e.g., product review, tweet, movie feedback)...", | |
| lines=6, | |
| max_lines=10, | |
| interactive=True | |
| ) | |
| with gr.Row(): | |
| clear_btn = gr.Button("Clear", variant="secondary") | |
| submit_btn = gr.Button("Analyze Sentiment", variant="primary") | |
| with gr.Column(scale=1): | |
| output_label = gr.Label( | |
| label="Classification Output", | |
| num_top_classes=2, | |
| show_label=True | |
| ) | |
| gr.Examples( | |
| examples=[ | |
| ["The cinematic direction was absolutely breathtaking, and the cast delivered a masterful performance. Highly recommended!"], | |
| ["An absolute disaster. The pacing was agonizingly slow, the dialogue felt completely forced, and I walked out early."], | |
| ["The device has decent battery life and a nice screen, but the software bugs make it frustrating to use daily."] | |
| ], | |
| inputs=input_text, | |
| label="Click an example to test instantly:" | |
| ) | |
| submit_btn.click( | |
| fn=analyze_text, | |
| inputs=input_text, | |
| outputs=output_label | |
| ) | |
| input_text.submit( | |
| fn=analyze_text, | |
| inputs=input_text, | |
| outputs=output_label | |
| ) | |
| clear_btn.click( | |
| fn=lambda: ("", None), | |
| inputs=None, | |
| outputs=[input_text, output_label] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |