Spaces:
Sleeping
Sleeping
Update api_server.py
Browse files- api_server.py +24 -18
api_server.py
CHANGED
|
@@ -39,14 +39,14 @@ elif load_type == 'remote_hub_download':
|
|
| 39 |
# 從 Hugging Face Hub 下載模型
|
| 40 |
model_path = hf_hub_download(repo_id=REPO_ID, filename=MODEL_NAME)
|
| 41 |
model = torch.load(model_path)
|
| 42 |
-
model.eval()
|
| 43 |
elif load_type == 'remote_hub_from_pretrained':
|
| 44 |
# 使用 Hugging Face Hub 預訓練的模型方式下載
|
| 45 |
os.environ['TRANSFORMERS_CACHE'] = str(Path(MODEL_DIR).absolute())
|
| 46 |
from huggingface_hub import from_pretrained
|
| 47 |
|
| 48 |
model = from_pretrained(REPO_ID, filename=MODEL_NAME, cache_dir=MODEL_DIR)
|
| 49 |
-
model.eval()
|
| 50 |
else:
|
| 51 |
raise AssertionError('No load type is specified!')
|
| 52 |
|
|
@@ -63,15 +63,14 @@ def get_jpg_files(path):
|
|
| 63 |
"""
|
| 64 |
Args:
|
| 65 |
path: 要搜尋的目錄路徑。
|
| 66 |
-
|
| 67 |
Returns:
|
| 68 |
一個包含所有 JPG 檔案路徑的列表。
|
| 69 |
"""
|
| 70 |
return glob.glob(os.path.join(path, "*.jpg"))
|
| 71 |
|
| 72 |
# 使用範例
|
| 73 |
-
image_folder = '/content/drive/MyDrive/chiikawa' # 替換成你的目錄路徑
|
| 74 |
-
jpg_files = get_jpg_files(image_folder)
|
| 75 |
|
| 76 |
|
| 77 |
# Initialize the Flask application
|
|
@@ -84,7 +83,7 @@ def predict():
|
|
| 84 |
|
| 85 |
#user_id = request.args.get('user_id')
|
| 86 |
file = request.files['image']
|
| 87 |
-
message_id = request.form.get('message_id') #str(uuid.uuid4())
|
| 88 |
|
| 89 |
if 'image' not in request.files:
|
| 90 |
# Handle if no file is selected
|
|
@@ -121,18 +120,25 @@ def predict():
|
|
| 121 |
element_list =[]
|
| 122 |
|
| 123 |
for element, count in element_counts.items():
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
# 建立回應資料
|
| 138 |
response_data = {
|
|
|
|
| 39 |
# 從 Hugging Face Hub 下載模型
|
| 40 |
model_path = hf_hub_download(repo_id=REPO_ID, filename=MODEL_NAME)
|
| 41 |
model = torch.load(model_path)
|
| 42 |
+
#model.eval()
|
| 43 |
elif load_type == 'remote_hub_from_pretrained':
|
| 44 |
# 使用 Hugging Face Hub 預訓練的模型方式下載
|
| 45 |
os.environ['TRANSFORMERS_CACHE'] = str(Path(MODEL_DIR).absolute())
|
| 46 |
from huggingface_hub import from_pretrained
|
| 47 |
|
| 48 |
model = from_pretrained(REPO_ID, filename=MODEL_NAME, cache_dir=MODEL_DIR)
|
| 49 |
+
#model.eval()
|
| 50 |
else:
|
| 51 |
raise AssertionError('No load type is specified!')
|
| 52 |
|
|
|
|
| 63 |
"""
|
| 64 |
Args:
|
| 65 |
path: 要搜尋的目錄路徑。
|
|
|
|
| 66 |
Returns:
|
| 67 |
一個包含所有 JPG 檔案路徑的列表。
|
| 68 |
"""
|
| 69 |
return glob.glob(os.path.join(path, "*.jpg"))
|
| 70 |
|
| 71 |
# 使用範例
|
| 72 |
+
# image_folder = '/content/drive/MyDrive/chiikawa' # 替換成你的目錄路徑
|
| 73 |
+
# jpg_files = get_jpg_files(image_folder)
|
| 74 |
|
| 75 |
|
| 76 |
# Initialize the Flask application
|
|
|
|
| 83 |
|
| 84 |
#user_id = request.args.get('user_id')
|
| 85 |
file = request.files['image']
|
| 86 |
+
message_id = request.form.get('message_id') #str(uuid.uuid4())
|
| 87 |
|
| 88 |
if 'image' not in request.files:
|
| 89 |
# Handle if no file is selected
|
|
|
|
| 120 |
element_list =[]
|
| 121 |
|
| 122 |
for element, count in element_counts.items():
|
| 123 |
+
|
| 124 |
+
output_path = f"{YOLO_DIR}/{message_id}/{element}"
|
| 125 |
+
output_file = get_jpg_files(output_path)
|
| 126 |
+
|
| 127 |
+
element_list.append(element)
|
| 128 |
+
|
| 129 |
+
for output_img in output_file: # 取得每張圖的路徑
|
| 130 |
+
encoded_images.append(image_to_base64(output_img))
|
| 131 |
+
|
| 132 |
+
# if element_counts[element] > 1: #某隻角色的數量>1
|
| 133 |
+
# output_path = f"{YOLO_DIR}/{message_id}/{element}"
|
| 134 |
+
# output_file = get_jpg_files(output_path)
|
| 135 |
+
|
| 136 |
+
# for output_img in output_file: # 取得每張圖的路徑
|
| 137 |
+
# encoded_images.append(image_to_base64(output_img))
|
| 138 |
+
|
| 139 |
+
# else : #某隻角色的數量=1
|
| 140 |
+
# output_path = f"{YOLO_DIR}/{message_id}/{element}/im.jpg.jpg"
|
| 141 |
+
# encoded_images.append(image_to_base64(output_path))
|
| 142 |
|
| 143 |
# 建立回應資料
|
| 144 |
response_data = {
|