Spaces:
Running
Running
Update autorun_lora_gradio.py
Browse files- autorun_lora_gradio.py +271 -0
autorun_lora_gradio.py
CHANGED
|
@@ -10,6 +10,277 @@ from fastapi import FastAPI
|
|
| 10 |
from fastapi.responses import JSONResponse
|
| 11 |
from huggingface_hub import hf_hub_download, whoami
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# ========== CONFIGURATION ==========
|
| 14 |
REPO_ID = "rahul7star/ohamlab"
|
| 15 |
FOLDER_IN_REPO = "filter-demo/upload_20250708_041329_9c5c81"
|
|
|
|
| 10 |
from fastapi.responses import JSONResponse
|
| 11 |
from huggingface_hub import hf_hub_download, whoami
|
| 12 |
|
| 13 |
+
|
| 14 |
+
import os
|
| 15 |
+
os.environ["HF_HOME"] = "/tmp/hf_cache"
|
| 16 |
+
os.makedirs("/tmp/hf_cache", exist_ok=True)
|
| 17 |
+
|
| 18 |
+
from fastapi import FastAPI, Query
|
| 19 |
+
from huggingface_hub import list_repo_files, hf_hub_download, upload_file
|
| 20 |
+
import io
|
| 21 |
+
import requests
|
| 22 |
+
from fastapi import BackgroundTasks
|
| 23 |
+
from fastapi import FastAPI, UploadFile, File
|
| 24 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
import os
|
| 28 |
+
import os
|
| 29 |
+
import zipfile
|
| 30 |
+
import tempfile # ✅ Add this!
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
app = FastAPI()
|
| 36 |
+
|
| 37 |
+
# CORS setup to allow requests from your frontend
|
| 38 |
+
app.add_middleware(
|
| 39 |
+
CORSMiddleware,
|
| 40 |
+
allow_origins=["*"], # Replace "*" with your frontend domain in production
|
| 41 |
+
allow_credentials=True,
|
| 42 |
+
allow_methods=["*"],
|
| 43 |
+
allow_headers=["*"],
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
@app.get("/")
|
| 47 |
+
def health_check():
|
| 48 |
+
return {"status": "✅ FastAPI running on Hugging Face Spaces!"}
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
REPO_ID = "rahul7star/ohamlab"
|
| 53 |
+
FOLDER = "demo"
|
| 54 |
+
BASE_URL = f"https://huggingface.co/{REPO_ID}/resolve/main/"
|
| 55 |
+
|
| 56 |
+
#show all images in a DIR at UI FE
|
| 57 |
+
@app.get("/images")
|
| 58 |
+
def list_images():
|
| 59 |
+
try:
|
| 60 |
+
all_files = list_repo_files(REPO_ID)
|
| 61 |
+
|
| 62 |
+
folder_prefix = FOLDER.rstrip("/") + "/"
|
| 63 |
+
|
| 64 |
+
files_in_folder = [
|
| 65 |
+
f for f in all_files
|
| 66 |
+
if f.startswith(folder_prefix)
|
| 67 |
+
and "/" not in f[len(folder_prefix):] # no subfolder files
|
| 68 |
+
and f.lower().endswith((".png", ".jpg", ".jpeg", ".webp"))
|
| 69 |
+
]
|
| 70 |
+
|
| 71 |
+
urls = [BASE_URL + f for f in files_in_folder]
|
| 72 |
+
|
| 73 |
+
return {"images": urls}
|
| 74 |
+
|
| 75 |
+
except Exception as e:
|
| 76 |
+
return {"error": str(e)}
|
| 77 |
+
|
| 78 |
+
from datetime import datetime
|
| 79 |
+
import tempfile
|
| 80 |
+
import uuid
|
| 81 |
+
|
| 82 |
+
# upload zip from UI
|
| 83 |
+
@app.post("/upload-zip")
|
| 84 |
+
async def upload_zip(file: UploadFile = File(...)):
|
| 85 |
+
if not file.filename.endswith(".zip"):
|
| 86 |
+
return {"error": "Please upload a .zip file"}
|
| 87 |
+
|
| 88 |
+
# Save the ZIP to /tmp
|
| 89 |
+
temp_zip_path = f"/tmp/{file.filename}"
|
| 90 |
+
with open(temp_zip_path, "wb") as f:
|
| 91 |
+
f.write(await file.read())
|
| 92 |
+
|
| 93 |
+
# Create a unique subfolder name inside 'demo/'
|
| 94 |
+
timestamp = datetime.utcnow().strftime("%Y%m%d_%H%M%S")
|
| 95 |
+
unique_id = uuid.uuid4().hex[:6]
|
| 96 |
+
folder_name = f"upload_{timestamp}_{unique_id}"
|
| 97 |
+
hf_folder_prefix = f"demo/{folder_name}"
|
| 98 |
+
|
| 99 |
+
try:
|
| 100 |
+
with tempfile.TemporaryDirectory() as extract_dir:
|
| 101 |
+
# Extract zip
|
| 102 |
+
with zipfile.ZipFile(temp_zip_path, 'r') as zip_ref:
|
| 103 |
+
zip_ref.extractall(extract_dir)
|
| 104 |
+
|
| 105 |
+
uploaded_files = []
|
| 106 |
+
|
| 107 |
+
# Upload all extracted files
|
| 108 |
+
for root_dir, _, files in os.walk(extract_dir):
|
| 109 |
+
for name in files:
|
| 110 |
+
file_path = os.path.join(root_dir, name)
|
| 111 |
+
relative_path = os.path.relpath(file_path, extract_dir)
|
| 112 |
+
repo_path = f"{hf_folder_prefix}/{relative_path}".replace("\\", "/")
|
| 113 |
+
|
| 114 |
+
upload_file(
|
| 115 |
+
path_or_fileobj=file_path,
|
| 116 |
+
path_in_repo=repo_path,
|
| 117 |
+
repo_id="rahul7star/ohamlab",
|
| 118 |
+
repo_type="model",
|
| 119 |
+
commit_message=f"Upload {relative_path} to {folder_name}",
|
| 120 |
+
token=True,
|
| 121 |
+
)
|
| 122 |
+
uploaded_files.append(repo_path)
|
| 123 |
+
|
| 124 |
+
return {
|
| 125 |
+
"message": f"✅ Uploaded {len(uploaded_files)} files",
|
| 126 |
+
"folder": folder_name,
|
| 127 |
+
"files": uploaded_files,
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
except Exception as e:
|
| 131 |
+
return {"error": f"❌ Failed to process zip: {str(e)}"}
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
# upload a single file from UI
|
| 135 |
+
from typing import List
|
| 136 |
+
from fastapi import UploadFile, File, APIRouter
|
| 137 |
+
import os
|
| 138 |
+
from fastapi import UploadFile, File, APIRouter
|
| 139 |
+
from typing import List
|
| 140 |
+
from datetime import datetime
|
| 141 |
+
import uuid, os
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
@app.post("/upload")
|
| 145 |
+
async def upload_images(
|
| 146 |
+
background_tasks: BackgroundTasks,
|
| 147 |
+
files: List[UploadFile] = File(...)
|
| 148 |
+
):
|
| 149 |
+
# Step 1: Generate dynamic folder name
|
| 150 |
+
timestamp = datetime.utcnow().strftime("%Y%m%d_%H%M%S")
|
| 151 |
+
unique_id = uuid.uuid4().hex[:6]
|
| 152 |
+
folder_name = f"upload_{timestamp}_{unique_id}"
|
| 153 |
+
hf_folder_prefix = f"demo/{folder_name}"
|
| 154 |
+
|
| 155 |
+
responses = []
|
| 156 |
+
|
| 157 |
+
# Step 2: Save and upload each image
|
| 158 |
+
for file in files:
|
| 159 |
+
filename = file.filename
|
| 160 |
+
contents = await file.read()
|
| 161 |
+
temp_path = f"/tmp/{filename}"
|
| 162 |
+
with open(temp_path, "wb") as f:
|
| 163 |
+
f.write(contents)
|
| 164 |
+
|
| 165 |
+
try:
|
| 166 |
+
upload_file(
|
| 167 |
+
path_or_fileobj=temp_path,
|
| 168 |
+
path_in_repo=f"{hf_folder_prefix}/{filename}",
|
| 169 |
+
repo_id=T_REPO_ID,
|
| 170 |
+
repo_type="model",
|
| 171 |
+
commit_message=f"Upload {filename} to {hf_folder_prefix}",
|
| 172 |
+
token=True,
|
| 173 |
+
)
|
| 174 |
+
responses.append({
|
| 175 |
+
"filename": filename,
|
| 176 |
+
"status": "✅ uploaded",
|
| 177 |
+
"path": f"{hf_folder_prefix}/{filename}"
|
| 178 |
+
})
|
| 179 |
+
except Exception as e:
|
| 180 |
+
responses.append({
|
| 181 |
+
"filename": filename,
|
| 182 |
+
"status": f"❌ failed: {str(e)}"
|
| 183 |
+
})
|
| 184 |
+
|
| 185 |
+
os.remove(temp_path)
|
| 186 |
+
|
| 187 |
+
# Step 3: Add filter job to background
|
| 188 |
+
def run_filter():
|
| 189 |
+
try:
|
| 190 |
+
result = filter_and_rename_images(folder=hf_folder_prefix)
|
| 191 |
+
print(f"🧼 Filter result: {result}")
|
| 192 |
+
except Exception as e:
|
| 193 |
+
print(f"❌ Filter failed: {str(e)}")
|
| 194 |
+
|
| 195 |
+
background_tasks.add_task(run_filter)
|
| 196 |
+
|
| 197 |
+
return {
|
| 198 |
+
"message": f"{len(files)} file(s) uploaded",
|
| 199 |
+
"upload_folder": hf_folder_prefix,
|
| 200 |
+
"results": responses,
|
| 201 |
+
"note": "Filtering started in background"
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
#Tranining Data set start fitering data for traninig
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
T_REPO_ID = "rahul7star/ohamlab"
|
| 213 |
+
DESCRIPTION_TEXT = (
|
| 214 |
+
"Ra3hul is wearing a black jacket over a striped white t-shirt with blue jeans. "
|
| 215 |
+
"He is standing near a lake with his arms spread wide open, with mountains and cloudy skies in the background."
|
| 216 |
+
)
|
| 217 |
+
|
| 218 |
+
def is_image_file(filename: str) -> bool:
|
| 219 |
+
return filename.lower().endswith((".png", ".jpg", ".jpeg", ".webp"))
|
| 220 |
+
|
| 221 |
+
@app.post("/filter-images")
|
| 222 |
+
def filter_and_rename_images(folder: str = Query("demo", description="Folder path in repo to scan")):
|
| 223 |
+
try:
|
| 224 |
+
all_files = list_repo_files(T_REPO_ID)
|
| 225 |
+
folder_prefix = folder.rstrip("/") + "/"
|
| 226 |
+
filter_folder = f"filter-{folder.rstrip('/')}"
|
| 227 |
+
filter_prefix = filter_folder + "/"
|
| 228 |
+
|
| 229 |
+
# Filter images only directly in the folder (no subfolders)
|
| 230 |
+
image_files = [
|
| 231 |
+
f for f in all_files
|
| 232 |
+
if f.startswith(folder_prefix)
|
| 233 |
+
and "/" not in f[len(folder_prefix):] # no deeper path
|
| 234 |
+
and is_image_file(f)
|
| 235 |
+
]
|
| 236 |
+
|
| 237 |
+
if not image_files:
|
| 238 |
+
return {"error": f"No images found in folder '{folder}'"}
|
| 239 |
+
|
| 240 |
+
uploaded_files = []
|
| 241 |
+
|
| 242 |
+
for idx, orig_path in enumerate(image_files, start=1):
|
| 243 |
+
# Download image content bytes (uses local cache)
|
| 244 |
+
local_path = hf_hub_download(repo_id=T_REPO_ID, filename=orig_path)
|
| 245 |
+
with open(local_path, "rb") as f:
|
| 246 |
+
file_bytes = f.read()
|
| 247 |
+
|
| 248 |
+
# Rename images as image1.jpeg, image2.jpeg, ...
|
| 249 |
+
new_image_name = f"image{idx}.jpeg"
|
| 250 |
+
|
| 251 |
+
# Upload renamed image from memory
|
| 252 |
+
upload_file(
|
| 253 |
+
path_or_fileobj=io.BytesIO(file_bytes),
|
| 254 |
+
path_in_repo=filter_prefix + new_image_name,
|
| 255 |
+
repo_id=T_REPO_ID,
|
| 256 |
+
repo_type="model",
|
| 257 |
+
commit_message=f"Upload renamed image {new_image_name} to {filter_folder}",
|
| 258 |
+
token=True,
|
| 259 |
+
)
|
| 260 |
+
uploaded_files.append(filter_prefix + new_image_name)
|
| 261 |
+
|
| 262 |
+
# Create and upload text file for each image
|
| 263 |
+
txt_filename = f"image{idx}.txt"
|
| 264 |
+
upload_file(
|
| 265 |
+
path_or_fileobj=io.BytesIO(DESCRIPTION_TEXT.encode("utf-8")),
|
| 266 |
+
path_in_repo=filter_prefix + txt_filename,
|
| 267 |
+
repo_id=T_REPO_ID,
|
| 268 |
+
repo_type="model",
|
| 269 |
+
commit_message=f"Upload text file {txt_filename} to {filter_folder}",
|
| 270 |
+
token=True,
|
| 271 |
+
)
|
| 272 |
+
uploaded_files.append(filter_prefix + txt_filename)
|
| 273 |
+
|
| 274 |
+
return {
|
| 275 |
+
"message": f"Processed and uploaded {len(image_files)} images and text files.",
|
| 276 |
+
"files": uploaded_files,
|
| 277 |
+
}
|
| 278 |
+
|
| 279 |
+
except Exception as e:
|
| 280 |
+
return {"error": str(e)}
|
| 281 |
+
|
| 282 |
+
|
| 283 |
+
|
| 284 |
# ========== CONFIGURATION ==========
|
| 285 |
REPO_ID = "rahul7star/ohamlab"
|
| 286 |
FOLDER_IN_REPO = "filter-demo/upload_20250708_041329_9c5c81"
|