Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,7 @@ import requests
|
|
6 |
from io import BytesIO
|
7 |
import io
|
8 |
import base64
|
|
|
9 |
|
10 |
# الحصول على التوكن من متغير البيئة
|
11 |
hf_token = os.environ.get("HF_TOKEN_API_DEMO")
|
@@ -13,6 +14,15 @@ if not hf_token:
|
|
13 |
raise ValueError("HF_TOKEN_API_DEMO is not set. Please set it as an environment variable.")
|
14 |
auth_headers = {"api_token": hf_token}
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
# تحويل الصورة إلى Base64
|
17 |
def convert_mask_image_to_base64_string(mask_image):
|
18 |
buffer = io.BytesIO()
|
@@ -41,9 +51,16 @@ def eraser_api_call(image_base64_file, mask_base64_file, mask_type):
|
|
41 |
if response.status_code != 200:
|
42 |
print(f"Error Response: {response.text}") # طباعة نص الخطأ إذا حدث
|
43 |
raise ValueError(f"API request failed: {response.status_code}")
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
return res_image
|
48 |
|
49 |
# دالة التنبؤ
|
@@ -69,6 +86,9 @@ def predict(dict):
|
|
69 |
|
70 |
return gen_img
|
71 |
|
|
|
|
|
|
|
72 |
# CSS مخصص
|
73 |
css = '''
|
74 |
.gradio-container{max-width: 1100px !important}
|
|
|
6 |
from io import BytesIO
|
7 |
import io
|
8 |
import base64
|
9 |
+
import json # لإضافة دعم قراءة ملف config.json
|
10 |
|
11 |
# الحصول على التوكن من متغير البيئة
|
12 |
hf_token = os.environ.get("HF_TOKEN_API_DEMO")
|
|
|
14 |
raise ValueError("HF_TOKEN_API_DEMO is not set. Please set it as an environment variable.")
|
15 |
auth_headers = {"api_token": hf_token}
|
16 |
|
17 |
+
# تحميل إعدادات النموذج من ملف config.json
|
18 |
+
def load_config(config_path="config.json"):
|
19 |
+
if not os.path.exists(config_path):
|
20 |
+
raise FileNotFoundError(f"Config file not found at {config_path}")
|
21 |
+
with open(config_path, 'r') as file:
|
22 |
+
config = json.load(file)
|
23 |
+
print("Config loaded successfully:", config)
|
24 |
+
return config
|
25 |
+
|
26 |
# تحويل الصورة إلى Base64
|
27 |
def convert_mask_image_to_base64_string(mask_image):
|
28 |
buffer = io.BytesIO()
|
|
|
51 |
if response.status_code != 200:
|
52 |
print(f"Error Response: {response.text}") # طباعة نص الخطأ إذا حدث
|
53 |
raise ValueError(f"API request failed: {response.status_code}")
|
54 |
+
try:
|
55 |
+
response_json = response.json()
|
56 |
+
print("Response JSON:", response_json) # طباعة الرد JSON
|
57 |
+
except ValueError:
|
58 |
+
raise ValueError("Invalid JSON response from API")
|
59 |
+
|
60 |
+
if "result_url" not in response_json:
|
61 |
+
raise KeyError("The key 'result_url' is missing in the response JSON")
|
62 |
+
|
63 |
+
res_image = download_image(response_json["result_url"])
|
64 |
return res_image
|
65 |
|
66 |
# دالة التنبؤ
|
|
|
86 |
|
87 |
return gen_img
|
88 |
|
89 |
+
# قراءة إعدادات config.json (إذا لزم الأمر)
|
90 |
+
config = load_config()
|
91 |
+
|
92 |
# CSS مخصص
|
93 |
css = '''
|
94 |
.gradio-container{max-width: 1100px !important}
|