Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -12,14 +12,24 @@ import subprocess
|
|
12 |
# Install necessary packages
|
13 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
14 |
|
15 |
-
# Initialize Florence model
|
16 |
-
model_id = 'microsoft/Florence-2-large'
|
17 |
-
florence_model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True).to("cuda").eval()
|
18 |
-
florence_processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
19 |
-
|
20 |
# Initialize Llama Cleaner model
|
21 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
@spaces.GPU()
|
24 |
def process_image(image, mask, strategy, sampler, fx=1, fy=1):
|
25 |
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
@@ -45,7 +55,7 @@ def process_image(image, mask, strategy, sampler, fx=1, fy=1):
|
|
45 |
def create_mask(image, prediction):
|
46 |
mask = Image.new("RGBA", image.size, (0, 0, 0, 255)) # Black background
|
47 |
draw = ImageDraw.Draw(mask)
|
48 |
-
scale = 1
|
49 |
for polygons in prediction['polygons']:
|
50 |
for _polygon in polygons:
|
51 |
_polygon = np.array(_polygon).reshape(-1, 2)
|
@@ -56,12 +66,14 @@ def create_mask(image, prediction):
|
|
56 |
return mask
|
57 |
|
58 |
@spaces.GPU()
|
59 |
-
def process_images_florence_lama(image):
|
|
|
|
|
60 |
# Convert image to OpenCV format
|
61 |
image_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
62 |
|
63 |
# Run Florence to get mask
|
64 |
-
text_input = 'watermark' # Teks untuk Florence agar mengenali watermark
|
65 |
task_prompt = '<REGION_TO_SEGMENTATION>'
|
66 |
image_pil = Image.fromarray(image_cv) # Convert array to PIL Image
|
67 |
inputs = florence_processor(text=task_prompt + text_input, images=image_pil, return_tensors="pt").to("cuda")
|
@@ -92,11 +104,14 @@ def process_images_florence_lama(image):
|
|
92 |
# Define Gradio interface
|
93 |
demo = gr.Interface(
|
94 |
fn=process_images_florence_lama,
|
95 |
-
inputs=
|
|
|
|
|
|
|
96 |
outputs=gr.Image(type="pil", label="Output Image"),
|
97 |
-
title="Watermark Remover
|
98 |
description="Upload images and remove selected watermarks using Florence and Lama Cleaner.\nhttps://github.com/Damarcreative/rem-wm.git"
|
99 |
)
|
100 |
-
|
101 |
if __name__ == "__main__":
|
102 |
demo.launch()
|
|
|
12 |
# Install necessary packages
|
13 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
14 |
|
|
|
|
|
|
|
|
|
|
|
15 |
# Initialize Llama Cleaner model
|
16 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
17 |
|
18 |
+
# Define available models
|
19 |
+
available_models = [
|
20 |
+
'microsoft/Florence-2-base',
|
21 |
+
'microsoft/Florence-2-base-ft',
|
22 |
+
'microsoft/Florence-2-large',
|
23 |
+
'microsoft/Florence-2-large-ft'
|
24 |
+
]
|
25 |
+
|
26 |
+
# Load all models and processors
|
27 |
+
model_dict = {}
|
28 |
+
for model_id in available_models:
|
29 |
+
florence_model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True).to("cuda").eval()
|
30 |
+
florence_processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
31 |
+
model_dict[model_id] = (florence_model, florence_processor)
|
32 |
+
|
33 |
@spaces.GPU()
|
34 |
def process_image(image, mask, strategy, sampler, fx=1, fy=1):
|
35 |
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
|
|
55 |
def create_mask(image, prediction):
|
56 |
mask = Image.new("RGBA", image.size, (0, 0, 0, 255)) # Black background
|
57 |
draw = ImageDraw.Draw(mask)
|
58 |
+
scale = 1.1
|
59 |
for polygons in prediction['polygons']:
|
60 |
for _polygon in polygons:
|
61 |
_polygon = np.array(_polygon).reshape(-1, 2)
|
|
|
66 |
return mask
|
67 |
|
68 |
@spaces.GPU()
|
69 |
+
def process_images_florence_lama(image, model_choice):
|
70 |
+
florence_model, florence_processor = model_dict[model_choice]
|
71 |
+
|
72 |
# Convert image to OpenCV format
|
73 |
image_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
74 |
|
75 |
# Run Florence to get mask
|
76 |
+
text_input = 'watermark, text' # Teks untuk Florence agar mengenali watermark
|
77 |
task_prompt = '<REGION_TO_SEGMENTATION>'
|
78 |
image_pil = Image.fromarray(image_cv) # Convert array to PIL Image
|
79 |
inputs = florence_processor(text=task_prompt + text_input, images=image_pil, return_tensors="pt").to("cuda")
|
|
|
104 |
# Define Gradio interface
|
105 |
demo = gr.Interface(
|
106 |
fn=process_images_florence_lama,
|
107 |
+
inputs=[
|
108 |
+
gr.Image(type="pil", label="Input Image"),
|
109 |
+
gr.Dropdown(choices=available_models, value='microsoft/Florence-2-large', label="Choose Florence Model")
|
110 |
+
],
|
111 |
outputs=gr.Image(type="pil", label="Output Image"),
|
112 |
+
title="Watermark Remover",
|
113 |
description="Upload images and remove selected watermarks using Florence and Lama Cleaner.\nhttps://github.com/Damarcreative/rem-wm.git"
|
114 |
)
|
115 |
+
|
116 |
if __name__ == "__main__":
|
117 |
demo.launch()
|