# ---------- Sentiment Analyzer Pro (With CSV Export and Language Support) ---------- import streamlit as st import pandas as pd import time from transformers import pipeline from io import StringIO import requests # -------------------- Configurations -------------------- st.set_page_config( page_title="Sentiment Analyzer Pro", page_icon="đ§ ", layout="wide", ) # -------------------- Helper Functions -------------------- @st.cache_resource def load_pipeline(language='en'): if language == 'ur': return pipeline("sentiment-analysis", model="urduhack/bert-base-urdu-cased-sentiment") return pipeline("sentiment-analysis") classifier = load_pipeline() def analyze_sentiment(text, language): result = classifier(text)[0] return result["label"], result["score"] def create_csv(data): df = pd.DataFrame(data) csv = df.to_csv(index=False) return csv def save_csv(data, filename="sentiment_analysis.csv"): csv = create_csv(data) st.download_button( label="Download CSV", data=csv, file_name=filename, mime="text/csv", ) def submit_contact_form(name, email, message): # [Optional] You can connect this with Google Forms or Email APIs st.success(f"â Thank you {name}, your message has been received!") def show_footer(): st.markdown("---") st.markdown( "
Š 2025 Sentiment Analyzer Pro | Built with â¤ī¸ using Hugging Face and Streamlit
", unsafe_allow_html=True, ) # -------------------- Sidebar -------------------- st.sidebar.title("đ Navigation") page = st.sidebar.radio("Go to", ["Home", "About", "Contact"]) st.sidebar.title("đ Use Cases (on Home)") use_case = st.sidebar.radio( "Choose a Use Case", ( "General Text", "Social Media Post", "Customer Review", "Email/Message", "Product Description", ) ) st.sidebar.title("đ Language") language = st.sidebar.selectbox( "Select Language", ("English", "Urdu"), ) st.sidebar.markdown("---") st.sidebar.info( "This app uses a **Transformers-based model** to predict the **sentiment** " "(Positive, Negative, Neutral) with a **confidence score**." ) st.sidebar.write("Made with â¤ī¸ by M-Abdullah") # -------------------- Page Logic -------------------- if page == "Home": # ------------- HOME PAGE ------------- st.markdown("Analyze emotions in text, reviews, posts, and more!
", unsafe_allow_html=True) st.write("---") st.write(f"### âī¸ Enter your {use_case} below:") user_input = st.text_area("", placeholder=f"Write your {use_case.lower()} here...", height=200) if st.button("đ Analyze Sentiment"): if user_input.strip() == "": st.warning("â ī¸ Please enter some text to analyze!") else: with st.spinner('Analyzing...'): label, score = analyze_sentiment(user_input, language) time.sleep(1) # Smooth spinner effect # Emoji Reaction emoji = { "POSITIVE": "đ", "NEGATIVE": "đ", "NEUTRAL": "đ" }.get(label.upper(), "đ¤") # Result Display st.success(f"**Sentiment:** {label} {emoji}") st.info(f"**Confidence Score:** {score:.2f}") # Chart Section st.write("### đ Sentiment Score") st.bar_chart({"Confidence Score": [score]}) # Detailed Explanation st.write("### đ Interpretation") if label == "POSITIVE": st.write("â This text expresses positive emotions, happiness, satisfaction, or support.") elif label == "NEGATIVE": st.write("â ī¸ This text expresses negative emotions, dissatisfaction, criticism, or sadness.") else: st.write("âšī¸ This text is neutral without strong emotions.") # Download CSV Button save_csv([{"Text": user_input, "Sentiment": label, "Confidence Score": score}], "sentiment_analysis.csv") elif page == "About": # ------------- ABOUT PAGE ------------- st.markdown("Made with â¤ī¸ by M-Abdullah | 2025
", unsafe_allow_html=True) # -------------------- Footer -------------------- show_footer()