ALIMHGFY commited on
Commit
392f702
·
verified ·
1 Parent(s): 2702b53

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -3
app.py CHANGED
@@ -23,6 +23,8 @@ def convert_mask_image_to_base64_string(mask_image):
23
  # تنزيل الصورة من رابط
24
  def download_image(url):
25
  response = requests.get(url)
 
 
26
  return Image.open(BytesIO(response.content)).convert("RGB")
27
 
28
  # استدعاء API الخاص بالإزالة
@@ -33,28 +35,86 @@ def eraser_api_call(image_base64_file, mask_base64_file, mask_type):
33
  "mask_file": mask_base64_file,
34
  "mask_type": mask_type,
35
  }
 
36
  response = requests.post(url, json=payload, headers=auth_headers)
 
37
  if response.status_code != 200:
38
- raise ValueError(f"API request failed with status code {response.status_code}: {response.text}")
 
39
  response = response.json()
 
40
  res_image = download_image(response["result_url"])
41
  return res_image
42
 
43
  # دالة التنبؤ
44
  def predict(dict):
 
 
 
45
  if 'background' not in dict or 'layers' not in dict:
46
  raise ValueError("Invalid input format. Missing 'background' or 'layers' keys.")
 
47
  init_image = Image.fromarray(dict['background'][:, :, :3], 'RGB')
48
  mask = Image.fromarray(dict['layers'][0][:, :, 3], 'L')
 
 
 
49
  image_base64_file = convert_mask_image_to_base64_string(init_image)
50
  mask_base64_file = convert_mask_image_to_base64_string(mask)
 
51
  mask_type = "manual"
 
52
  gen_img = eraser_api_call(image_base64_file, mask_base64_file, mask_type)
 
 
53
  return gen_img
54
 
55
  # CSS مخصص
56
  css = '''
57
- /* محتوى CSS كما هو */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  '''
59
 
60
  # واجهة Gradio
@@ -77,4 +137,4 @@ with image_blocks as demo:
77
  image_out = gr.Image(label="Output", elem_id="output-img")
78
  btn.click(fn=predict, inputs=image, outputs=image_out, api_name='run')
79
  gr.HTML("<div class='footer'><p>Footer text here...</p></div>")
80
- image_blocks.queue(max_size=25, api_open=False).launch(show_api=False)
 
23
  # تنزيل الصورة من رابط
24
  def download_image(url):
25
  response = requests.get(url)
26
+ if response.status_code != 200:
27
+ raise ValueError(f"Failed to download image from {url}. Status code: {response.status_code}")
28
  return Image.open(BytesIO(response.content)).convert("RGB")
29
 
30
  # استدعاء API الخاص بالإزالة
 
35
  "mask_file": mask_base64_file,
36
  "mask_type": mask_type,
37
  }
38
+ print(f"Payload being sent: {payload}") # طباعة البيانات المرسلة
39
  response = requests.post(url, json=payload, headers=auth_headers)
40
+ print(f"API Response Code: {response.status_code}") # طباعة رمز الاستجابة
41
  if response.status_code != 200:
42
+ print(f"Error Response: {response.text}") # طباعة نص الخطأ إذا حدث
43
+ raise ValueError(f"API request failed: {response.status_code}")
44
  response = response.json()
45
+ print("Response JSON:", response) # طباعة الرد JSON
46
  res_image = download_image(response["result_url"])
47
  return res_image
48
 
49
  # دالة التنبؤ
50
  def predict(dict):
51
+ print("Predict function called.") # للتأكد من استدعاء الدالة
52
+ print("Received Data Keys:", dict.keys()) # طباعة المفاتيح المستلمة
53
+
54
  if 'background' not in dict or 'layers' not in dict:
55
  raise ValueError("Invalid input format. Missing 'background' or 'layers' keys.")
56
+
57
  init_image = Image.fromarray(dict['background'][:, :, :3], 'RGB')
58
  mask = Image.fromarray(dict['layers'][0][:, :, 3], 'L')
59
+
60
+ print("Initial image and mask created.") # تأكيد نجاح إنشاء الصور
61
+
62
  image_base64_file = convert_mask_image_to_base64_string(init_image)
63
  mask_base64_file = convert_mask_image_to_base64_string(mask)
64
+
65
  mask_type = "manual"
66
+ print("Sending API call...") # تأكيد استدعاء API
67
  gen_img = eraser_api_call(image_base64_file, mask_base64_file, mask_type)
68
+ print("API call completed.") # تأكيد انتهاء استدعاء API
69
+
70
  return gen_img
71
 
72
  # CSS مخصص
73
  css = '''
74
+ .gradio-container{max-width: 1100px !important}
75
+ #image_upload{min-height:400px}
76
+ #image_upload [data-testid="image"], #image_upload [data-testid="image"] > div{min-height: 400px}
77
+ #mask_radio .gr-form{background:transparent; border: none}
78
+ #word_mask{margin-top: .75em !important}
79
+ #word_mask textarea:disabled{opacity: 0.3}
80
+ .footer {margin-bottom: 45px;margin-top: 35px;text-align: center;border-bottom: 1px solid #e5e5e5}
81
+ .footer>p {font-size: .8rem; display: inline-block; padding: 0 10px;transform: translateY(10px);background: white}
82
+ .dark .footer {border-color: #303030}
83
+ .dark .footer>p {background: #0b0f19}
84
+ .acknowledgments h4{margin: 1.25em 0 .25em 0;font-weight: bold;font-size: 115%}
85
+ #image_upload .touch-none{display: flex}
86
+ @keyframes spin {
87
+ from {
88
+ transform: rotate(0deg);
89
+ }
90
+ to {
91
+ transform: rotate(360deg);
92
+ }
93
+ }
94
+ #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;}
95
+ div#share-btn-container > div {flex-direction: row;background: black;align-items: center}
96
+ #share-btn-container:hover {background-color: #060606}
97
+ #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;}
98
+ #share-btn * {all: unset}
99
+ #share-btn-container div:nth-child(-n+2){width: auto !important;min-height: 0px !important;}
100
+ #share-btn-container .wrap {display: none !important}
101
+ #share-btn-container.hidden {display: none!important}
102
+ #prompt input{width: calc(100% - 160px);border-top-right-radius: 0px;border-bottom-right-radius: 0px;}
103
+ #run_button {
104
+ width: 100%;
105
+ height: 50px; /* Set a fixed height for the button */
106
+ display: flex;
107
+ align-items: center;
108
+ justify-content: center;
109
+ }
110
+ #output-img img, #image_upload img {
111
+ object-fit: contain; /* Ensure aspect ratio is preserved */
112
+ width: 100%;
113
+ height: auto; /* Let height adjust automatically */
114
+ }
115
+ #prompt-container{margin-top:-18px;}
116
+ #prompt-container .form{border-top-left-radius: 0;border-top-right-radius: 0}
117
+ #image_upload{border-bottom-left-radius: 0px;border-bottom-right-radius: 0px}
118
  '''
119
 
120
  # واجهة Gradio
 
137
  image_out = gr.Image(label="Output", elem_id="output-img")
138
  btn.click(fn=predict, inputs=image, outputs=image_out, api_name='run')
139
  gr.HTML("<div class='footer'><p>Footer text here...</p></div>")
140
+ image_blocks.queue(max_size=25, api_open=False).launch(show_api=False, ssr=False)