KrSharangrav commited on
Commit
58c2482
Β·
1 Parent(s): 4837d8e

adding db.py for inserting the data into mongoDB

Browse files
Files changed (2) hide show
  1. app.py +14 -32
  2. db.py +33 -0
app.py CHANGED
@@ -1,45 +1,27 @@
1
  import streamlit as st
2
  import pandas as pd
3
- import requests
4
- import io
5
  from pymongo import MongoClient
 
6
 
7
- #### **1. MongoDB Connection**
8
- def get_mongo_client():
9
- client = MongoClient("mongodb+srv://groupA:[email protected]/?retryWrites=true&w=majority&appName=SentimentCluster")
10
- db = client["sentiment_db"]
11
- return db["tweets"]
12
 
 
13
  collection = get_mongo_client()
14
 
15
- #### **2. Load Dataset from Hugging Face**
16
- csv_url = "https://huggingface.co/spaces/sharangrav24/SentimentAnalysis/resolve/main/sentiment140.csv"
17
-
18
- try:
19
- response = requests.get(csv_url)
20
- response.raise_for_status() # Ensure the request was successful
21
- df = pd.read_csv(io.StringIO(response.text), encoding="ISO-8859-1")
22
- st.success("Dataset Loaded Successfully!")
23
- except Exception as e:
24
- st.error(f"Error loading dataset: {e}")
25
- st.stop()
26
-
27
- #### **3. Upload Data to MongoDB**
28
- collection.insert_many(df.to_dict("records"))
29
- st.success("Data Uploaded to MongoDB!")
30
-
31
- #### **4. Build Streamlit Dashboard**
32
- st.title("πŸ“Š MongoDB Data Insertion")
33
 
34
  # Show first 5 rows from MongoDB
35
  st.subheader("First 5 Rows from Database")
36
  data = list(collection.find({}, {"_id": 0}).limit(5))
37
- st.write(pd.DataFrame(data))
38
 
39
- # Buttons to display more data
40
- if st.button("Show Complete Data"):
41
- st.write(df)
42
-
43
- if st.button("Show MongoDB Data"):
44
- data = list(collection.find({}, {"_id": 0}))
45
  st.write(pd.DataFrame(data))
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import pandas as pd
 
 
3
  from pymongo import MongoClient
4
+ from db import insert_data_if_empty, get_mongo_client # Import functions from db.py
5
 
6
+ #### **1. Ensure Data is Inserted Before Display**
7
+ insert_data_if_empty()
 
 
 
8
 
9
+ #### **2. MongoDB Connection**
10
  collection = get_mongo_client()
11
 
12
+ #### **3. Streamlit App to Display Data**
13
+ st.title("πŸ“Š MongoDB Data Viewer")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  # Show first 5 rows from MongoDB
16
  st.subheader("First 5 Rows from Database")
17
  data = list(collection.find({}, {"_id": 0}).limit(5))
 
18
 
19
+ if data:
 
 
 
 
 
20
  st.write(pd.DataFrame(data))
21
+ else:
22
+ st.warning("⚠️ No data found. Try refreshing the app.")
23
+
24
+ # Button to show full MongoDB data
25
+ if st.button("Show Complete Data"):
26
+ all_data = list(collection.find({}, {"_id": 0}))
27
+ st.write(pd.DataFrame(all_data))
db.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import requests
3
+ import io
4
+ from pymongo import MongoClient
5
+
6
+ #### **1. MongoDB Connection**
7
+ def get_mongo_client():
8
+ client = MongoClient("mongodb+srv://groupA:[email protected]/?retryWrites=true&w=majority&appName=SentimentCluster")
9
+ db = client["sentiment_db"]
10
+ return db["tweets"]
11
+
12
+ #### **2. Insert Data If Collection is Empty**
13
+ def insert_data_if_empty():
14
+ collection = get_mongo_client()
15
+
16
+ if collection.count_documents({}) == 0:
17
+ print("🟒 No data found. Inserting dataset...")
18
+
19
+ csv_url = "https://huggingface.co/spaces/sharangrav24/SentimentAnalysis/resolve/main/sentiment140.csv"
20
+
21
+ try:
22
+ response = requests.get(csv_url)
23
+ response.raise_for_status() # Ensure request was successful
24
+ df = pd.read_csv(io.StringIO(response.text), encoding="ISO-8859-1")
25
+
26
+ # Insert into MongoDB
27
+ collection.insert_many(df.to_dict("records"))
28
+ print("βœ… Data Inserted into MongoDB!")
29
+ except Exception as e:
30
+ print(f"❌ Error loading dataset: {e}")
31
+
32
+ # Uncomment the below line if running `db.py` independently
33
+ # insert_data_if_empty()