Spaces:
Runtime error
Runtime error
add GAS
Browse files
main.py
CHANGED
|
@@ -17,6 +17,8 @@ from ultralytics import YOLO
|
|
| 17 |
import shutil
|
| 18 |
import zipfile
|
| 19 |
import uuid # 匯入 uuid 以生成唯一的執行 ID
|
|
|
|
|
|
|
| 20 |
from pathlib import Path # 匯入 Path 以更方便地操作路徑
|
| 21 |
import gemini_ai as genai
|
| 22 |
import gemini_ai_work as genai_work
|
|
@@ -26,6 +28,23 @@ import mongo_lib as mongo
|
|
| 26 |
|
| 27 |
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
def create_zip_archive(files, zip_filename):
|
| 30 |
"""
|
| 31 |
將一系列檔案壓縮成一個 zip 檔案。
|
|
@@ -212,6 +231,9 @@ def gradio_multi_model_detection(
|
|
| 212 |
output_mllm_txt_path = run_output_dir / f"{image_base_name}_mllm_result.txt"
|
| 213 |
output_mllm_txt_path.write_text(mllm_result_content, encoding='utf-8')
|
| 214 |
all_result_files.append(str(output_mllm_txt_path))
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
# --- 3c. 職業預測分析 (如果啟用) ---
|
| 217 |
output_career_prediction_txt_path = None
|
|
@@ -228,6 +250,9 @@ def gradio_multi_model_detection(
|
|
| 228 |
output_career_prediction_txt_path = run_output_dir / f"{image_base_name}_career_prediction.txt"
|
| 229 |
output_career_prediction_txt_path.write_text(career_prediction_result_content, encoding='utf-8')
|
| 230 |
all_result_files.append(str(output_career_prediction_txt_path))
|
|
|
|
|
|
|
|
|
|
| 231 |
|
| 232 |
#寫明細表log
|
| 233 |
# document = {"log_style":"detail",
|
|
@@ -283,8 +308,8 @@ def toggle_career_prediction_checkbox(is_enabled):
|
|
| 283 |
|
| 284 |
# --- Gradio Interface ---
|
| 285 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 286 |
-
gr.Markdown("# ㊙️智慧影像與職業潛能分析 (YOLO + MLLM)")
|
| 287 |
-
gr.Markdown("上傳圖片與YOLO模型進行物件偵測,並可選用MLLM進行進階圖像理解。 ver.
|
| 288 |
# mongo_uri = os.getenv('mongo_uri')
|
| 289 |
# gr.Markdown(mongo_uri)
|
| 290 |
|
|
|
|
| 17 |
import shutil
|
| 18 |
import zipfile
|
| 19 |
import uuid # 匯入 uuid 以生成唯一的執行 ID
|
| 20 |
+
import requests
|
| 21 |
+
import json
|
| 22 |
from pathlib import Path # 匯入 Path 以更方便地操作路徑
|
| 23 |
import gemini_ai as genai
|
| 24 |
import gemini_ai_work as genai_work
|
|
|
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
+
def send_to_gas(action, run_id, file_name, result_content):
|
| 32 |
+
"""
|
| 33 |
+
將分析結果傳送到指定的 Google Apps Script (GAS) URL。
|
| 34 |
+
"""
|
| 35 |
+
gas_url = "https://script.google.com/macros/s/AKfycbyW89mbMozl7KB1RFDLTMmyAbe7Dx87TNE1X5VXRcqidjXQKvYrHgLyOhMRJ2j6ndVLbw/exec"
|
| 36 |
+
payload = {
|
| 37 |
+
"action": action,
|
| 38 |
+
"uuid": f"{{{run_id}}}",
|
| 39 |
+
"檔案名稱": file_name,
|
| 40 |
+
"分析結果": result_content
|
| 41 |
+
}
|
| 42 |
+
try:
|
| 43 |
+
response = requests.post(gas_url, json=payload, timeout=10)
|
| 44 |
+
print(f"GAS 傳送結果 ({action}): {response.status_code}, {response.text}")
|
| 45 |
+
except Exception as e:
|
| 46 |
+
print(f"GAS 傳送失敗: {e}")
|
| 47 |
+
|
| 48 |
def create_zip_archive(files, zip_filename):
|
| 49 |
"""
|
| 50 |
將一系列檔案壓縮成一個 zip 檔案。
|
|
|
|
| 231 |
output_mllm_txt_path = run_output_dir / f"{image_base_name}_mllm_result.txt"
|
| 232 |
output_mllm_txt_path.write_text(mllm_result_content, encoding='utf-8')
|
| 233 |
all_result_files.append(str(output_mllm_txt_path))
|
| 234 |
+
|
| 235 |
+
# 傳送 MLLM 結果至 GAS
|
| 236 |
+
send_to_gas("mllm", run_id, f"{image_base_name}.txt", mllm_result_content)
|
| 237 |
|
| 238 |
# --- 3c. 職業預測分析 (如果啟用) ---
|
| 239 |
output_career_prediction_txt_path = None
|
|
|
|
| 250 |
output_career_prediction_txt_path = run_output_dir / f"{image_base_name}_career_prediction.txt"
|
| 251 |
output_career_prediction_txt_path.write_text(career_prediction_result_content, encoding='utf-8')
|
| 252 |
all_result_files.append(str(output_career_prediction_txt_path))
|
| 253 |
+
|
| 254 |
+
# 傳送職業預測結果至 GAS
|
| 255 |
+
send_to_gas("career_prediction", run_id, f"{image_base_name}_work.txt", career_prediction_result_content)
|
| 256 |
|
| 257 |
#寫明細表log
|
| 258 |
# document = {"log_style":"detail",
|
|
|
|
| 308 |
|
| 309 |
# --- Gradio Interface ---
|
| 310 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 311 |
+
gr.Markdown("# ㊙️智慧影像與職業潛能分析 (YOLO + MLLM + GAS)㊙️")
|
| 312 |
+
gr.Markdown("上傳圖片與YOLO模型(default yolov8n)進行物件偵測,並可選用MLLM進行進階圖像理解。 ver.260228.1")
|
| 313 |
# mongo_uri = os.getenv('mongo_uri')
|
| 314 |
# gr.Markdown(mongo_uri)
|
| 315 |
|