import pandas as pd import requests import io from pymongo import MongoClient # Function to establish MongoDB connection and return the collection def get_mongo_client(): client = MongoClient("mongodb+srv://groupA:pythongroupA@sentimentcluster.4usfj.mongodb.net/?retryWrites=true&w=majority&appName=SentimentCluster") db = client["sentiment_db"] return db["tweets"] # Function to insert the dataset into MongoDB if the collection is empty def insert_data_if_empty(): collection = get_mongo_client() if collection.count_documents({}) == 0: print("🟢 No data found in MongoDB. Inserting dataset...") 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") # Insert dataset records into MongoDB collection.insert_many(df.to_dict("records")) print("✅ Data Inserted into MongoDB!") except Exception as e: print(f"❌ Error loading dataset: {e}")