Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -8,37 +8,42 @@ import io
|
|
8 |
import base64
|
9 |
|
10 |
hf_token = os.environ.get("HF_TOKEN_API_DEMO") # نحصل عليه من متغير بيئة سري لضمان الخصوصية
|
11 |
-
auth_headers = {"
|
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
|
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 |
-
|
|
|
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 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
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 |
|
|
|
8 |
import base64
|
9 |
|
10 |
hf_token = os.environ.get("HF_TOKEN_API_DEMO") # نحصل عليه من متغير بيئة سري لضمان الخصوصية
|
11 |
+
auth_headers = {"Authorization": f"Bearer {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 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 |
+
# تحديث الرابط ليعمل مع Gen-Fill API
|
26 |
+
url = "https://api.bria.ai/v1/gen-fill"
|
27 |
payload = {
|
28 |
"file": image_base64_file,
|
29 |
"mask_file": mask_base64_file,
|
30 |
"mask_type": mask_type,
|
31 |
}
|
32 |
response = requests.post(url, json=payload, headers=auth_headers)
|
33 |
+
|
34 |
+
if response.status_code == 200:
|
35 |
+
response = response.json()
|
36 |
+
res_image = download_image(response["result_url"]) # تحميل الصورة الناتجة
|
37 |
+
return res_image
|
38 |
+
else:
|
39 |
+
raise Exception(f"API Error: {response.status_code} - {response.text}")
|
40 |
|
41 |
def predict(dict):
|
42 |
init_image = Image.fromarray(dict['background'][:, :, :3], 'RGB')
|
43 |
mask = Image.fromarray(dict['layers'][0][:, :, 3], 'L')
|
44 |
image_base64_file = convert_mask_image_to_base64_string(init_image)
|
45 |
mask_base64_file = convert_mask_image_to_base64_string(mask)
|
46 |
+
mask_type = "manual" # حدد نوع القناع
|
47 |
gen_img = eraser_api_call(image_base64_file, mask_base64_file, mask_type)
|
48 |
return gen_img
|
49 |
|