File size: 1,497 Bytes
0105e3b
af09235
ba2af1a
 
0105e3b
ea4634d
af09235
ea4634d
922ab20
ea4634d
 
 
af09235
ea4634d
af09235
 
0105e3b
 
ba2af1a
 
 
 
0105e3b
 
 
 
ad9bf8d
af09235
ea4634d
0105e3b
ea4634d
ad9bf8d
 
ea4634d
af09235
 
 
 
 
0105e3b
af09235
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
43
44
45
46
47
import streamlit as st
import pandas as pd
import requests
import io
from pymongo import MongoClient

#### **1. MongoDB Connection**
def get_mongo_client():
    client = MongoClient("mongodb+srv://groupA:[email protected]/?retryWrites=true&w=majority&appName=SentimentCluster")
    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:
    response = requests.get(csv_url)
    response.raise_for_status()  # Ensure the request was successful
    df = pd.read_csv(io.StringIO(response.text), encoding="ISO-8859-1")
    st.success("Dataset Loaded Successfully!")
except Exception as e:
    st.error(f"Error loading dataset: {e}")
    st.stop()

#### **3. 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!")

#### **4. Build Streamlit Dashboard**
st.title("📊 MongoDB Data Insertion")

# 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))