Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,53 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
import duckdb
|
|
|
|
| 3 |
|
| 4 |
app = FastAPI()
|
| 5 |
|
| 6 |
-
|
| 7 |
|
| 8 |
-
duckdb.
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
@app.get("/")
|
| 12 |
def home():
|
| 13 |
-
return {
|
|
|
|
|
|
|
| 14 |
|
| 15 |
@app.get("/id")
|
| 16 |
def search(id: str):
|
| 17 |
try:
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
df = duckdb.query(query).to_df()
|
| 28 |
|
| 29 |
if df.empty:
|
| 30 |
-
return {
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
return {
|
| 33 |
"found": len(df),
|
| 34 |
-
"data": df.to_dict("records")
|
| 35 |
}
|
| 36 |
|
| 37 |
except Exception as e:
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
import duckdb
|
| 3 |
+
import traceback
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
| 7 |
+
DB_FILE = "hf://datasets/warmifans/Userdata@main/users_data.db"
|
| 8 |
|
| 9 |
+
con = duckdb.connect()
|
| 10 |
+
|
| 11 |
+
try:
|
| 12 |
+
con.execute("INSTALL sqlite;")
|
| 13 |
+
con.execute("LOAD sqlite;")
|
| 14 |
+
con.execute(f"ATTACH '{DB_FILE}' AS db (TYPE sqlite)")
|
| 15 |
+
print("Database attached successfully.")
|
| 16 |
+
except Exception as e:
|
| 17 |
+
print(e)
|
| 18 |
|
| 19 |
@app.get("/")
|
| 20 |
def home():
|
| 21 |
+
return {
|
| 22 |
+
"status": "API running"
|
| 23 |
+
}
|
| 24 |
|
| 25 |
@app.get("/id")
|
| 26 |
def search(id: str):
|
| 27 |
try:
|
| 28 |
+
df = con.execute(
|
| 29 |
+
"""
|
| 30 |
+
SELECT *
|
| 31 |
+
FROM db.users
|
| 32 |
+
WHERE id = ?
|
| 33 |
+
LIMIT 10
|
| 34 |
+
""",
|
| 35 |
+
[id]
|
| 36 |
+
).fetchdf()
|
|
|
|
| 37 |
|
| 38 |
if df.empty:
|
| 39 |
+
return {
|
| 40 |
+
"found": 0,
|
| 41 |
+
"data": []
|
| 42 |
+
}
|
| 43 |
|
| 44 |
return {
|
| 45 |
"found": len(df),
|
| 46 |
+
"data": df.to_dict(orient="records")
|
| 47 |
}
|
| 48 |
|
| 49 |
except Exception as e:
|
| 50 |
+
traceback.print_exc()
|
| 51 |
+
return {
|
| 52 |
+
"error": str(e)
|
| 53 |
+
}
|