Spaces:
Sleeping

digiintel commited on
Commit
3560d2a
·
verified ·
1 Parent(s): 56a260f

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +20 -0
  2. package.json +25 -0
  3. server.js +115 -0
Dockerfile ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM node:18-bullseye
2
+
3
+ # Create app directory
4
+ WORKDIR /app
5
+
6
+ # Install app dependencies
7
+ COPY package*.json ./
8
+ RUN npm install
9
+
10
+ # Bundle app source
11
+ COPY . .
12
+
13
+ # Expose port 7860 (Hugging Face Spaces requirement)
14
+ EXPOSE 7860
15
+
16
+ # Set environment variables
17
+ ENV PORT=7860
18
+
19
+ # Run the search API server
20
+ CMD [ "node", "server.js" ]
package.json ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "hi-tek-db",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "start": "node server.js",
8
+ "ingest": "node ingest.js",
9
+ "test": "echo \"Error: no test specified\" && exit 1"
10
+ },
11
+ "keywords": [],
12
+ "author": "",
13
+ "license": "ISC",
14
+ "type": "commonjs",
15
+ "dependencies": {
16
+ "axios": "^1.16.0",
17
+ "better-sqlite3": "^12.9.0",
18
+ "cors": "^2.8.6",
19
+ "crypto-js": "^4.2.0",
20
+ "csv-parser": "^3.2.0",
21
+ "dotenv": "^17.4.2",
22
+ "duckdb": "^1.4.4",
23
+ "express": "^5.2.1"
24
+ }
25
+ }
server.js ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const express = require('express');
2
+ const cors = require('cors');
3
+ const duckdb = require('duckdb');
4
+ const https = require('https');
5
+
6
+ const app = express();
7
+ app.use(cors());
8
+ const port = process.env.PORT || 3000;
9
+
10
+ // Tokens from environment variables for security
11
+ const HF_TOKEN = process.env.HF_TOKEN;
12
+ const HF_TOKEN_2 = process.env.HF_TOKEN_2 || HF_TOKEN; // Fallback to first token if not set
13
+ const db = new duckdb.Database(':memory:');
14
+
15
+ // In-Memory RAM Caching for Hugging Face S3 Links (30 minutes expiry) to maximize speed!
16
+ const linkCache = new Map();
17
+ const CACHE_TTL = 30 * 60 * 1000;
18
+
19
+ async function getDirectLink(url, token = HF_TOKEN) {
20
+ const cached = linkCache.get(url);
21
+ if (cached && (Date.now() - cached.timestamp < CACHE_TTL)) {
22
+ return cached.directLink;
23
+ }
24
+
25
+ return new Promise((resolve, reject) => {
26
+ const options = { headers: { 'Authorization': `Bearer ${token}` }, method: 'HEAD' };
27
+ const req = https.request(url, options, (res) => {
28
+ if (res.statusCode === 302 || res.statusCode === 301) {
29
+ const directLink = res.headers.location;
30
+ linkCache.set(url, { directLink, timestamp: Date.now() });
31
+ resolve(directLink);
32
+ } else {
33
+ reject(new Error(`Hugging Face error: ${res.statusCode}`));
34
+ }
35
+ });
36
+ req.on('error', reject);
37
+ req.end();
38
+ });
39
+ }
40
+
41
+ // Database Initialization
42
+ async function initDB() {
43
+ console.log("Initializing Cloud Search Engine (RAM Limit: 256MB)...");
44
+ return new Promise((resolve, reject) => {
45
+ db.run("INSTALL httpfs; LOAD httpfs;", (err) => {
46
+ if (err) return reject(err);
47
+
48
+ // CRITICAL: Limit memory usage for Render Free Tier (512MB total)
49
+ db.run("PRAGMA max_memory='256MB';");
50
+ db.run("PRAGMA memory_limit='256MB';");
51
+ resolve();
52
+ });
53
+ });
54
+ }
55
+
56
+ const BASE_URL_1 = process.env.BASE_URL_1 || 'https://huggingface.co/datasets/digiintel/digiintell/resolve/main';
57
+ const BASE_URL_2 = process.env.BASE_URL_2 || 'https://huggingface.co/datasets/suraj14921/digiintel2/resolve/main';
58
+
59
+ const MOBILE_FILES = [
60
+ ...Array.from({length: 10}, (_, i) => ({ url: `${BASE_URL_1}/hitek_mobile_part${i+1}.parquet`, token: HF_TOKEN })), // Batch 1 Mobile in Repo 1
61
+ ...Array.from({length: 10}, (_, i) => ({ url: `${BASE_URL_2}/hitek2_mobile_part${i+1}.parquet`, token: HF_TOKEN_2 })) // Batch 2 Mobile in Repo 2
62
+ ];
63
+
64
+ const ID_FILES = [
65
+ ...Array.from({length: 10}, (_, i) => ({ url: `${BASE_URL_1}/hitek_id_part${i+1}.parquet`, token: HF_TOKEN })), // Batch 1 ID in Repo 1
66
+ ...Array.from({length: 10}, (_, i) => ({ url: `${BASE_URL_1}/hitek2_id_part${i+1}.parquet`, token: HF_TOKEN })) // Batch 2 ID in Repo 1
67
+ ];
68
+
69
+ // MOBILE SEARCH API
70
+ app.get('/search/mobile/:number', async (req, res) => {
71
+ const number = req.params.number;
72
+ console.log(`Mobile Search Request: ${number}`);
73
+
74
+ try {
75
+ const directLinks = await Promise.all(MOBILE_FILES.map(f => getDirectLink(f.url, f.token)));
76
+ // Using 'mobile' column for number search
77
+ const sql = `SELECT * FROM read_parquet([${directLinks.map(f => `'${f}'`).join(',')}], union_by_name=True) WHERE mobile = '${number}' LIMIT 10;`;
78
+
79
+ db.all(sql, (err, rows) => {
80
+ if (err) return res.status(500).json({ error: err.message });
81
+ res.json({ results: rows, count: rows.length });
82
+ });
83
+ } catch (e) {
84
+ res.status(500).json({ error: e.message });
85
+ }
86
+ });
87
+
88
+ // ID / AADHAR SEARCH API
89
+ app.get('/search/id/:id', async (req, res) => {
90
+ const id = req.params.id;
91
+ console.log(`Deep Aadhar Search Request: ${id}`);
92
+
93
+ try {
94
+ // FAST SEARCH: Only look in ID_FILES for Aadhar searches
95
+ const directLinks = await Promise.all(ID_FILES.map(f => getDirectLink(f.url, f.token)));
96
+
97
+ // Use 'id' column as confirmed by the database error message
98
+ const sql = `SELECT * FROM read_parquet([${directLinks.map(f => `'${f}'`).join(',')}], union_by_name=True)
99
+ WHERE id = '${id}'
100
+ LIMIT 20;`;
101
+
102
+ db.all(sql, (err, rows) => {
103
+ if (err) return res.status(500).json({ error: err.message });
104
+ res.json({ results: rows, count: rows.length });
105
+ });
106
+ } catch (e) {
107
+ res.status(500).json({ error: e.message });
108
+ }
109
+ });
110
+
111
+ app.get('/', (req, res) => res.send('Hi-Tek OSINT API is ONLINE 🚀'));
112
+
113
+ initDB().then(() => {
114
+ app.listen(port, () => console.log(`🚀 API Server running at port ${port}`));
115
+ }).catch(console.error);