Spaces:
Running
Running
Commit ·
b82a60c
1
Parent(s): 2188936
feat : image cover local and remote
Browse files- services/agents.py +38 -10
services/agents.py
CHANGED
|
@@ -4,6 +4,7 @@ import PIL.Image # 匯入 PIL 的 Image 模組以處理圖片
|
|
| 4 |
import requests # 匯入 requests 模組以進行 HTTP 請求
|
| 5 |
from dotenv import load_dotenv # 匯入 dotenv 以載入 .env 環境變數檔案
|
| 6 |
import json # 匯入 json 庫用於序列化
|
|
|
|
| 7 |
|
| 8 |
# LangChain 相關匯入
|
| 9 |
from langchain.agents import create_agent
|
|
@@ -27,6 +28,36 @@ google_api = os.environ["GOOGLE_API_KEY"]
|
|
| 27 |
# 初始化 Google GenAI 客戶端
|
| 28 |
genai_client = genai.Client(api_key=google_api)
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
# ==========================
|
| 31 |
# LangChain 工具定義
|
| 32 |
# ==========================
|
|
@@ -97,13 +128,14 @@ def analyze_image_with_text(image_path: str, user_text: str) -> str:
|
|
| 97 |
"""
|
| 98 |
try:
|
| 99 |
# 檢查圖片路徑是否存在
|
| 100 |
-
if not os.path.exists(image_path):
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
|
| 105 |
# 使用 PIL 開啟圖片
|
| 106 |
-
img_user = PIL.Image.open(image_path)
|
|
|
|
| 107 |
# 呼叫 Google GenAI 模型 (gemini-2.5-flash) 進行多模態分析
|
| 108 |
response = genai_client.models.generate_content(
|
| 109 |
model="gemini-2.5-flash",
|
|
@@ -149,13 +181,9 @@ def deblur_image_from_url(
|
|
| 149 |
try:
|
| 150 |
tile_size = 512
|
| 151 |
overlap = 32
|
| 152 |
-
# 1. 下載圖片
|
| 153 |
-
print(f"Agent 正在下載圖片: {file_url}")
|
| 154 |
-
resp = requests.get(file_url, timeout=15)
|
| 155 |
-
resp.raise_for_status()
|
| 156 |
|
| 157 |
# 內容轉換為 PIL Image
|
| 158 |
-
img_input =
|
| 159 |
|
| 160 |
# 2. 執行去模糊處理
|
| 161 |
img_deblurred = deblur_image_tiled(
|
|
|
|
| 4 |
import requests # 匯入 requests 模組以進行 HTTP 請求
|
| 5 |
from dotenv import load_dotenv # 匯入 dotenv 以載入 .env 環境變數檔案
|
| 6 |
import json # 匯入 json 庫用於序列化
|
| 7 |
+
from urllib.parse import urlparse
|
| 8 |
|
| 9 |
# LangChain 相關匯入
|
| 10 |
from langchain.agents import create_agent
|
|
|
|
| 28 |
# 初始化 Google GenAI 客戶端
|
| 29 |
genai_client = genai.Client(api_key=google_api)
|
| 30 |
|
| 31 |
+
# ==========================
|
| 32 |
+
# some 工具定義
|
| 33 |
+
# ==========================
|
| 34 |
+
def load_image(file_url: str) -> PIL.Image.Image:
|
| 35 |
+
"""
|
| 36 |
+
支援本地檔案或 HTTP(S) URL 讀取圖片
|
| 37 |
+
"""
|
| 38 |
+
parsed = urlparse(file_url)
|
| 39 |
+
|
| 40 |
+
if parsed.scheme in ("http", "https"):
|
| 41 |
+
# 網路圖片
|
| 42 |
+
try:
|
| 43 |
+
print(f"Agent 正在下載圖片: {file_url}")
|
| 44 |
+
resp = requests.get(file_url, timeout=15)
|
| 45 |
+
resp.raise_for_status()
|
| 46 |
+
img = PIL.Image.open(io.BytesIO(resp.content)).convert("RGB")
|
| 47 |
+
return img
|
| 48 |
+
except Exception as e:
|
| 49 |
+
raise ValueError(f"下載圖片失敗: {e}")
|
| 50 |
+
else:
|
| 51 |
+
# 本地檔案
|
| 52 |
+
if not os.path.exists(file_url):
|
| 53 |
+
raise ValueError("圖片路徑無效,無法進行分析。")
|
| 54 |
+
try:
|
| 55 |
+
img = PIL.Image.open(file_url).convert("RGB")
|
| 56 |
+
return img
|
| 57 |
+
except Exception as e:
|
| 58 |
+
raise ValueError(f"開啟本地圖片失敗: {e}")
|
| 59 |
+
|
| 60 |
+
|
| 61 |
# ==========================
|
| 62 |
# LangChain 工具定義
|
| 63 |
# ==========================
|
|
|
|
| 128 |
"""
|
| 129 |
try:
|
| 130 |
# 檢查圖片路徑是否存在
|
| 131 |
+
#if not os.path.exists(image_path):
|
| 132 |
+
# return json.dumps({
|
| 133 |
+
# "error": "圖片路徑無效,無法進行分析。"
|
| 134 |
+
# })
|
| 135 |
|
| 136 |
# 使用 PIL 開啟圖片
|
| 137 |
+
#img_user = PIL.Image.open(image_path)
|
| 138 |
+
img_user = load_image(image_path)
|
| 139 |
# 呼叫 Google GenAI 模型 (gemini-2.5-flash) 進行多模態分析
|
| 140 |
response = genai_client.models.generate_content(
|
| 141 |
model="gemini-2.5-flash",
|
|
|
|
| 181 |
try:
|
| 182 |
tile_size = 512
|
| 183 |
overlap = 32
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
# 內容轉換為 PIL Image
|
| 186 |
+
img_input = load_image(file_url)
|
| 187 |
|
| 188 |
# 2. 執行去模糊處理
|
| 189 |
img_deblurred = deblur_image_tiled(
|