Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import hashlib, zipfile, os, shutil
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
|
| 7 |
+
# Load the AI Captioner
|
| 8 |
+
print("Loading AI...")
|
| 9 |
+
captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
| 10 |
+
|
| 11 |
+
# --- CUSTOM TAXONOMY RULES ---
|
| 12 |
+
# You can edit the words below to match your specific project!
|
| 13 |
+
RULES = {
|
| 14 |
+
"snail": "cartoon-snail",
|
| 15 |
+
"shirt": "apparel-merch",
|
| 16 |
+
"graffiti": "street-art",
|
| 17 |
+
"train": "transit-model",
|
| 18 |
+
"font": "typography",
|
| 19 |
+
"leaf": "cannabis-asset"
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
def process_images(input_file):
|
| 23 |
+
if not input_file: return None, None
|
| 24 |
+
|
| 25 |
+
extract_path = "temp_images"
|
| 26 |
+
output_path = "renamed_images"
|
| 27 |
+
if os.path.exists(extract_path): shutil.rmtree(extract_path)
|
| 28 |
+
if os.path.exists(output_path): shutil.rmtree(output_path)
|
| 29 |
+
os.makedirs(output_path)
|
| 30 |
+
os.makedirs(extract_path)
|
| 31 |
+
|
| 32 |
+
# Unpack Zip or Single File
|
| 33 |
+
if input_file.name.lower().endswith('.zip'):
|
| 34 |
+
with zipfile.ZipFile(input_file.name, 'r') as zip_ref:
|
| 35 |
+
zip_ref.extractall(extract_path)
|
| 36 |
+
else:
|
| 37 |
+
shutil.copy(input_file.name, os.path.join(extract_path, os.path.basename(input_file.name)))
|
| 38 |
+
|
| 39 |
+
data_log = []
|
| 40 |
+
|
| 41 |
+
for filename in os.listdir(extract_path):
|
| 42 |
+
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.webp')):
|
| 43 |
+
img_path = os.path.join(extract_path, filename)
|
| 44 |
+
|
| 45 |
+
# 1. AI Captioning
|
| 46 |
+
result = captioner(img_path)
|
| 47 |
+
caption = result[0]['generated_text'].lower()
|
| 48 |
+
|
| 49 |
+
# 2. Apply Taxonomy Rules
|
| 50 |
+
label = "uncategorized"
|
| 51 |
+
for keyword, category in RULES.items():
|
| 52 |
+
if keyword in caption:
|
| 53 |
+
label = category
|
| 54 |
+
break
|
| 55 |
+
|
| 56 |
+
# If no rule matched, use the first word the AI said
|
| 57 |
+
if label == "uncategorized":
|
| 58 |
+
label = caption.split()[0]
|
| 59 |
+
|
| 60 |
+
# 3. Create Name: label + short_hash
|
| 61 |
+
short_hash = hashlib.md5(filename.encode()).hexdigest()[:6]
|
| 62 |
+
ext = os.path.splitext(filename)[1]
|
| 63 |
+
new_name = f"{label}-{short_hash}{ext}"
|
| 64 |
+
|
| 65 |
+
# 4. Save
|
| 66 |
+
shutil.copy(img_path, os.path.join(output_path, new_name))
|
| 67 |
+
data_log.append({"Original": filename, "Renamed": new_name, "AI_Caption": caption, "Final_Label": label})
|
| 68 |
+
|
| 69 |
+
# Create CSV and Final Zip
|
| 70 |
+
df = pd.DataFrame(data_log)
|
| 71 |
+
df.to_csv("audit_trail.csv", index=False)
|
| 72 |
+
|
| 73 |
+
final_zip = "renamed_assets.zip"
|
| 74 |
+
with zipfile.ZipFile(final_zip, 'w') as zipf:
|
| 75 |
+
for file in os.listdir(output_path):
|
| 76 |
+
zipf.write(os.path.join(output_path, file), file)
|
| 77 |
+
zipf.write("audit_trail.csv")
|
| 78 |
+
|
| 79 |
+
return df, final_zip
|
| 80 |
+
|
| 81 |
+
# UI Setup (Chrome & Orange Theme)
|
| 82 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange", neutral_hue="slate")) as demo:
|
| 83 |
+
gr.Markdown("# 📸 ScaleBreakers: IMG_Name_Breaker\n### Semantic AI Renamer & Taxonomy Tool")
|
| 84 |
+
|
| 85 |
+
with gr.Row():
|
| 86 |
+
file_input = gr.File(label="Upload Zip or Image")
|
| 87 |
+
|
| 88 |
+
run_btn = gr.Button("🚀 EXECUTE PIPELINE", variant="primary")
|
| 89 |
+
|
| 90 |
+
with gr.Row():
|
| 91 |
+
table_out = gr.Dataframe(label="Audit Trail / Preview")
|
| 92 |
+
file_out = gr.File(label="Download Renamed Package")
|
| 93 |
+
|
| 94 |
+
demo.launch()
|