Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
import os
|
| 2 |
import io
|
|
|
|
| 3 |
import PIL.Image
|
| 4 |
import uvicorn
|
| 5 |
-
import re
|
| 6 |
from collections import defaultdict
|
| 7 |
from fastapi import FastAPI, Request, Header, BackgroundTasks, HTTPException
|
| 8 |
from fastapi.middleware.cors import CORSMiddleware
|
|
@@ -19,16 +19,23 @@ from linebot.models import (
|
|
| 19 |
ImageMessage,
|
| 20 |
)
|
| 21 |
|
| 22 |
-
# --- 核心修正:
|
| 23 |
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 24 |
from langchain_core.tools import tool
|
| 25 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 26 |
-
|
| 27 |
try:
|
| 28 |
-
|
|
|
|
| 29 |
except ImportError:
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
# ==========================
|
| 34 |
# 環境設定與工具函式
|
|
@@ -82,6 +89,7 @@ def get_previous_message(user_id):
|
|
| 82 |
def generate_and_upload_image(prompt: str) -> str:
|
| 83 |
"""根據文字提示生成圖片。"""
|
| 84 |
try:
|
|
|
|
| 85 |
response = genai_client.models.generate_content(
|
| 86 |
model="imagen-3.0-generate-001",
|
| 87 |
contents=prompt,
|
|
@@ -100,7 +108,6 @@ def generate_and_upload_image(prompt: str) -> str:
|
|
| 100 |
image.save(file_name, format="PNG")
|
| 101 |
|
| 102 |
base_url = os.getenv("HF_SPACE", "http://localhost:7860").rstrip("/")
|
| 103 |
-
# 如果在本地執行,請確保 URL 格式正確
|
| 104 |
return f"{base_url}/{file_name}"
|
| 105 |
return "圖片生成失敗:模型未回傳數據。"
|
| 106 |
except Exception as e:
|
|
@@ -113,7 +120,7 @@ def analyze_image_with_text(image_path: str, user_text: str) -> str:
|
|
| 113 |
if not os.path.exists(image_path):
|
| 114 |
return "錯誤:找不到該圖片檔案。"
|
| 115 |
img_user = PIL.Image.open(image_path)
|
| 116 |
-
# 修正:
|
| 117 |
response = genai_client.models.generate_content(
|
| 118 |
model="gemini-1.5-flash",
|
| 119 |
contents=[img_user, user_text]
|
|
@@ -126,6 +133,7 @@ def analyze_image_with_text(image_path: str, user_text: str) -> str:
|
|
| 126 |
# LangChain 代理人設定
|
| 127 |
# ==========================
|
| 128 |
tools = [generate_and_upload_image, analyze_image_with_text]
|
|
|
|
| 129 |
llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash", temperature=0.2)
|
| 130 |
|
| 131 |
prompt_template = ChatPromptTemplate.from_messages([
|
|
@@ -158,21 +166,21 @@ async def webhook(request: Request, background_tasks: BackgroundTasks, x_line_si
|
|
| 158 |
def handle_message(event):
|
| 159 |
user_id = event.source.user_id
|
| 160 |
|
| 161 |
-
# 處理圖片訊息
|
| 162 |
if isinstance(event.message, ImageMessage):
|
| 163 |
image_path = get_image_url_from_line(event.message.id)
|
| 164 |
if image_path:
|
| 165 |
store_user_message(user_id, "image", image_path)
|
| 166 |
-
line_bot_api.reply_message(event.reply_token, TextSendMessage(text="收到圖片了!請問你想
|
| 167 |
|
| 168 |
-
# 處理文字訊息
|
| 169 |
elif isinstance(event.message, TextMessage):
|
| 170 |
user_text = event.message.text
|
| 171 |
previous_message = get_previous_message(user_id)
|
| 172 |
|
| 173 |
if previous_message["type"] == "image":
|
| 174 |
image_path = previous_message["content"]
|
| 175 |
-
agent_input = {"input": f"請分析圖片 {image_path},問題:{user_text}"}
|
| 176 |
user_message_history[user_id].pop()
|
| 177 |
else:
|
| 178 |
agent_input = {"input": user_text}
|
|
@@ -181,15 +189,15 @@ def handle_message(event):
|
|
| 181 |
response = agent_executor.invoke(agent_input)
|
| 182 |
out = response["output"]
|
| 183 |
|
| 184 |
-
# URL
|
| 185 |
urls = re.findall(r'https?://[^\s<>"]+|www\.[^\s<>"]+', out)
|
| 186 |
-
image_url = next((u for u in urls if any(ext in u.lower() for ext in ['.png', '.jpg'
|
| 187 |
|
| 188 |
if image_url:
|
| 189 |
line_bot_api.reply_message(
|
| 190 |
event.reply_token,
|
| 191 |
[
|
| 192 |
-
TextSendMessage(text="這是生成的圖片:"),
|
| 193 |
ImageSendMessage(original_content_url=image_url, preview_image_url=image_url)
|
| 194 |
]
|
| 195 |
)
|
|
@@ -197,7 +205,7 @@ def handle_message(event):
|
|
| 197 |
line_bot_api.reply_message(event.reply_token, TextSendMessage(text=out))
|
| 198 |
except Exception as e:
|
| 199 |
print(f"Agent Error: {e}")
|
| 200 |
-
line_bot_api.reply_message(event.reply_token, TextSendMessage(text="處理
|
| 201 |
|
| 202 |
if __name__ == "__main__":
|
| 203 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
import os
|
| 2 |
import io
|
| 3 |
+
import re
|
| 4 |
import PIL.Image
|
| 5 |
import uvicorn
|
|
|
|
| 6 |
from collections import defaultdict
|
| 7 |
from fastapi import FastAPI, Request, Header, BackgroundTasks, HTTPException
|
| 8 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 19 |
ImageMessage,
|
| 20 |
)
|
| 21 |
|
| 22 |
+
# --- 核心修正:動態匯入 AgentExecutor ---
|
| 23 |
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 24 |
from langchain_core.tools import tool
|
| 25 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 26 |
+
|
| 27 |
try:
|
| 28 |
+
# 嘗試 1: 標準匯入
|
| 29 |
+
from langchain.agents import AgentExecutor, create_tool_calling_agent
|
| 30 |
except ImportError:
|
| 31 |
+
try:
|
| 32 |
+
# 嘗試 2: 從 executor 模組匯入 (部分 0.2/0.3 版本路徑)
|
| 33 |
+
from langchain.agents.agent_executor import AgentExecutor
|
| 34 |
+
from langchain.agents import create_tool_calling_agent
|
| 35 |
+
except ImportError:
|
| 36 |
+
# 嘗試 3: 最後手段
|
| 37 |
+
from langchain.agents.executor import AgentExecutor
|
| 38 |
+
from langchain.agents import create_tool_calling_agent
|
| 39 |
|
| 40 |
# ==========================
|
| 41 |
# 環境設定與工具函式
|
|
|
|
| 89 |
def generate_and_upload_image(prompt: str) -> str:
|
| 90 |
"""根據文字提示生成圖片。"""
|
| 91 |
try:
|
| 92 |
+
# 修正:2026 年請使用實質存在的模型 imagen-3.0
|
| 93 |
response = genai_client.models.generate_content(
|
| 94 |
model="imagen-3.0-generate-001",
|
| 95 |
contents=prompt,
|
|
|
|
| 108 |
image.save(file_name, format="PNG")
|
| 109 |
|
| 110 |
base_url = os.getenv("HF_SPACE", "http://localhost:7860").rstrip("/")
|
|
|
|
| 111 |
return f"{base_url}/{file_name}"
|
| 112 |
return "圖片生成失敗:模型未回傳數據。"
|
| 113 |
except Exception as e:
|
|
|
|
| 120 |
if not os.path.exists(image_path):
|
| 121 |
return "錯誤:找不到該圖片檔案。"
|
| 122 |
img_user = PIL.Image.open(image_path)
|
| 123 |
+
# 修正模型:gemini-1.5-flash 或 2.0-flash
|
| 124 |
response = genai_client.models.generate_content(
|
| 125 |
model="gemini-1.5-flash",
|
| 126 |
contents=[img_user, user_text]
|
|
|
|
| 133 |
# LangChain 代理人設定
|
| 134 |
# ==========================
|
| 135 |
tools = [generate_and_upload_image, analyze_image_with_text]
|
| 136 |
+
# 修正模型:gemini-3-flash-preview 目前在正式 API 中通常不可用
|
| 137 |
llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash", temperature=0.2)
|
| 138 |
|
| 139 |
prompt_template = ChatPromptTemplate.from_messages([
|
|
|
|
| 166 |
def handle_message(event):
|
| 167 |
user_id = event.source.user_id
|
| 168 |
|
| 169 |
+
# 1. 處理圖片訊息
|
| 170 |
if isinstance(event.message, ImageMessage):
|
| 171 |
image_path = get_image_url_from_line(event.message.id)
|
| 172 |
if image_path:
|
| 173 |
store_user_message(user_id, "image", image_path)
|
| 174 |
+
line_bot_api.reply_message(event.reply_token, TextSendMessage(text="收到圖片了!請問你想對這張圖做什麼分析?"))
|
| 175 |
|
| 176 |
+
# 2. 處理文字訊息 (修正縮進,確保它與上面的 if 對齊)
|
| 177 |
elif isinstance(event.message, TextMessage):
|
| 178 |
user_text = event.message.text
|
| 179 |
previous_message = get_previous_message(user_id)
|
| 180 |
|
| 181 |
if previous_message["type"] == "image":
|
| 182 |
image_path = previous_message["content"]
|
| 183 |
+
agent_input = {"input": f"請分析這張圖片 {image_path},問題是:{user_text}"}
|
| 184 |
user_message_history[user_id].pop()
|
| 185 |
else:
|
| 186 |
agent_input = {"input": user_text}
|
|
|
|
| 189 |
response = agent_executor.invoke(agent_input)
|
| 190 |
out = response["output"]
|
| 191 |
|
| 192 |
+
# 搜尋 URL 邏輯
|
| 193 |
urls = re.findall(r'https?://[^\s<>"]+|www\.[^\s<>"]+', out)
|
| 194 |
+
image_url = next((u for u in urls if any(ext in u.lower() for ext in ['.png', '.jpg'])), None)
|
| 195 |
|
| 196 |
if image_url:
|
| 197 |
line_bot_api.reply_message(
|
| 198 |
event.reply_token,
|
| 199 |
[
|
| 200 |
+
TextSendMessage(text="這是我為你生成的圖片:"),
|
| 201 |
ImageSendMessage(original_content_url=image_url, preview_image_url=image_url)
|
| 202 |
]
|
| 203 |
)
|
|
|
|
| 205 |
line_bot_api.reply_message(event.reply_token, TextSendMessage(text=out))
|
| 206 |
except Exception as e:
|
| 207 |
print(f"Agent Error: {e}")
|
| 208 |
+
line_bot_api.reply_message(event.reply_token, TextSendMessage(text="抱歉,我現在無法處理這個請求。"))
|
| 209 |
|
| 210 |
if __name__ == "__main__":
|
| 211 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|