ALIMHGFY commited on
Commit
7c8c2b6
·
verified ·
1 Parent(s): e4fdb3c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -73
app.py CHANGED
@@ -7,112 +7,102 @@ from io import BytesIO
7
  import io
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
 
 
50
  css = '''
51
  .gradio-container{max-width: 1100px !important}
52
  #image_upload{min-height:400px}
53
  #image_upload [data-testid="image"], #image_upload [data-testid="image"] > div{min-height: 400px}
54
- #mask_radio .gr-form{background:transparent; border: none}
55
- #word_mask{margin-top: .75em !important}
56
- #word_mask textarea:disabled{opacity: 0.3}
57
- .footer {margin-bottom: 45px;margin-top: 35px;text-align: center;border-bottom: 1px solid #e5e5e5}
58
- .footer>p {font-size: .8rem; display: inline-block; padding: 0 10px;transform: translateY(10px);background: white}
59
- .dark .footer {border-color: #303030}
60
- .dark .footer>p {background: #0b0f19}
61
- .acknowledgments h4{margin: 1.25em 0 .25em 0;font-weight: bold;font-size: 115%}
62
- #image_upload .touch-none{display: flex}
63
- @keyframes spin {
64
- from {
65
- transform: rotate(0deg);
66
- }
67
- to {
68
- transform: rotate(360deg);
69
- }
70
- }
71
- #share-btn-container {padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; max-width: 13rem; margin-left: auto;}
72
- div#share-btn-container > div {flex-direction: row;background: black;align-items: center}
73
- #share-btn-container:hover {background-color: #060606}
74
- #share-btn {all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.5rem !important; padding-bottom: 0.5rem !important;right:0;}
75
- #share-btn * {all: unset}
76
- #share-btn-container div:nth-child(-n+2){width: auto !important;min-height: 0px !important;}
77
- #share-btn-container .wrap {display: none !important}
78
- #share-btn-container.hidden {display: none!important}
79
- #prompt input{width: calc(100% - 160px);border-top-right-radius: 0px;border-bottom-right-radius: 0px;}
80
  #run_button {
81
  width: 100%;
82
- height: 50px; /* تحديد ارتفاع ثابت للزر */
83
  display: flex;
84
  align-items: center;
85
  justify-content: center;
86
  }
87
  #output-img img, #image_upload img {
88
- object-fit: contain; /* الحفاظ على نسبة العرض إلى الارتفاع */
89
  width: 100%;
90
- height: auto; /* السماح بارتفاع تلقائي */
91
  }
92
- #prompt-container{margin-top:-18px;}
93
- #prompt-container .form{border-top-left-radius: 0;border-top-right-radius: 0}
94
- #image_upload{border-bottom-left-radius: 0px;border-bottom-right-radius: 0px}
95
  '''
96
 
 
97
  image_blocks = gr.Blocks(css=css, elem_id="total-container")
98
  with image_blocks as demo:
99
  with gr.Column(elem_id="col-container"):
100
- gr.Markdown("## BRIA Eraser API")
101
  gr.HTML('''
102
  <p style="margin-bottom: 10px; font-size: 94%">
103
- This demo showcases the BRIA Eraser capability, which allows users to remove specific elements or objects from images.<br>
104
- The pipeline comprises multiple components, including <a href="https://huggingface.co/briaai/BRIA-2.3" target="_blank">briaai/BRIA-2.3</a>,
105
- <a href="https://huggingface.co/briaai/BRIA-2.3-ControlNet-Inpainting" target="_blank">briaai/BRIA-2.3-ControlNet-Inpainting</a>,
106
- and <a href="https://huggingface.co/briaai/BRIA-2.3-FAST-LORA" target="_blank">briaai/BRIA-2.3-FAST-LORA</a>, all trained on licensed data.<br>
107
- This ensures full legal liability coverage for copyright and privacy infringement.<br>
108
  Notes:<br>
109
- - High-resolution images may take longer to process.<br>
110
- - For multiple masks, results are better if all masks are included in inference.<br><br>
111
  </p>
112
- <p style="margin-bottom: 10px; font-size: 94%">
113
- API Endpoint available on: <a href="https://fal.ai/models/fal-ai/bria/eraser" target="_blank">fal.ai</a><br>
114
- ComfyUI node is available here: <a href="https://github.com/Bria-AI/ComfyUI-BRIA-API" target="_blank">ComfyUI Node</a>
115
- </p>
116
  ''')
117
  with gr.Row():
118
  with gr.Column():
@@ -120,11 +110,10 @@ with image_blocks as demo:
120
  sources=["upload"],
121
  layers=False,
122
  transforms=[],
123
- brush=gr.Brush(colors=["#000000"], color_mode="fixed"), # اللون الأسود كخيار ثابت
124
  )
125
- with gr.Row(elem_id="prompt-container", equal_height=True):
126
- with gr.Column(): # التفاف الزر داخل عمود
127
- btn = gr.Button("Erase!", elem_id="run_button")
128
 
129
  with gr.Column():
130
  image_out = gr.Image(label="Output", elem_id="output-img")
