Spaces:
Running
Running
Commit ·
6f25e32
1
Parent(s): 8c49f83
feat : add ai agent api
Browse files
app.py
CHANGED
|
@@ -8,7 +8,9 @@ from linebot.exceptions import InvalidSignatureError # 匯入 Line 簽章無效
|
|
| 8 |
from services.linebot import line_handle
|
| 9 |
|
| 10 |
from services.deblur import deblur_image_tiled
|
|
|
|
| 11 |
|
|
|
|
| 12 |
from PIL import Image
|
| 13 |
import io
|
| 14 |
import os
|
|
@@ -69,6 +71,95 @@ async def webhook(
|
|
| 69 |
raise HTTPException(status_code=400, detail="Invalid signature")
|
| 70 |
return "ok"
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
@app.post("/predict")
|
| 73 |
async def predict(
|
| 74 |
request: Request,
|
|
|
|
| 8 |
from services.linebot import line_handle
|
| 9 |
|
| 10 |
from services.deblur import deblur_image_tiled
|
| 11 |
+
from services.agents import run_agent
|
| 12 |
|
| 13 |
+
import json
|
| 14 |
from PIL import Image
|
| 15 |
import io
|
| 16 |
import os
|
|
|
|
| 71 |
raise HTTPException(status_code=400, detail="Invalid signature")
|
| 72 |
return "ok"
|
| 73 |
|
| 74 |
+
@app.post("/ai_agent")
|
| 75 |
+
async def ai_agent(
|
| 76 |
+
input_text: Annotated[str, Form(description="輸入文字")],
|
| 77 |
+
# 將您的 form-data 欄位定義為函數參數,並使用 Form()
|
| 78 |
+
file_name: Annotated[str, Form(description="檔案名稱")] = None,
|
| 79 |
+
file_format: Annotated[str, Form(description="檔案格式")] = None,
|
| 80 |
+
file_url: Annotated[str, Form(description="檔案下載網址")] = None, # 可選參數,有預設值
|
| 81 |
+
# 對於需要轉換類型 (例如 int) 的欄位,直接在類型提示中指定
|
| 82 |
+
file_width: Annotated[int, Form(description="檔案寬度")] = 0,
|
| 83 |
+
file_height: Annotated[int, Form(description="檔案高度")] = 0,
|
| 84 |
+
file_created_at: Annotated[str, Form(description="檔案建立時間")] = None,
|
| 85 |
+
):
|
| 86 |
+
out_str = ""
|
| 87 |
+
image_url = ""
|
| 88 |
+
text_result = ""
|
| 89 |
+
|
| 90 |
+
try:
|
| 91 |
+
|
| 92 |
+
agent_input = input_text
|
| 93 |
+
if file_url:
|
| 94 |
+
agent_input = f"請根據這張圖片回答問題。圖片的路徑是 {file_url},我的問題是:{input_text}"
|
| 95 |
+
|
| 96 |
+
# 運行 LangChain 代理人
|
| 97 |
+
response = run_agent(agent_input)
|
| 98 |
+
# Agent 的輸出是 JSON 字串 (無論成功或失敗)
|
| 99 |
+
out_str = response["output"]
|
| 100 |
+
#print(f"out_str:{out_str}")
|
| 101 |
+
tool_result = out_str.get("tool_result", {})
|
| 102 |
+
if tool_result is None:
|
| 103 |
+
tool_result = {}
|
| 104 |
+
#print(f"tool_result:{tool_result}")
|
| 105 |
+
final_response = out_str.get("final_response")
|
| 106 |
+
#print(f"final_response:{final_response}")
|
| 107 |
+
|
| 108 |
+
if "error" in tool_result:
|
| 109 |
+
# 3A. 處理錯誤情況 (例如:圖片下載失敗、API 錯誤)
|
| 110 |
+
error_msg = tool_result["error"]
|
| 111 |
+
reply_text = f"🚫 圖片工具執行失敗:\n{error_msg}"
|
| 112 |
+
return JSONResponse(
|
| 113 |
+
{"status": "error", "message": reply_text},
|
| 114 |
+
status_code=500
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
elif "image_url" in tool_result:
|
| 118 |
+
# 3B. 處理成功情況 (帶有圖片 URL,例如:圖片生成或去模糊工具)
|
| 119 |
+
image_url = tool_result["image_url"]
|
| 120 |
+
text_result = tool_result.get("text_result", "圖片處理完成。")
|
| 121 |
+
|
| 122 |
+
# Line 要求圖片 URL 必須是 HTTPS
|
| 123 |
+
# 由於 HF_SPACE 預設是 https,這裡做一個保險轉換
|
| 124 |
+
if image_url.startswith("http://"):
|
| 125 |
+
image_url = image_url.replace("http://", "https://")
|
| 126 |
+
|
| 127 |
+
elif "text_result" in tool_result:
|
| 128 |
+
# 3C. 處理純文字結果 (例如:多模態分析工具)
|
| 129 |
+
text_result = tool_result["text_result"]
|
| 130 |
+
|
| 131 |
+
elif final_response:
|
| 132 |
+
text_result = final_response
|
| 133 |
+
|
| 134 |
+
else:
|
| 135 |
+
# 3D. 處理意外的 JSON 結構
|
| 136 |
+
text_result = "代理人回覆格式無法識別,請聯繫管理員。"
|
| 137 |
+
|
| 138 |
+
return JSONResponse(
|
| 139 |
+
{
|
| 140 |
+
"status": "success",
|
| 141 |
+
"message": text_result,
|
| 142 |
+
"image_url" : image_url
|
| 143 |
+
}, status_code=200
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
except json.JSONDecodeError:
|
| 147 |
+
# 4. 處理 Agent 返回了無法解析的純文字 (非 JSON)
|
| 148 |
+
# 這通常發生在 Agent 決定不使用工具,直接回覆純文字,或者推理過程出錯。
|
| 149 |
+
return JSONResponse(
|
| 150 |
+
{"status": "error", "message": out_str }, status_code=500
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
except Exception as e:
|
| 154 |
+
# 處理代理人執行時的錯誤
|
| 155 |
+
print(f"代理人執行出錯: {e}")
|
| 156 |
+
out = f"代理人執行出錯!錯誤訊息:{e}"
|
| 157 |
+
import traceback
|
| 158 |
+
traceback.print_exc()
|
| 159 |
+
return JSONResponse(
|
| 160 |
+
{"status": "error", "message": out }, status_code=500
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
@app.post("/predict")
|
| 164 |
async def predict(
|
| 165 |
request: Request,
|