KrSharangrav
Fixed UnicodeDecodeError
0105e3b
raw
history blame
1.77 kB
import streamlit as st
import pandas as pd
from pymongo import MongoClient
from transformers import pipeline
#### **1. MongoDB Connection**
def get_mongo_client():
client = MongoClient("mongodb+srv://GMP-21-03:[email protected]/?retryWrites=true&w=majority&appName=Cluster1")
db = client["sentiment_db"]
return db["tweets"]
collection = get_mongo_client()
#### **2. Load Dataset from Hugging Face**
csv_url = "https://huggingface.co/spaces/sharangrav24/SentimentAnalysis/resolve/main/sentiment140.csv"
try:
df = pd.read_csv(csv_url, encoding="ISO-8859-1", errors="replace") # Fix encoding issue
except Exception as e:
st.error(f"Error loading dataset: {e}")
st.stop()
st.success("Dataset Loaded Successfully!")
#### **3. Sentiment Analysis using BERT-ROBERTA**
st.info("Running Sentiment Analysis...")
sentiment_pipeline = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment")
# Function to analyze sentiment
def analyze_sentiment(text):
return sentiment_pipeline(text)[0]['label']
df["sentiment"] = df["text"].apply(analyze_sentiment)
#### **4. Upload Data to MongoDB**
collection.delete_many({}) # Optional: Clear existing data before inserting
collection.insert_many(df.to_dict("records"))
st.success("Data Uploaded to MongoDB!")
#### **5. Build Streamlit Dashboard**
st.title("πŸ“Š Sentiment Analysis Dashboard")
# Show first 5 rows from MongoDB
st.subheader("First 5 Rows from Database")
data = list(collection.find({}, {"_id": 0}).limit(5))
st.write(pd.DataFrame(data))
# Buttons to display more data
if st.button("Show Complete Data"):
st.write(df)
if st.button("Show MongoDB Data"):
data = list(collection.find({}, {"_id": 0}))
st.write(pd.DataFrame(data))