digitalhub483 commited on
Commit
96de815
·
verified ·
1 Parent(s): 1ce5d71

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -43
app.py CHANGED
@@ -1,10 +1,12 @@
1
  import os
 
2
  from typing import List
3
  from fastapi import FastAPI, UploadFile, File, HTTPException
4
  from fastapi.responses import JSONResponse
5
  import dagshub
6
  from dagshub import upload_files
7
  from dagshub.data_engine import datasources
 
8
  # =====================================================
9
  # CONFIG
10
  # =====================================================
@@ -12,11 +14,12 @@ from dagshub.data_engine import datasources
12
  REPO_OWNER = "Rizwanali324"
13
  REPO_NAME = "my-first-repo"
14
  FULL_REPO = f"{REPO_OWNER}/{REPO_NAME}"
 
15
  REMOTE_FOLDER = "data"
16
- BRANCH = "main" # change if your repo uses another branch
17
 
18
  # =====================================================
19
- # AUTHENTICATION (CI SAFE)
20
  # =====================================================
21
 
22
  if "DAGSHUB_USER_TOKEN" not in os.environ:
@@ -24,7 +27,6 @@ if "DAGSHUB_USER_TOKEN" not in os.environ:
24
  "DAGSHUB_USER_TOKEN secret not found. Add it in Space Settings → Secrets."
25
  )
26
 
