Skip to content

Commit f4d126f

Browse files
authored
Merge pull request #4 from ParagGhatage/pr-3
Pr 3
2 parents 392705b + 09a6321 commit f4d126f

6 files changed

Lines changed: 76 additions & 15 deletions

File tree

backend/app/__

Whitespace-only changes.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import os
2+
import tempfile
3+
import json
4+
import requests
5+
import pandas as pd
6+
from fastapi import APIRouter, HTTPException, Query
7+
from dotenv import load_dotenv
8+
9+
# Load .env file (make sure you have HF_TOKEN inside it)
10+
load_dotenv()
11+
12+
router = APIRouter()
13+
14+
HF_REPO_ID = os.getenv("HF_DATA_REPO") # your repo ID
15+
HF_TOKEN = os.getenv("HF_TOKEN") # Hugging Face API token
16+
BASE_URL = f"https://huggingface.co/datasets/{HF_REPO_ID}/resolve/main/uploads"
17+
18+
19+
@router.get("/get-file")
20+
async def get_file(filename: str = Query(..., description="Filename to extract from Hugging Face dataset")):
21+
try:
22+
file_url = f"{BASE_URL}/{filename}.csv"
23+
24+
headers = {}
25+
if HF_TOKEN:
26+
headers["Authorization"] = f"Bearer {HF_TOKEN}"
27+
28+
response = requests.get(file_url, headers=headers)
29+
30+
if response.status_code != 200:
31+
raise HTTPException(status_code=404, detail=f"File not found at {file_url}")
32+
33+
# Save temporarily
34+
with tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as tmp:
35+
tmp.write(response.content)
36+
tmp_path = tmp.name
37+
38+
# Load with pandas
39+
df = pd.read_csv(tmp_path)
40+
41+
# Clean data for JSON
42+
df = df.replace([float("inf"), float("-inf")], None)
43+
df = df.where(pd.notnull(df), None)
44+
45+
json_str = df.head(50).to_json(orient="records", force_ascii=False, default_handler=str)
46+
records = json.loads(json_str)
47+
48+
return {"filename": filename, "records": records}
49+
50+
except Exception as e:
51+
raise HTTPException(status_code=500, detail=f"Error processing file: {str(e)}")

backend/app/components/Upload/data_upload.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
from fastapi import HTTPException
77
import pandas as pd
88
import pyreadstat
9-
from requests.exceptions import HTTPError
9+
from huggingface_hub.utils import HfHubHTTPError as HTTPError
1010

1111
load_dotenv()
1212

1313
HF_TOKEN = os.getenv("HF_TOKEN")
1414

15-
HF_DATA_REPO = "Thunder1245/Zero-ML-dataset-2"
15+
HF_DATA_REPO = "prthm20/ZeoMl"
1616

1717
api = HfApi()
1818

backend/app/routes/routes.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,14 @@ async def upload_file(file: UploadFile = File(...)):
3636
raise HTTPException(status_code=500, detail=str(e))
3737

3838

39+
@router.get("/get-file")
40+
async def get_file(filename: str):
41+
from app.components.ExtractFile.extract import get_file as extract_file
42+
try:
43+
res = await extract_file(filename)
44+
45+
return res
3946

47+
except Exception as e:
48+
print(e)
49+
raise HTTPException(status_code=500, detail=str(e))

backend/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ dependencies = [
1717
"lxml>=6.0.1",
1818
"html5lib>=1.1",
1919
"beautifulsoup4>=4.13.5",
20-
]
20+
]

backend/uv.lock

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)