Spaces:
Sleeping
Sleeping
| import asyncio | |
| import uuid | |
| import random | |
| from datetime import datetime, timedelta | |
| from motor.motor_asyncio import AsyncIOMotorClient | |
| import os | |
| from dotenv import load_dotenv | |
| load_dotenv(os.path.join(os.path.dirname(__file__), '..', '.env')) | |
| MONGO_URI = os.getenv("MONGO_URI", "mongodb://localhost:27017") | |
| DB_NAME = "janrakshak" | |
| async def seed(): | |
| client = AsyncIOMotorClient(MONGO_URI) | |
| db = client[DB_NAME] | |
| print("Clearing existing incidents for a clean demo state...") | |
| await db["incidents"].delete_many({}) | |
| # 1. Coordinate Centers for DBSCAN (Jamtara & Bharatpur) | |
| # Note: GeoJSON uses [longitude, latitude] | |
| clusters = { | |
| "Jamtara_Hub": [86.8000, 23.9667], | |
| "Bharatpur_Hub": [77.4930, 27.2152] | |
| } | |
| # 2. Key Entities for Agentic AI to find historical overlap | |
| scammer_phones = ["+91-9876543210", "+91-8765432109", "+91-7654321098"] | |
| safe_accounts = ["SBI-987654321", "HDFC-123456789"] | |
| incidents = [] | |
| print("Generating 100 heavily interconnected fraud incidents...") | |
| for i in range(100): | |
| # Pick a cluster 80% of the time, noise 20% | |
| if random.random() < 0.8: | |
| hub = random.choice(list(clusters.values())) | |
| # Add small random offset for scatter (approx 1-5km) | |
| lng = hub[0] + random.uniform(-0.05, 0.05) | |
| lat = hub[1] + random.uniform(-0.05, 0.05) | |
| else: | |
| # Random noise across India | |
| lng = random.uniform(68.0, 97.0) | |
| lat = random.uniform(8.0, 37.0) | |
| # Select entities | |
| phone = random.choice(scammer_phones) | |
| bank = random.choice(safe_accounts) | |
| incident = { | |
| "incident_id": str(uuid.uuid4()), | |
| "type": "audio", | |
| "timestamp": datetime.utcnow() - timedelta(days=random.randint(0, 30)), | |
| "location": { | |
| "type": "Point", | |
| "coordinates": [lng, lat] | |
| }, | |
| "extracted_entities": [ | |
| {"text": phone, "label": "phone_number"}, | |
| {"text": bank, "label": "bank_account"} | |
| ], | |
| "risk_score": round(random.uniform(0.7, 0.99), 2), | |
| "threat_dossier": "Legacy historical record.", | |
| "details": { | |
| "transcription": f"Hello, this is customs. Transfer to {bank} immediately or face arrest.", | |
| "intent": "Extortion", | |
| "scam_stage": "Stage 5: Extortion", | |
| "deepfake_probability": round(random.uniform(0.0, 1.0), 2) | |
| }, | |
| "digital_signature": "SEED_DATA_SIGNATURE" | |
| } | |
| incidents.append(incident) | |
| await db["incidents"].insert_many(incidents) | |
| # Ensure geospatial index exists | |
| await db["incidents"].create_index([("location", "2dsphere")]) | |
| print(f"Successfully seeded {len(incidents)} incidents.") | |
| print("Agentic AI will now successfully find historical patterns for phones:", scammer_phones) | |
| print("DBSCAN will now flag giant red hotspots over Jamtara and Bharatpur.") | |
| if __name__ == "__main__": | |
| asyncio.run(seed()) | |