ning8429 commited on
Commit
f5e663f
·
verified ·
1 Parent(s): 1bc3888

Update api_server.py

Browse files
Files changed (1) hide show
  1. api_server.py +30 -31
api_server.py CHANGED
@@ -25,8 +25,7 @@ load_type = 'local'
25
  MODEL_YOLO = "yolo11_detect_best_241024_1.pt"
26
  MODEL_DIR = "./artifacts/models"
27
  YOLO_DIR = "./artifacts/yolo"
28
- IMG2TEXT_URL = "https://f2d87a93eed6d95942.gradio.live/" # 圖生文
29
- TEXT2IMG_URL = "https://1fce6478b5a17e2fda.gradio.live/" # 文生圖
30
 
31
 
32
  # Load the saved YOLO model into memory
@@ -64,18 +63,6 @@ def image_to_base64(image_path):
64
  encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
65
  return encoded_string
66
 
67
- def convert_webp_to_base64(webp_path):
68
- # 開啟 .webp 圖片檔
69
- with Image.open(webp_path) as img:
70
- # 將圖片存到 BytesIO 物件中,以便轉換為 base64
71
- buffered = BytesIO()
72
- img.save(buffered, format="WEBP")
73
-
74
- # 取得 base64 編碼的字串
75
- img_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
76
-
77
- return img_base64
78
-
79
 
80
  # 抓取指定路徑下的所有 JPG 檔案
81
  def get_jpg_files(path):
@@ -92,6 +79,19 @@ def get_jpg_files(path):
92
  # jpg_files = get_jpg_files(image_folder)
93
 
94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  def check_memory_usage():
96
  # Get memory details
97
  memory_info = psutil.virtual_memory()
@@ -120,6 +120,8 @@ def predict():
120
  #user_id = request.args.get('user_id')
121
  file = request.files['image']
122
  message_id = request.form.get('message_id') #str(uuid.uuid4())
 
 
123
 
124
  if 'image' not in request.files:
125
  # Handle if no file is selected
@@ -132,8 +134,10 @@ def predict():
132
  return jsonify({'error': str(e)}), 400
133
 
134
  print("***** FLASK API---/predict Start YOLO predict *****")
 
135
  # Make a prediction using YOLO
136
  results = model(image_data)
 
137
  print ("===== FLASK API---/predict YOLO predict result:",results,"=====")
138
  print("***** FLASK API---/predict YOLO predict DONE *****")
139
 
@@ -176,13 +180,10 @@ def predict():
176
 
177
  for yolo_img in yolo_file: # 每張切圖yolo_img
178
  print("***** FLASK API---/predict 4. START CLIP *****")
179
- client = Client(IMG2TEXT_URL)
180
- clip_result = client.predict(
181
- image=handle_file(yolo_img),
182
- top_k=3,
183
- api_name="/predict"
184
- )
185
- top_k_words.append(clip_result) # CLIP預測3個結果(top_k_words)
186
  encoded_images.append(image_to_base64(yolo_img))
187
  element_list.append(element)
188
  print(f"===== FLASK API---/predict CLIP RESULT:{top_k_words} =====\n")
@@ -213,22 +214,20 @@ def predict():
213
  # API route for health check
214
  @app.route('/text2img', methods=['POST'])
215
  def text2img():
216
- text_message = request.form.get('text_message')
217
  message_id = request.form.get('message_id')
218
-
219
- client = Client(TEXT2IMG_URL)
220
- result = client.predict(
221
- word= text_message,
222
- api_name="/predict"
223
- )
224
- print(f"===== FLASK API---/text2img 文字轉圖片result:{result} =====")
225
- result_img = result[2] # 已經是base64 coded
226
 
227
  # 建立回應資料
228
  response_data = {
229
  'message_id': message_id,
230
  'encoded_image': result_img,
231
- 'description': result[1]
232
  }
233
 
234
  return jsonify(response_data), 200
 
25
  MODEL_YOLO = "yolo11_detect_best_241024_1.pt"
26
  MODEL_DIR = "./artifacts/models"
27
  YOLO_DIR = "./artifacts/yolo"
28
+ GRADIO_URL = "https://f1a7e15aa545f4e172.gradio.live/"
 
29
 
30
 
31
  # Load the saved YOLO model into memory
 
63
  encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
64
  return encoded_string
65
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
  # 抓取指定路徑下的所有 JPG 檔案
68
  def get_jpg_files(path):
 
79
  # jpg_files = get_jpg_files(image_folder)
80
 
81
 
82
+ def clip_model (choice="find_similar_words",image=None,word=None):
83
+ client = Client(GRADIO_URL)
84
+ clip_result = client.predict(
85
+ choice = choice,
86
+ image= handle_file(image),
87
+ word = word,
88
+ top_k = 3,
89
+ api_name = "/run_function"
90
+ )
91
+ return clip_result
92
+
93
+
94
+
95
  def check_memory_usage():
96
  # Get memory details
97
  memory_info = psutil.virtual_memory()
 
120
  #user_id = request.args.get('user_id')
121
  file = request.files['image']
122
  message_id = request.form.get('message_id') #str(uuid.uuid4())
123
+ choice = request.form.get('choice')
124
+ word = request.form.get('word')
125
 
126
  if 'image' not in request.files:
127
  # Handle if no file is selected
 
134
  return jsonify({'error': str(e)}), 400
135
 
136
  print("***** FLASK API---/predict Start YOLO predict *****")
137
+
138
  # Make a prediction using YOLO
139
  results = model(image_data)
140
+
141
  print ("===== FLASK API---/predict YOLO predict result:",results,"=====")
142
  print("***** FLASK API---/predict YOLO predict DONE *****")
143
 
 
180
 
181
  for yolo_img in yolo_file: # 每張切圖yolo_img
182
  print("***** FLASK API---/predict 4. START CLIP *****")
183
+
184
+ clip_model(choice,yolo_img,word)
185
+
186
+ top_k_words.append(clip_result[0]) # CLIP預測3個結果(top_k_words)
 
 
 
187
  encoded_images.append(image_to_base64(yolo_img))
188
  element_list.append(element)
189
  print(f"===== FLASK API---/predict CLIP RESULT:{top_k_words} =====\n")
 
214
  # API route for health check
215
  @app.route('/text2img', methods=['POST'])
216
  def text2img():
 
217
  message_id = request.form.get('message_id')
218
+ choice = request.form.get('choice')
219
+ word = request.form.get('word')
220
+
221
+ clip_model(choice,None,word)
222
+
223
+ print(f"===== FLASK API---/text2img 文字轉圖片result:{clip_result} =====")
224
+ result_img = clip_result[2] # 已經是base64 coded
 
225
 
226
  # 建立回應資料
227
  response_data = {
228
  'message_id': message_id,
229
  'encoded_image': result_img,
230
+ 'description': clip_result[0]
231
  }
232
 
233
  return jsonify(response_data), 200