Spaces:
Sleeping
Sleeping
KrSharangrav
commited on
Commit
·
0105e3b
1
Parent(s):
af09235
Fixed UnicodeDecodeError
Browse files
app.py
CHANGED
@@ -1,11 +1,11 @@
|
|
1 |
-
|
2 |
import pandas as pd
|
|
|
3 |
from transformers import pipeline
|
4 |
-
import streamlit as st
|
5 |
|
6 |
#### **1. MongoDB Connection**
|
7 |
def get_mongo_client():
|
8 |
-
client = MongoClient("mongodb+srv://
|
9 |
db = client["sentiment_db"]
|
10 |
return db["tweets"]
|
11 |
|
@@ -13,9 +13,18 @@ collection = get_mongo_client()
|
|
13 |
|
14 |
#### **2. Load Dataset from Hugging Face**
|
15 |
csv_url = "https://huggingface.co/spaces/sharangrav24/SentimentAnalysis/resolve/main/sentiment140.csv"
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
#### **3. Sentiment Analysis using BERT-ROBERTA**
|
|
|
|
|
19 |
sentiment_pipeline = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment")
|
20 |
|
21 |
# Function to analyze sentiment
|
@@ -25,18 +34,19 @@ def analyze_sentiment(text):
|
|
25 |
df["sentiment"] = df["text"].apply(analyze_sentiment)
|
26 |
|
27 |
#### **4. Upload Data to MongoDB**
|
28 |
-
# Convert DataFrame to dictionary and upload to MongoDB
|
29 |
collection.delete_many({}) # Optional: Clear existing data before inserting
|
30 |
collection.insert_many(df.to_dict("records"))
|
|
|
31 |
|
32 |
#### **5. Build Streamlit Dashboard**
|
33 |
-
st.title("Sentiment Analysis Dashboard")
|
34 |
|
35 |
# Show first 5 rows from MongoDB
|
36 |
st.subheader("First 5 Rows from Database")
|
37 |
data = list(collection.find({}, {"_id": 0}).limit(5))
|
38 |
st.write(pd.DataFrame(data))
|
39 |
|
|
|
40 |
if st.button("Show Complete Data"):
|
41 |
st.write(df)
|
42 |
|
|
|
1 |
+
import streamlit as st
|
2 |
import pandas as pd
|
3 |
+
from pymongo import MongoClient
|
4 |
from transformers import pipeline
|
|
|
5 |
|
6 |
#### **1. MongoDB Connection**
|
7 |
def get_mongo_client():
|
8 |
+
client = MongoClient("mongodb+srv://GMP-21-03:groupa2025@cluster1.u1zed.mongodb.net/?retryWrites=true&w=majority&appName=Cluster1")
|
9 |
db = client["sentiment_db"]
|
10 |
return db["tweets"]
|
11 |
|
|
|
13 |
|
14 |
#### **2. Load Dataset from Hugging Face**
|
15 |
csv_url = "https://huggingface.co/spaces/sharangrav24/SentimentAnalysis/resolve/main/sentiment140.csv"
|
16 |
+
|
17 |
+
try:
|
18 |
+
df = pd.read_csv(csv_url, encoding="ISO-8859-1", errors="replace") # Fix encoding issue
|
19 |
+
except Exception as e:
|
20 |
+
st.error(f"Error loading dataset: {e}")
|
21 |
+
st.stop()
|
22 |
+
|
23 |
+
st.success("Dataset Loaded Successfully!")
|
24 |
|
25 |
#### **3. Sentiment Analysis using BERT-ROBERTA**
|
26 |
+
st.info("Running Sentiment Analysis...")
|
27 |
+
|
28 |
sentiment_pipeline = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment")
|
29 |
|
30 |
# Function to analyze sentiment
|
|
|
34 |
df["sentiment"] = df["text"].apply(analyze_sentiment)
|
35 |
|
36 |
#### **4. Upload Data to MongoDB**
|
|
|
37 |
collection.delete_many({}) # Optional: Clear existing data before inserting
|
38 |
collection.insert_many(df.to_dict("records"))
|
39 |
+
st.success("Data Uploaded to MongoDB!")
|
40 |
|
41 |
#### **5. Build Streamlit Dashboard**
|
42 |
+
st.title("📊 Sentiment Analysis Dashboard")
|
43 |
|
44 |
# Show first 5 rows from MongoDB
|
45 |
st.subheader("First 5 Rows from Database")
|
46 |
data = list(collection.find({}, {"_id": 0}).limit(5))
|
47 |
st.write(pd.DataFrame(data))
|
48 |
|
49 |
+
# Buttons to display more data
|
50 |
if st.button("Show Complete Data"):
|
51 |
st.write(df)
|
52 |
|