KrSharangrav commited on
Commit
af09235
·
1 Parent(s): 3c80a27

Creating the backup file and changing app.py for DB purpose

Browse files
Files changed (2) hide show
  1. app.py +20 -16
  2. backup.py +41 -0
app.py CHANGED
@@ -1,21 +1,21 @@
1
  from pymongo import MongoClient
 
 
 
2
 
 
3
  def get_mongo_client():
4
- client = MongoClient("mongodb+srv://GMP-21-03:groupa2025@cluster1.u1zed.mongodb.net/?retryWrites=true&w=majority&appName=Cluster1")
5
  db = client["sentiment_db"]
6
  return db["tweets"]
7
 
8
- #### *3. Load and Process Dataset*
9
- import pandas as pd
10
-
11
- # Load dataset
12
- df = pd.read_csv("https://huggingface.co/spaces/sharangrav24/SentimentAnalysis/resolve/main/sentiment140.csv")
13
-
14
- #### *4. Sentiment Analysis using BERT-ROBERTA*
15
 
16
- from transformers import pipeline
 
 
17
 
18
- # Load Hugging Face model
19
  sentiment_pipeline = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment")
20
 
21
  # Function to analyze sentiment
@@ -24,16 +24,20 @@ def analyze_sentiment(text):
24
 
25
  df["sentiment"] = df["text"].apply(analyze_sentiment)
26
 
27
- # Save results to MongoDB
28
- collection = get_mongo_client()
 
29
  collection.insert_many(df.to_dict("records"))
30
 
31
- #### *5. Build Streamlit Dashboard*
32
- import streamlit as st
33
-
34
  st.title("Sentiment Analysis Dashboard")
35
 
36
- if st.button("Show Data"):
 
 
 
 
 
37
  st.write(df)
38
 
39
  if st.button("Show MongoDB Data"):
 
1
  from pymongo import MongoClient
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://groupA:pythongroupA@sentimentcluster.4usfj.mongodb.net/?retryWrites=true&w=majority&appName=SentimentCluster")
9
  db = client["sentiment_db"]
10
  return db["tweets"]
11
 
12
+ 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
+ df = pd.read_csv(csv_url)
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
 
24
 
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
 
43
  if st.button("Show MongoDB Data"):
backup.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pymongo import MongoClient
2
+
3
+ def get_mongo_client():
4
+ client = MongoClient("mongodb+srv://GMP-21-03:[email protected]/?retryWrites=true&w=majority&appName=Cluster1")
5
+ db = client["sentiment_db"]
6
+ return db["tweets"]
7
+
8
+ #### *3. Load and Process Dataset*
9
+ import pandas as pd
10
+
11
+ # Load dataset
12
+ df = pd.read_csv("https://huggingface.co/spaces/sharangrav24/SentimentAnalysis/resolve/main/sentiment140.csv")
13
+
14
+ #### *4. Sentiment Analysis using BERT-ROBERTA*
15
+
16
+ from transformers import pipeline
17
+
18
+ # Load Hugging Face model
19
+ sentiment_pipeline = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment")
20
+
21
+ # Function to analyze sentiment
22
+ def analyze_sentiment(text):
23
+ return sentiment_pipeline(text)[0]['label']
24
+
25
+ df["sentiment"] = df["text"].apply(analyze_sentiment)
26
+
27
+ # Save results to MongoDB
28
+ collection = get_mongo_client()
29
+ collection.insert_many(df.to_dict("records"))
30
+
31
+ #### *5. Build Streamlit Dashboard*
32
+ import streamlit as st
33
+
34
+ st.title("Sentiment Analysis Dashboard")
35
+
36
+ if st.button("Show Data"):
37
+ st.write(df)
38
+
39
+ if st.button("Show MongoDB Data"):
40
+ data = list(collection.find({}, {"_id": 0}))
41
+ st.write(pd.DataFrame(data))