27
- # Initialize DagsHub client
28
  dagshub.init(
29
  repo_owner=REPO_OWNER,
30
  repo_name=REPO_NAME,
@@ -35,107 +37,109 @@ dagshub.init(
35
  # FASTAPI INIT
36
  # =====================================================
37
 
38
- app = FastAPI(title="DagsHub Image Uploader API")
 
 
 
 
39
 
40
  # =====================================================
41
- # UPLOAD ENDPOINT
42
  # =====================================================
43
 
44
  @app.post("/upload")
45
  async def upload_images(
46
- files: List[UploadFile] = File(
47
- ..., description="Upload one or more image files"
48
- )
49
  ):
50
- """
51
- Upload one or more images and push to DagsHub.
52
- Returns list of public URLs for uploaded files.
53
- """
54
  if not files:
55
  raise HTTPException(status_code=400, detail="No files uploaded")
56
 
57
- os.makedirs(REMOTE_FOLDER, exist_ok=True)
58
- uploaded_urls = []
59
 
60
  for file in files:
 
61
  if not file.content_type.startswith("image/"):
62
  raise HTTPException(
63
  status_code=400,
64
- detail=f"{file.filename} is not a valid image",
65
  )
66
 
67
  original_name = file.filename
68
- local_path = os.path.join(REMOTE_FOLDER, original_name)
69
 
70
- # Save locally
 
 
 
 
 
71
  try:
 
 
 
72
  with open(local_path, "wb") as f:
73
- f.write(await file.read())
 
74
  except Exception as e:
75
  raise HTTPException(
76
- status_code=500, detail=f"Failed to save {original_name}: {str(e)}"
 
77
  )
78
 
79
- # Upload to DagsHub
80
  try:
81
  upload_files(
82
  repo=FULL_REPO,
83
  local_path=local_path,
84
- remote_path=f"{REMOTE_FOLDER}/{original_name}"
85
  )
 
86
  except Exception as e:
87
  raise HTTPException(
88
  status_code=500,
89
- detail=f"Failed to upload {original_name} to DagsHub: {str(e)}"
90
  )
91
 
92
- # Generate public URL
93
  file_url = (
94
- f"https://dagshub.com/{REPO_OWNER}/{REPO_NAME}/raw/{BRANCH}/"
95
- f"{REMOTE_FOLDER}/{original_name}"
96
  )
97
- uploaded_urls.append(file_url)
 
 
 
 
 
98
 
99
  return JSONResponse(
100
  content={
101
- "message": "Upload successful",
102
- "count": len(uploaded_urls),
103
- "files": uploaded_urls,
104
  }
105
  )
106
 
107
 
108
-
109
  # =====================================================
110
- # DATA FETCH ENDPOINT
111
  # =====================================================
112
 
113
  @app.get("/data")
114
  def get_uploaded_data(min_size: int = None):
115
- """
116
- Fetch uploaded data from DagsHub Data Engine.
117
- Optional query param:
118
- - min_size: return files larger than this size (in bytes)
119
- """
120
 
121
  try:
122
- # Connect to datasource
123
  ds = datasources.get_datasource(FULL_REPO)
124
 
125
- # Base query
126
  query = ds
127
 
128
- # Apply size filter if provided
129
  if min_size:
130
  query = ds["size"] > min_size
131
 
132
- # Select only useful columns
133
  query = query.select("path", "size")
134
 
135
- # Execute query
136
  df = query.all().dataframe
137
 
138
- # Convert to list of dicts
139
  records = df.to_dict(orient="records")
140
 
141
  return {
@@ -147,13 +151,15 @@ def get_uploaded_data(min_size: int = None):
147
  except Exception as e:
148
  raise HTTPException(status_code=500, detail=str(e))
149
 
 
150
  # =====================================================
151
  # HEALTH CHECK
152
  # =====================================================
153
 
154
  @app.get("/")
155
  def root():
156
- return {"status": "API is running"}
 
157
 
158
  # =====================================================
159
  # RUN SERVER
 
1
  import os
2
+ import uuid
3
  from typing import List
4
  from fastapi import FastAPI, UploadFile, File, HTTPException
5
  from fastapi.responses import JSONResponse
6
  import dagshub
7
  from dagshub import upload_files
8
  from dagshub.data_engine import datasources
9
+
10
  # =====================================================
11
  # CONFIG
12
  # =====================================================
 
14
  REPO_OWNER = "Rizwanali324"
15
  REPO_NAME = "my-first-repo"
16
  FULL_REPO = f"{REPO_OWNER}/{REPO_NAME}"
17
+
18
  REMOTE_FOLDER = "data"
19
+ BRANCH = "main"
20
 
21
  # =====================================================
22
+ # AUTHENTICATION
23
  # =====================================================
24
 
25
  if "DAGSHUB_USER_TOKEN" not in os.environ:
 
27
  "DAGSHUB_USER_TOKEN secret not found. Add it in Space Settings → Secrets."
28
  )
29
 
 
30
  dagshub.init(
31
  repo_owner=REPO_OWNER,
32
  repo_name=REPO_NAME,
 
37
  # FASTAPI INIT
38
  # =====================================================
39
 
40
+ app = FastAPI(title="Wildlife Detection Image API")
41
+
42
+ # local temp folder
43
+ os.makedirs("temp_uploads", exist_ok=True)
44
+
45
 
46
  # =====================================================
47
+ # IMAGE UPLOAD ENDPOINT
48
  # =====================================================
49
 
50
  @app.post("/upload")
51
  async def upload_images(
52
+ files: List[UploadFile] = File(..., description="Upload image files")
 
 
53
  ):
54
+
 
 
 
55
  if not files:
56
  raise HTTPException(status_code=400, detail="No files uploaded")
57
 
58
+ uploaded_files = []
 
59
 
60
  for file in files:
61
+
62
  if not file.content_type.startswith("image/"):
63
  raise HTTPException(
64
  status_code=400,
65
+ detail=f"{file.filename} is not a valid image"
66
  )
67
 
68
  original_name = file.filename
 
69
 
70
+ # generate unique filename
71
+ unique_id = uuid.uuid4().hex
72
+ stored_name = f"{unique_id}_{original_name}"
73
+
74
+ local_path = os.path.join("temp_uploads", stored_name)
75
+
76
  try:
77
+ content = await file.read()
78
+
79
+ # save locally
80
  with open(local_path, "wb") as f:
81
+ f.write(content)
82
+
83
  except Exception as e:
84
  raise HTTPException(
85
+ status_code=500,
86
+ detail=f"Failed saving {original_name}: {str(e)}"
87
  )
88
 
89
+ # upload to dagshub
90
  try:
91
  upload_files(
92
  repo=FULL_REPO,
93
  local_path=local_path,
94
+ remote_path=f"{REMOTE_FOLDER}/{stored_name}"
95
  )
96
+
97
  except Exception as e:
98
  raise HTTPException(
99
  status_code=500,
100
+ detail=f"Upload failed for {original_name}: {str(e)}"
101
  )
102
 
103
+ # public url
104
  file_url = (
105
+ f"https://dagshub.com/{REPO_OWNER}/{REPO_NAME}/raw/"
106
+ f"{BRANCH}/{REMOTE_FOLDER}/{stored_name}"
107
  )
108
+
109
+ uploaded_files.append({
110
+ "original_filename": original_name,
111
+ "stored_filename": stored_name,
112
+ "url": file_url
113
+ })
114
 
115
  return JSONResponse(
116
  content={
117
+ "message": "Images uploaded successfully",
118
+ "count": len(uploaded_files),
119
+ "files": uploaded_files
120
  }
121
  )
122
 
123
 
 
124
  # =====================================================
125
+ # FETCH DATA FROM DAGSHUB
126
  # =====================================================
127
 
128
  @app.get("/data")
129
  def get_uploaded_data(min_size: int = None):
 
 
 
 
 
130
 
131
  try:
 
132
  ds = datasources.get_datasource(FULL_REPO)
133
 
 
134
  query = ds
135
 
 
136
  if min_size:
137
  query = ds["size"] > min_size
138
 
 
139
  query = query.select("path", "size")
140
 
 
141
  df = query.all().dataframe
142
 
 
143
  records = df.to_dict(orient="records")
144
 
145
  return {
 
151
  except Exception as e:
152
  raise HTTPException(status_code=500, detail=str(e))
153
 
154
+
155
  # =====================================================
156
  # HEALTH CHECK
157
  # =====================================================
158
 
159
  @app.get("/")
160
  def root():
161
+ return {"status": "API running successfully"}
162
+
163
 
164
  # =====================================================
165
  # RUN SERVER