@@ -135,8 +124,7 @@ with image_blocks as demo:
135
  gr.HTML(
136
  """
137
  <div class="footer">
138
- <p>Model by <a href="https://huggingface.co/diffusers" style="text-decoration: underline;" target="_blank">Diffusers</a> - Gradio Demo by 🤗 Hugging Face
139
- </p>
140
  </div>
141
  """
142
  )
 
7
  import io
8
  import base64
9
 
10
+ # استرداد مفتاح API من متغيرات البيئة
11
+ hf_token = os.environ.get("HF_TOKEN_API_DEMO") # تأكد من ضبط هذا المتغير في بيئتك
12
+ auth_headers = {"Authorization": f"Bearer {hf_token}"} # إعداد المصادقة
13
 
14
+ # تحويل الصورة إلى Base64
15
+ def convert_image_to_base64(image):
16
  buffer = io.BytesIO()
17
+ image.save(buffer, format="PNG") # يمكن استخدام تنسيقات أخرى مثل JPEG إذا لزم الأمر
18
+ return base64.b64encode(buffer.getvalue()).decode('utf-8')
 
 
19
 
20
+ # تنزيل الصورة الناتجة من URL
21
  def download_image(url):
22
  response = requests.get(url)
23
  return Image.open(BytesIO(response.content)).convert("RGB")
24
 
25
+ # استدعاء Bria API باستخدام نقطة النهاية gen_fill
26
+ def call_gen_fill_api(image, mask, api_token):
27
+ url = "https://engine.prod.bria-api.com/v1/gen_fill"
28
+ headers = {
29
+ "Content-Type": "application/json",
30
+ "Authorization": f"Bearer {api_token}",
31
+ }
32
+
33
+ # تحويل الصورة والقناع إلى Base64
34
+ image_base64 = convert_image_to_base64(image)
35
+ mask_base64 = convert_image_to_base64(mask)
36
+
37
  payload = {
38
+ "file": image_base64,
39
+ "mask_file": mask_base64,
40
+ "mask_type": "manual",
41
+ "prompt": "Erase object",
42
+ "negative_prompt": "",
43
+ "num_results": 1,
44
+ "sync": True, # معالجة متزامنة للحصول على النتيجة مباشرة
45
+ "seed": 0,
46
+ "content_moderation": False
47
  }
 
48
 
49
+ response = requests.post(url, json=payload, headers=headers)
50
  if response.status_code == 200:
51
+ result = response.json()
52
+ result_url = result.get("result_url")
53
+ if result_url:
54
+ return download_image(result_url) # تنزيل الصورة الناتجة
55
+ else:
56
+ raise Exception("No result URL found in the response.")
57
  else:
58
  raise Exception(f"API Error: {response.status_code} - {response.text}")
59
 
60
+ # دالة معالجة الصورة والقناع
61
  def predict(dict):
62
  init_image = Image.fromarray(dict['background'][:, :, :3], 'RGB')
63
+ mask = Image.fromarray(dict['layers'][0][:, :, 3], 'L') # القناع بتدرج الرمادي
64
+
65
+ api_token = hf_token # استخدم مفتاح API الخاص بك
66
+ if not api_token:
67
+ return "Error: API token is missing. Please set it in your environment variables."
68
+
69
+ try:
70
+ result_image = call_gen_fill_api(init_image, mask, api_token)
71
+ return result_image
72
+ except Exception as e:
73
+ return f"Error: {e}"
74
 
75
+ # تخصيص CSS
76
  css = '''
77
  .gradio-container{max-width: 1100px !important}
78
  #image_upload{min-height:400px}
79
  #image_upload [data-testid="image"], #image_upload [data-testid="image"] > div{min-height: 400px}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  #run_button {
81
  width: 100%;
82
+ height: 50px;
83
  display: flex;
84
  align-items: center;
85
  justify-content: center;
86
  }
87
  #output-img img, #image_upload img {
88
+ object-fit: contain;
89
  width: 100%;
90
+ height: auto;
91
  }
 
 
 
92
  '''
93
 
94
+ # إعداد واجهة Gradio
95
  image_blocks = gr.Blocks(css=css, elem_id="total-container")
96
  with image_blocks as demo:
97
  with gr.Column(elem_id="col-container"):
98
+ gr.Markdown("## BRIA Eraser API Integration")
99
  gr.HTML('''
100
  <p style="margin-bottom: 10px; font-size: 94%">
101
+ This demo showcases the BRIA Gen-Fill API, allowing users to remove specific elements or objects from images.<br>
 
 
 
 
102
  Notes:<br>
103
+ - For high-resolution images, processing time may be longer.<br>
104
+ - Ensure masks are accurate for better results.<br>
105
  </p>
 
 
 
 
106
  ''')
107
  with gr.Row():
108
  with gr.Column():
 
110
  sources=["upload"],
111
  layers=False,
112
  transforms=[],
113
+ brush=gr.Brush(colors=["#000000"], color_mode="fixed"), # اللون الأسود كخيار فرشاة
114
  )
115
+ with gr.Row(equal_height=True):
116
+ btn = gr.Button("Erase!", elem_id="run_button")
 
117
 
118
  with gr.Column():
119
  image_out = gr.Image(label="Output", elem_id="output-img")
 
124
  gr.HTML(
125
  """
126
  <div class="footer">
127
+ <p>Powered by BRIA API - Gradio Demo by Hugging Face</p>
 
128
  </div>
129
  """
130
  )