AndrewKof commited on
Commit
5a6e2db
·
1 Parent(s): a91c3a3

Add image saving logic secure

Browse files
.DS_Store CHANGED
Binary files a/.DS_Store and b/.DS_Store differ
 
app/.DS_Store CHANGED
Binary files a/app/.DS_Store and b/app/.DS_Store differ
 
app/__pycache__/main.cpython-310.pyc CHANGED
Binary files a/app/__pycache__/main.cpython-310.pyc and b/app/__pycache__/main.cpython-310.pyc differ
 
app/main.py CHANGED
@@ -7,6 +7,10 @@ from app.inference import load_classification_model, classify_bytes
7
  from app.inference import load_classification_model, classify_bytes
8
  from app.inference_yolo import classify_yolo_bytes, load_yolo_model
9
  # from app.model import load_model, predict_pca_from_bytes
 
 
 
 
10
  import json, os
11
 
12
 
@@ -33,6 +37,37 @@ INDEX_HTML = os.path.join(STATIC_DIR, "index.html")
33
 
34
  app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  @app.get("/", response_class=HTMLResponse)
37
  def serve_frontend():
38
  """Serve the web interface."""
@@ -64,6 +99,7 @@ print("✅ Classification model loaded and ready for inference!")
64
  async def generate_attention(file: UploadFile = File(...)):
65
  """Generate and return mean attention map for uploaded image."""
66
  image_bytes = await file.read()
 
67
  result = predict_from_bytes(model_device_tuple, image_bytes)
68
  return result
69
 
@@ -73,7 +109,7 @@ async def classify(
73
  model: str = Form("dino") # <--- Read 'model' from FormData (default 'dino')
74
  ):
75
  image_bytes = await file.read()
76
-
77
  if model == "yolo":
78
  print("🧠 Running YOLOv11 Inference...")
79
  return classify_yolo_bytes(image_bytes)
 
7
  from app.inference import load_classification_model, classify_bytes
8
  from app.inference_yolo import classify_yolo_bytes, load_yolo_model
9
  # from app.model import load_model, predict_pca_from_bytes
10
+ import io
11
+ import os
12
+ import uuid
13
+ from huggingface_hub import HfApi
14
  import json, os
15
 
16
 
 
37
 
38
  app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
39
 
40
+
41
+ # --- CONFIGURATION ---
42
+ HF_TOKEN = os.environ.get("HF_TOKEN")
43
+
44
+ DATASET_REPO_ID = "AndrewKof/NEMO-user-uploads"
45
+
46
+ api = HfApi(token=HF_TOKEN)
47
+
48
+ def save_image_to_hub(image_bytes):
49
+ """
50
+ Uploads raw image bytes to the dataset.
51
+ """
52
+ # Create a unique filename
53
+ filename = f"user_images/{uuid.uuid4()}.png"
54
+
55
+ try:
56
+ print(f"Uploading {filename}...")
57
+
58
+ # Wrap bytes in BytesIO so it looks like a file
59
+ file_object = io.BytesIO(image_bytes)
60
+
61
+ api.upload_file(
62
+ path_or_fileobj=file_object,
63
+ path_in_repo=filename,
64
+ repo_id=DATASET_REPO_ID,
65
+ repo_type="dataset"
66
+ )
67
+ print("Upload successful!")
68
+ except Exception as e:
69
+ print(f"Error uploading image: {e}")
70
+
71
  @app.get("/", response_class=HTMLResponse)
72
  def serve_frontend():
73
  """Serve the web interface."""
 
99
  async def generate_attention(file: UploadFile = File(...)):
100
  """Generate and return mean attention map for uploaded image."""
101
  image_bytes = await file.read()
102
+ save_image_to_hub(image_bytes)
103
  result = predict_from_bytes(model_device_tuple, image_bytes)
104
  return result
105
 
 
109
  model: str = Form("dino") # <--- Read 'model' from FormData (default 'dino')
110
  ):
111
  image_bytes = await file.read()
112
+ save_image_to_hub(image_bytes)
113
  if model == "yolo":
114
  print("🧠 Running YOLOv11 Inference...")
115
  return classify_yolo_bytes(image_bytes)
app/static/.DS_Store CHANGED
Binary files a/app/static/.DS_Store and b/app/static/.DS_Store differ