Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
-
|
4 |
import os
|
5 |
from PIL import Image
|
6 |
import requests
|
@@ -8,50 +7,41 @@ from io import BytesIO
|
|
8 |
import io
|
9 |
import base64
|
10 |
|
11 |
-
hf_token = os.environ.get("HF_TOKEN_API_DEMO")
|
12 |
auth_headers = {"api_token": hf_token}
|
13 |
|
14 |
def convert_mask_image_to_base64_string(mask_image):
|
15 |
buffer = io.BytesIO()
|
16 |
-
mask_image.save(buffer, format="PNG") #
|
17 |
-
#
|
18 |
image_base64_string = base64.b64encode(buffer.getvalue()).decode('utf-8')
|
19 |
-
return f",{image_base64_string}"
|
20 |
|
21 |
def download_image(url):
|
22 |
response = requests.get(url)
|
23 |
return Image.open(BytesIO(response.content)).convert("RGB")
|
24 |
|
25 |
def eraser_api_call(image_base64_file, mask_base64_file, mask_type):
|
26 |
-
|
27 |
url = "http://engine.prod.bria-api.com/v1/eraser"
|
28 |
-
|
29 |
payload = {
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
}
|
34 |
response = requests.post(url, json=payload, headers=auth_headers)
|
35 |
response = response.json()
|
36 |
res_image = download_image(response["result_url"])
|
37 |
-
|
38 |
return res_image
|
39 |
|
40 |
-
|
41 |
def predict(dict):
|
42 |
-
|
43 |
-
|
44 |
-
mask = Image.fromarray(dict['layers'][0][:,:,3], 'L') #dict['layers'].convert("RGB")#.resize((1024, 1024))
|
45 |
-
|
46 |
image_base64_file = convert_mask_image_to_base64_string(init_image)
|
47 |
mask_base64_file = convert_mask_image_to_base64_string(mask)
|
48 |
-
|
49 |
mask_type = "manual"
|
50 |
gen_img = eraser_api_call(image_base64_file, mask_base64_file, mask_type)
|
51 |
-
|
52 |
return gen_img
|
53 |
|
54 |
-
|
55 |
css = '''
|
56 |
.gradio-container{max-width: 1100px !important}
|
57 |
#image_upload{min-height:400px}
|
@@ -84,15 +74,15 @@ div#share-btn-container > div {flex-direction: row;background: black;align-items
|
|
84 |
#prompt input{width: calc(100% - 160px);border-top-right-radius: 0px;border-bottom-right-radius: 0px;}
|
85 |
#run_button {
|
86 |
width: 100%;
|
87 |
-
height: 50px; /*
|
88 |
display: flex;
|
89 |
align-items: center;
|
90 |
justify-content: center;
|
91 |
}
|
92 |
#output-img img, #image_upload img {
|
93 |
-
object-fit: contain; /*
|
94 |
width: 100%;
|
95 |
-
height: auto; /*
|
96 |
}
|
97 |
#prompt-container{margin-top:-18px;}
|
98 |
#prompt-container .form{border-top-left-radius: 0;border-top-right-radius: 0}
|
@@ -121,20 +111,22 @@ with image_blocks as demo:
|
|
121 |
''')
|
122 |
with gr.Row():
|
123 |
with gr.Column():
|
124 |
-
image = gr.ImageEditor(
|
125 |
-
|
126 |
-
|
|
|
|
|
|
|
127 |
with gr.Row(elem_id="prompt-container", equal_height=True):
|
128 |
-
with gr.Column(): #
|
129 |
btn = gr.Button("Erase!", elem_id="run_button")
|
130 |
|
131 |
with gr.Column():
|
132 |
image_out = gr.Image(label="Output", elem_id="output-img")
|
133 |
|
134 |
-
#
|
135 |
btn.click(fn=predict, inputs=[image], outputs=[image_out], api_name='run')
|
136 |
|
137 |
-
|
138 |
gr.HTML(
|
139 |
"""
|
140 |
<div class="footer">
|
@@ -144,4 +136,4 @@ with image_blocks as demo:
|
|
144 |
"""
|
145 |
)
|
146 |
|
147 |
-
image_blocks.queue(max_size=25,api_open=False).launch(show_api=False)
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
|
|
3 |
import os
|
4 |
from PIL import Image
|
5 |
import requests
|
|
|
7 |
import io
|
8 |
import base64
|
9 |
|
10 |
+
hf_token = os.environ.get("HF_TOKEN_API_DEMO") # نحصل عليه من متغير بيئة سري لضمان الخصوصية
|
11 |
auth_headers = {"api_token": hf_token}
|
12 |
|
13 |
def convert_mask_image_to_base64_string(mask_image):
|
14 |
buffer = io.BytesIO()
|
15 |
+
mask_image.save(buffer, format="PNG") # يمكن اختيار التنسيق (مثل "JPEG" أو "PNG")
|
16 |
+
# تحويل الصورة إلى base64
|
17 |
image_base64_string = base64.b64encode(buffer.getvalue()).decode('utf-8')
|
18 |
+
return f",{image_base64_string}" # يتم إضافة "," لتكون متوافقة مع الوظيفة التي تستخرج الصور
|
19 |
|
20 |
def download_image(url):
|
21 |
response = requests.get(url)
|
22 |
return Image.open(BytesIO(response.content)).convert("RGB")
|
23 |
|
24 |
def eraser_api_call(image_base64_file, mask_base64_file, mask_type):
|
|
|
25 |
url = "http://engine.prod.bria-api.com/v1/eraser"
|
|
|
26 |
payload = {
|
27 |
+
"file": image_base64_file,
|
28 |
+
"mask_file": mask_base64_file,
|
29 |
+
"mask_type": mask_type,
|
30 |
}
|
31 |
response = requests.post(url, json=payload, headers=auth_headers)
|
32 |
response = response.json()
|
33 |
res_image = download_image(response["result_url"])
|
|
|
34 |
return res_image
|
35 |
|
|
|
36 |
def predict(dict):
|
37 |
+
init_image = Image.fromarray(dict['background'][:, :, :3], 'RGB')
|
38 |
+
mask = Image.fromarray(dict['layers'][0][:, :, 3], 'L')
|
|
|
|
|
39 |
image_base64_file = convert_mask_image_to_base64_string(init_image)
|
40 |
mask_base64_file = convert_mask_image_to_base64_string(mask)
|
|
|
41 |
mask_type = "manual"
|
42 |
gen_img = eraser_api_call(image_base64_file, mask_base64_file, mask_type)
|
|
|
43 |
return gen_img
|
44 |
|
|
|
45 |
css = '''
|
46 |
.gradio-container{max-width: 1100px !important}
|
47 |
#image_upload{min-height:400px}
|
|
|
74 |
#prompt input{width: calc(100% - 160px);border-top-right-radius: 0px;border-bottom-right-radius: 0px;}
|
75 |
#run_button {
|
76 |
width: 100%;
|
77 |
+
height: 50px; /* تحديد ارتفاع ثابت للزر */
|
78 |
display: flex;
|
79 |
align-items: center;
|
80 |
justify-content: center;
|
81 |
}
|
82 |
#output-img img, #image_upload img {
|
83 |
+
object-fit: contain; /* الحفاظ على نسبة العرض إلى الارتفاع */
|
84 |
width: 100%;
|
85 |
+
height: auto; /* السماح بارتفاع تلقائي */
|
86 |
}
|
87 |
#prompt-container{margin-top:-18px;}
|
88 |
#prompt-container .form{border-top-left-radius: 0;border-top-right-radius: 0}
|
|
|
111 |
''')
|
112 |
with gr.Row():
|
113 |
with gr.Column():
|
114 |
+
image = gr.ImageEditor(
|
115 |
+
sources=["upload"],
|
116 |
+
layers=False,
|
117 |
+
transforms=[],
|
118 |
+
brush=gr.Brush(colors=["#000000"], color_mode="fixed"), # اللون الأسود كخيار ثابت
|
119 |
+
)
|
120 |
with gr.Row(elem_id="prompt-container", equal_height=True):
|
121 |
+
with gr.Column(): # التفاف الزر داخل عمود
|
122 |
btn = gr.Button("Erase!", elem_id="run_button")
|
123 |
|
124 |
with gr.Column():
|
125 |
image_out = gr.Image(label="Output", elem_id="output-img")
|
126 |
|
127 |
+
# تشغيل الزر لتفعيل وظيفة التعديل
|
128 |
btn.click(fn=predict, inputs=[image], outputs=[image_out], api_name='run')
|
129 |
|
|
|
130 |
gr.HTML(
|
131 |
"""
|
132 |
<div class="footer">
|
|
|
136 |
"""
|
137 |
)
|
138 |
|
139 |
+
image_blocks.queue(max_size=25, api_open=False).launch(show_api=False)
|