Spaces:
Sleeping
Sleeping
en_US
Browse files
app.py
CHANGED
|
@@ -7,12 +7,12 @@ from PIL import Image
|
|
| 7 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 8 |
from huggingface_hub import snapshot_download
|
| 9 |
|
| 10 |
-
# ---
|
| 11 |
MODEL_STANDARD = "microsoft/Florence-2-base"
|
| 12 |
MODEL_NSFW = "ljnlonoljpiljm/florence-2-base-nsfw-v2"
|
| 13 |
|
| 14 |
def setup_model_cache():
|
| 15 |
-
"""
|
| 16 |
cache_dir = os.path.expanduser("~/.cache/huggingface/hub")
|
| 17 |
folder_standard = f"models--{MODEL_STANDARD.replace('/', '--')}"
|
| 18 |
folder_nsfw = f"models--{MODEL_NSFW.replace('/', '--')}"
|
|
@@ -22,45 +22,45 @@ def setup_model_cache():
|
|
| 22 |
print(f"🔍 ตรวจสอบ Cache: {folder_standard}")
|
| 23 |
|
| 24 |
if not os.path.exists(path_standard) or not os.listdir(path_standard):
|
| 25 |
-
print(f"⚠️
|
| 26 |
if os.path.exists(path_nsfw) and os.listdir(path_nsfw):
|
| 27 |
-
print(f"✅
|
| 28 |
source_path = path_nsfw
|
| 29 |
else:
|
| 30 |
-
print(f"🚀
|
| 31 |
try:
|
| 32 |
snapshot_download(
|
| 33 |
repo_id=MODEL_NSFW,
|
| 34 |
local_dir=path_nsfw,
|
| 35 |
local_dir_use_symlinks=False
|
| 36 |
)
|
| 37 |
-
print("✅
|
| 38 |
source_path = path_nsfw
|
| 39 |
except Exception as e:
|
| 40 |
-
print(f"❌
|
| 41 |
-
print("💡
|
| 42 |
return MODEL_STANDARD
|
| 43 |
|
| 44 |
if os.path.exists(path_standard):
|
| 45 |
shutil.rmtree(path_standard)
|
| 46 |
-
print(f"📂
|
| 47 |
shutil.copytree(source_path, path_standard)
|
| 48 |
-
print("✅
|
| 49 |
else:
|
| 50 |
-
print(f"✅
|
| 51 |
|
| 52 |
return MODEL_STANDARD
|
| 53 |
|
| 54 |
# --- เรียกฟังก์ชันจัดการ Cache ---
|
| 55 |
FINAL_MODEL_NAME = setup_model_cache()
|
| 56 |
-
print(f"🚀
|
| 57 |
|
| 58 |
# --- ติดตั้ง flash-attn (ถ้าจำเป็น) ---
|
| 59 |
try:
|
| 60 |
import flash_attn
|
| 61 |
-
print("✅ flash_attn
|
| 62 |
except ImportError:
|
| 63 |
-
print("⚠️ flash_attn
|
| 64 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
| 65 |
|
| 66 |
# --- โหลดโมเดล ---
|
|
@@ -77,7 +77,7 @@ try:
|
|
| 77 |
)
|
| 78 |
print("✅ โหลดโมเดล Florence-2 (NSFW Version) เสร็จสิ้น!")
|
| 79 |
except Exception as e:
|
| 80 |
-
print(f"❌
|
| 81 |
florence_model = None
|
| 82 |
florence_processor = None
|
| 83 |
|
|
@@ -102,9 +102,9 @@ TASK_PROMPTS = {
|
|
| 102 |
def process_image(image, task_name):
|
| 103 |
global florence_model, florence_processor
|
| 104 |
if florence_model is None or florence_processor is None:
|
| 105 |
-
return "❌
|
| 106 |
if image is None:
|
| 107 |
-
return "
|
| 108 |
try:
|
| 109 |
# แป���ง Gradio Image เป็น PIL Image
|
| 110 |
if not isinstance(image, Image.Image):
|
|
@@ -143,24 +143,24 @@ def process_image(image, task_name):
|
|
| 143 |
return result_text
|
| 144 |
|
| 145 |
except Exception as e:
|
| 146 |
-
return f"❌
|
| 147 |
|
| 148 |
# --- สร้าง UI ---
|
| 149 |
with gr.Blocks(title="Image-to-Prompt (Florence-2)", theme=gr.themes.Soft()) as demo:
|
| 150 |
gr.Markdown("""
|
| 151 |
# 🖼️ Image-to-Prompt (Florence-2)
|
| 152 |
-
|
| 153 |
""")
|
| 154 |
|
| 155 |
with gr.Row():
|
| 156 |
with gr.Column():
|
| 157 |
-
image_input = gr.Image(type="pil", label="
|
| 158 |
task_dropdown = gr.Dropdown(
|
| 159 |
choices=list(TASK_PROMPTS.keys()),
|
| 160 |
value="More Detailed Caption", # เลือกเป็นค่าเริ่มต้น
|
| 161 |
-
label="
|
| 162 |
)
|
| 163 |
-
btn = gr.Button("🚀
|
| 164 |
|
| 165 |
with gr.Column():
|
| 166 |
text_output = gr.Textbox(label="ผลลัพธ์", lines=10, max_lines=20, show_copy_button=True)
|
|
|
|
| 7 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 8 |
from huggingface_hub import snapshot_download
|
| 9 |
|
| 10 |
+
# --- Management Section Cache: Bring the NSFW model to the standard model. ---
|
| 11 |
MODEL_STANDARD = "microsoft/Florence-2-base"
|
| 12 |
MODEL_NSFW = "ljnlonoljpiljm/florence-2-base-nsfw-v2"
|
| 13 |
|
| 14 |
def setup_model_cache():
|
| 15 |
+
""" Download Model NSFW Let's place the folder of the standard model in Cache """
|
| 16 |
cache_dir = os.path.expanduser("~/.cache/huggingface/hub")
|
| 17 |
folder_standard = f"models--{MODEL_STANDARD.replace('/', '--')}"
|
| 18 |
folder_nsfw = f"models--{MODEL_NSFW.replace('/', '--')}"
|
|
|
|
| 22 |
print(f"🔍 ตรวจสอบ Cache: {folder_standard}")
|
| 23 |
|
| 24 |
if not os.path.exists(path_standard) or not os.listdir(path_standard):
|
| 25 |
+
print(f"⚠️ No standard models found in Cache or empty folders")
|
| 26 |
if os.path.exists(path_nsfw) and os.listdir(path_nsfw):
|
| 27 |
+
print(f"✅ NSFW model has been found in Cache.: {folder_nsfw}")
|
| 28 |
source_path = path_nsfw
|
| 29 |
else:
|
| 30 |
+
print(f"🚀 Downloading Model NSFW ({MODEL_NSFW})...")
|
| 31 |
try:
|
| 32 |
snapshot_download(
|
| 33 |
repo_id=MODEL_NSFW,
|
| 34 |
local_dir=path_nsfw,
|
| 35 |
local_dir_use_symlinks=False
|
| 36 |
)
|
| 37 |
+
print("✅ Download Model NSFW Finished")
|
| 38 |
source_path = path_nsfw
|
| 39 |
except Exception as e:
|
| 40 |
+
print(f"❌ Download failed: {e}")
|
| 41 |
+
print("💡 Use standard models instead (may not have NSFW filter)")
|
| 42 |
return MODEL_STANDARD
|
| 43 |
|
| 44 |
if os.path.exists(path_standard):
|
| 45 |
shutil.rmtree(path_standard)
|
| 46 |
+
print(f"📂 Power Copy File from {folder_nsfw} -> {folder_standard}...")
|
| 47 |
shutil.copytree(source_path, path_standard)
|
| 48 |
+
print("✅ Overlay file finish!")
|
| 49 |
else:
|
| 50 |
+
print(f"✅ Models have been found in Cache.: {folder_standard}")
|
| 51 |
|
| 52 |
return MODEL_STANDARD
|
| 53 |
|
| 54 |
# --- เรียกฟังก์ชันจัดการ Cache ---
|
| 55 |
FINAL_MODEL_NAME = setup_model_cache()
|
| 56 |
+
print(f"🚀 Loading model (customized): {FINAL_MODEL_NAME}...")
|
| 57 |
|
| 58 |
# --- ติดตั้ง flash-attn (ถ้าจำเป็น) ---
|
| 59 |
try:
|
| 60 |
import flash_attn
|
| 61 |
+
print("✅ flash_attn Available")
|
| 62 |
except ImportError:
|
| 63 |
+
print("⚠️ flash_attn not Available...")
|
| 64 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
| 65 |
|
| 66 |
# --- โหลดโมเดล ---
|
|
|
|
| 77 |
)
|
| 78 |
print("✅ โหลดโมเดล Florence-2 (NSFW Version) เสร็จสิ้น!")
|
| 79 |
except Exception as e:
|
| 80 |
+
print(f"❌ Error loading model: {e}")
|
| 81 |
florence_model = None
|
| 82 |
florence_processor = None
|
| 83 |
|
|
|
|
| 102 |
def process_image(image, task_name):
|
| 103 |
global florence_model, florence_processor
|
| 104 |
if florence_model is None or florence_processor is None:
|
| 105 |
+
return "❌ The model has not been loaded or there is an error in the start."
|
| 106 |
if image is None:
|
| 107 |
+
return "Please select a picture"
|
| 108 |
try:
|
| 109 |
# แป���ง Gradio Image เป็น PIL Image
|
| 110 |
if not isinstance(image, Image.Image):
|
|
|
|
| 143 |
return result_text
|
| 144 |
|
| 145 |
except Exception as e:
|
| 146 |
+
return f"❌ Error while processing: {str(e)}"
|
| 147 |
|
| 148 |
# --- สร้าง UI ---
|
| 149 |
with gr.Blocks(title="Image-to-Prompt (Florence-2)", theme=gr.themes.Soft()) as demo:
|
| 150 |
gr.Markdown("""
|
| 151 |
# 🖼️ Image-to-Prompt (Florence-2)
|
| 152 |
+
Upload the image and select the analysis type to create Prompt
|
| 153 |
""")
|
| 154 |
|
| 155 |
with gr.Row():
|
| 156 |
with gr.Column():
|
| 157 |
+
image_input = gr.Image(type="pil", label="Pictures", height=400)
|
| 158 |
task_dropdown = gr.Dropdown(
|
| 159 |
choices=list(TASK_PROMPTS.keys()),
|
| 160 |
value="More Detailed Caption", # เลือกเป็นค่าเริ่มต้น
|
| 161 |
+
label="Analysis Type"
|
| 162 |
)
|
| 163 |
+
btn = gr.Button("🚀 Create results", variant="primary")
|
| 164 |
|
| 165 |
with gr.Column():
|
| 166 |
text_output = gr.Textbox(label="ผลลัพธ์", lines=10, max_lines=20, show_copy_button=True)
|