File size: 1,151 Bytes
ea4634d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from pymongo import MongoClient

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"]

#### *3. Load and Process Dataset*
import pandas as pd

# Load dataset
df = pd.read_csv("C:/Users/ANOOP G ZACHARIA/Downloads/archive/sentiment140.csv")

#### *4. Sentiment Analysis using BERT-ROBERTA*

from transformers import pipeline

# Load Hugging Face model
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)

# Save results to MongoDB
collection = get_mongo_client()
collection.insert_many(df.to_dict("records"))

#### *5. Build Streamlit Dashboard*
import streamlit as st

st.title("Sentiment Analysis Dashboard")

if st.button("Show Data"):
    st.write(df)

if st.button("Show MongoDB Data"):
    data = list(collection.find({}, {"_id": 0}))
    st.write(pd.DataFrame(data))