tfqdeadlo commited on
Commit
b893af5
·
verified ·
1 Parent(s): 3926fbd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException, Query
2
+ import duckdb
3
+
4
+ app = FastAPI(title="Fast Mobile Search API")
5
+
6
+ # Global DuckDB connection setup karo aur HTTPFS extension load karo remote files read karne ke liye
7
+ con = duckdb.connect()
8
+ con.execute("INSTALL httpfs; LOAD httpfs;")
9
+
10
+ # Tumhari Hugging Face Parquet file ka direct URL
11
+ PARQUET_URL = "https://huggingface.co/datasets/tfqdeadlo/Inddatainone/resolve/main/users_data.parquet"
12
+
13
+ @app.get("/")
14
+ def home():
15
+ return {"message": "API is running. Use /search?mobile=YOUR_NUMBER to find data."}
16
+
17
+ @app.get("/search")
18
+ async def search_mobile(mobile: str = Query(..., description="Enter the mobile number to search")):
19
+ try:
20
+ # Parameterized query use kar rahe hain taaki SQL injection na ho
21
+ # read_parquet() seedhe URL se data fetch karega bina download kiye
22
+ query = f"""
23
+ SELECT mobile, name, fname, address, alt, circle, id, email
24
+ FROM read_parquet('{PARQUET_URL}')
25
+ WHERE mobile = ?
26
+ LIMIT 1
27
+ """
28
+
29
+ # Query execute karo
30
+ result = con.execute(query, [str(mobile)]).fetchall()
31
+
32
+ if not result:
33
+ return {
34
+ "status": "error",
35
+ "message": f"Mobile number {mobile} nahi mila."
36
+ }
37
+
38
+ # Column names nikalne ke liye desc ka use karenge
39
+ columns = [desc[0] for desc in con.description]
40
+ row_dict = dict(zip(columns, result[0]))
41
+
42
+ return {
43
+ "status": "success",
44
+ "data": row_dict
45
+ }
46
+
47
+ except Exception as e:
48
+ raise HTTPException(status_code=500, detail=f"Kuch gadbad hui: {str(e)}")
49
+