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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ---------- Sentiment Analyzer Pro (With CSV Export and Language Support) ----------
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import time
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
from io import StringIO
|
| 7 |
+
import requests
|
| 8 |
+
|
| 9 |
+
# -------------------- Configurations --------------------
|
| 10 |
+
st.set_page_config(
|
| 11 |
+
page_title="Sentiment Analyzer Pro",
|
| 12 |
+
page_icon="π§ ",
|
| 13 |
+
layout="wide",
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# -------------------- Helper Functions --------------------
|
| 17 |
+
@st.cache_resource
|
| 18 |
+
def load_pipeline(language='en'):
|
| 19 |
+
if language == 'ur':
|
| 20 |
+
return pipeline("sentiment-analysis", model="urduhack/bert-base-urdu-cased-sentiment")
|
| 21 |
+
return pipeline("sentiment-analysis")
|
| 22 |
+
|
| 23 |
+
classifier = load_pipeline()
|
| 24 |
+
|
| 25 |
+
def analyze_sentiment(text, language):
|
| 26 |
+
result = classifier(text)[0]
|
| 27 |
+
return result["label"], result["score"]
|
| 28 |
+
|
| 29 |
+
def create_csv(data):
|
| 30 |
+
df = pd.DataFrame(data)
|
| 31 |
+
csv = df.to_csv(index=False)
|
| 32 |
+
return csv
|
| 33 |
+
|
| 34 |
+
def save_csv(data, filename="sentiment_analysis.csv"):
|
| 35 |
+
csv = create_csv(data)
|
| 36 |
+
st.download_button(
|
| 37 |
+
label="Download CSV",
|
| 38 |
+
data=csv,
|
| 39 |
+
file_name=filename,
|
| 40 |
+
mime="text/csv",
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
def submit_contact_form(name, email, message):
|
| 44 |
+
# [Optional] You can connect this with Google Forms or Email APIs
|
| 45 |
+
st.success(f"β
Thank you {name}, your message has been received!")
|
| 46 |
+
|
| 47 |
+
def show_footer():
|
| 48 |
+
st.markdown("---")
|
| 49 |
+
st.markdown(
|
| 50 |
+
"<p style='text-align: center; font-size: 14px;'>Β© 2025 Sentiment Analyzer Pro | Built with β€οΈ using Hugging Face and Streamlit</p>",
|
| 51 |
+
unsafe_allow_html=True,
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# -------------------- Sidebar --------------------
|
| 55 |
+
st.sidebar.title("π Navigation")
|
| 56 |
+
page = st.sidebar.radio("Go to", ["Home", "About", "Contact"])
|
| 57 |
+
|
| 58 |
+
st.sidebar.title("π Use Cases (on Home)")
|
| 59 |
+
use_case = st.sidebar.radio(
|
| 60 |
+
"Choose a Use Case",
|
| 61 |
+
(
|
| 62 |
+
"General Text",
|
| 63 |
+
"Social Media Post",
|
| 64 |
+
"Customer Review",
|
| 65 |
+
"Email/Message",
|
| 66 |
+
"Product Description",
|
| 67 |
+
)
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
st.sidebar.title("π Language")
|
| 71 |
+
language = st.sidebar.selectbox(
|
| 72 |
+
"Select Language",
|
| 73 |
+
("English", "Urdu"),
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
st.sidebar.markdown("---")
|
| 77 |
+
st.sidebar.info(
|
| 78 |
+
"This app uses a **Transformers-based model** to predict the **sentiment** "
|
| 79 |
+
"(Positive, Negative, Neutral) with a **confidence score**."
|
| 80 |
+
)
|
| 81 |
+
st.sidebar.write("Made with β€οΈ by M-Abdullah")
|
| 82 |
+
|
| 83 |
+
# -------------------- Page Logic --------------------
|
| 84 |
+
|
| 85 |
+
if page == "Home":
|
| 86 |
+
# ------------- HOME PAGE -------------
|
| 87 |
+
st.markdown("<h1 style='text-align: center; color: #4CAF50;'>π§ Sentiment Analyzer Pro</h1>", unsafe_allow_html=True)
|
| 88 |
+
st.markdown("<p style='text-align: center;'>Analyze emotions in text, reviews, posts, and more!</p>", unsafe_allow_html=True)
|
| 89 |
+
st.write("---")
|
| 90 |
+
|
| 91 |
+
st.write(f"### βοΈ Enter your {use_case} below:")
|
| 92 |
+
user_input = st.text_area("", placeholder=f"Write your {use_case.lower()} here...", height=200)
|
| 93 |
+
|
| 94 |
+
if st.button("π Analyze Sentiment"):
|
| 95 |
+
if user_input.strip() == "":
|
| 96 |
+
st.warning("β οΈ Please enter some text to analyze!")
|
| 97 |
+
else:
|
| 98 |
+
with st.spinner('Analyzing...'):
|
| 99 |
+
label, score = analyze_sentiment(user_input, language)
|
| 100 |
+
time.sleep(1) # Smooth spinner effect
|
| 101 |
+
|
| 102 |
+
# Emoji Reaction
|
| 103 |
+
emoji = {
|
| 104 |
+
"POSITIVE": "π",
|
| 105 |
+
"NEGATIVE": "π",
|
| 106 |
+
"NEUTRAL": "π"
|
| 107 |
+
}.get(label.upper(), "π€")
|
| 108 |
+
|
| 109 |
+
# Result Display
|
| 110 |
+
st.success(f"**Sentiment:** {label} {emoji}")
|
| 111 |
+
st.info(f"**Confidence Score:** {score:.2f}")
|
| 112 |
+
|
| 113 |
+
# Chart Section
|
| 114 |
+
st.write("### π Sentiment Score")
|
| 115 |
+
st.bar_chart({"Confidence Score": [score]})
|
| 116 |
+
|
| 117 |
+
# Detailed Explanation
|
| 118 |
+
st.write("### π Interpretation")
|
| 119 |
+
if label == "POSITIVE":
|
| 120 |
+
st.write("β
This text expresses positive emotions, happiness, satisfaction, or support.")
|
| 121 |
+
elif label == "NEGATIVE":
|
| 122 |
+
st.write("β οΈ This text expresses negative emotions, dissatisfaction, criticism, or sadness.")
|
| 123 |
+
else:
|
| 124 |
+
st.write("βΉοΈ This text is neutral without strong emotions.")
|
| 125 |
+
|
| 126 |
+
# Download CSV Button
|
| 127 |
+
save_csv([{"Text": user_input, "Sentiment": label, "Confidence Score": score}], "sentiment_analysis.csv")
|
| 128 |
+
|
| 129 |
+
elif page == "About":
|
| 130 |
+
# ------------- ABOUT PAGE -------------
|
| 131 |
+
st.markdown("<h1 style='text-align: center; color: #4CAF50;'>π About Sentiment Analyzer Pro</h1>", unsafe_allow_html=True)
|
| 132 |
+
st.write("---")
|
| 133 |
+
st.markdown("""
|
| 134 |
+
### β¨ Overview
|
| 135 |
+
**Sentiment Analyzer Pro** is an advanced natural language processing (NLP) app built using:
|
| 136 |
+
- π **Streamlit** for the web app interface
|
| 137 |
+
- π§ **Hugging Face Transformers** for deep learning models
|
| 138 |
+
|
| 139 |
+
This application can detect and classify **sentiments** (Positive, Negative, Neutral) in:
|
| 140 |
+
- General texts
|
| 141 |
+
- Social media posts (e.g., Twitter, Facebook)
|
| 142 |
+
- Customer feedback
|
| 143 |
+
- Emails and messaging
|
| 144 |
+
- Product descriptions
|
| 145 |
+
|
| 146 |
+
### π― Objectives
|
| 147 |
+
- Simplify sentiment analysis for all users
|
| 148 |
+
- Provide fast, reliable, real-time emotional analysis
|
| 149 |
+
- Useful for businesses, researchers, students, and individuals
|
| 150 |
+
|
| 151 |
+
### π Future Enhancements
|
| 152 |
+
- Multilingual Sentiment Analysis (Urdu, Arabic, French)
|
| 153 |
+
- Custom Model Uploads
|
| 154 |
+
- Sentiment Trends over Time
|
| 155 |
+
""")
|
| 156 |
+
|
| 157 |
+
elif page == "Contact":
|
| 158 |
+
FORM_URL = "https://docs.google.com/forms/u/0/d/1A95xalIMN6jDN8DCAq3agQGGDaDXP7x1hed-DjHqxow/prefill"
|
| 159 |
+
FIELD_IDS = {
|
| 160 |
+
"name": "entry.796411702", # replace with actual ID
|
| 161 |
+
"email": "entry.796411702", # replace with actual ID
|
| 162 |
+
"message": "entry.939908203", # Correct ID
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
# Page Title
|
| 166 |
+
st.markdown("<h1 style='text-align: center; color: #4CAF50;'>π¬ Contact Us</h1>", unsafe_allow_html=True)
|
| 167 |
+
st.markdown("---")
|
| 168 |
+
|
| 169 |
+
st.markdown("""
|
| 170 |
+
### π¬ We'd Love to Hear From You!
|
| 171 |
+
Fill out the form below and your message will be saved to our system.
|
| 172 |
+
""")
|
| 173 |
+
|
| 174 |
+
with st.form(key='contact_form'):
|
| 175 |
+
name = st.text_input("Your Name")
|
| 176 |
+
email = st.text_input("Your Email")
|
| 177 |
+
message = st.text_area("Your Message")
|
| 178 |
+
|
| 179 |
+
submit_button = st.form_submit_button(label='Send Message')
|
| 180 |
+
|
| 181 |
+
if submit_button:
|
| 182 |
+
if name.strip() == "" or email.strip() == "" or message.strip() == "":
|
| 183 |
+
st.warning("β οΈ Please fill out all fields before submitting.")
|
| 184 |
+
else:
|
| 185 |
+
# Debugging: Check form values
|
| 186 |
+
st.write(f"Name: {name}, Email: {email}, Message: {message}")
|
| 187 |
+
|
| 188 |
+
# Submit data to Google Form
|
| 189 |
+
data = {
|
| 190 |
+
FIELD_IDS["name"]: name,
|
| 191 |
+
FIELD_IDS["email"]: email,
|
| 192 |
+
FIELD_IDS["message"]: message,
|
| 193 |
+
}
|
| 194 |
+
response = requests.post(FORM_URL, data=data)
|
| 195 |
+
|
| 196 |
+
if response.status_code == 200:
|
| 197 |
+
st.success(f"β
Thank you {name}! Your message has been sent successfully.")
|
| 198 |
+
else:
|
| 199 |
+
st.error(f"β There was a problem sending your message. Error: {response.status_code}")
|
| 200 |
+
|
| 201 |
+
# Footer
|
| 202 |
+
st.markdown("---")
|
| 203 |
+
st.markdown("<p style='text-align: center; font-size: 16px;'>Made with β€οΈ by <strong>M-Abdullah</strong> | 2025</p>", unsafe_allow_html=True)
|
| 204 |
+
|
| 205 |
+
# -------------------- Footer --------------------
|
| 206 |
+
show_footer()
|