|
|
from fastapi.middleware.cors import CORSMiddleware |
|
|
from fastapi import FastAPI, Request, Header, BackgroundTasks, HTTPException, status, staticfiles |
|
|
from gradio_client import Client |
|
|
import json |
|
|
import os |
|
|
|
|
|
from linebot import ( |
|
|
LineBotApi, WebhookHandler |
|
|
) |
|
|
from linebot.exceptions import ( |
|
|
InvalidSignatureError |
|
|
) |
|
|
from linebot.models import ( |
|
|
MessageEvent, TextMessage, TextSendMessage, ImageSendMessage, AudioMessage |
|
|
) |
|
|
|
|
|
|
|
|
client = Client(os.environ["GeminiRAGapi"], hf_token=os.environ['HF_TOKEN']) |
|
|
|
|
|
line_bot_api = LineBotApi(os.environ["CHANNEL_ACCESS_TOKEN"]) |
|
|
line_handler = WebhookHandler(os.environ["CHANNEL_SECRET"]) |
|
|
|
|
|
working_status = os.getenv("DEFALUT_TALKING", default = "true").lower() == "true" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tools = [generate_and_upload_image, analyze_image_with_text] |
|
|
|
|
|
|
|
|
llm = ChatGoogleGenerativeAI(google_api_key=google_api, model="gemini-2.5-flash-lite", temperature=0.2) |
|
|
|
|
|
|
|
|
prompt_template = ChatPromptTemplate([ |
|
|
("system", "你是一個強大的虛擬穿搭助理,可以根據用戶的請求使用提供的工具。當你執行 generate_and_upload_image 工具\ |
|
|
成功後,你將會獲得一個圖片的包含https的完整網址,你的任務是將這個包含https的完整網址回傳。如果工具有產生錯誤訊息請解讀回應。"), |
|
|
("user", "{input}"), |
|
|
("placeholder", "{agent_scratchpad}"), |
|
|
]) |
|
|
|
|
|
|
|
|
agent = create_tool_calling_agent(llm, tools, prompt_template) |
|
|
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True) |
|
|
|
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
|
|
|
|
|
|
app.add_middleware( |
|
|
CORSMiddleware, |
|
|
allow_origins=["*"], |
|
|
allow_credentials=True, |
|
|
allow_methods=["*"], |
|
|
allow_headers=["*"], |
|
|
) |
|
|
|
|
|
|
|
|
@app.get("/") |
|
|
def root(): |
|
|
return {"title": "RAG Line Bot"} |
|
|
|
|
|
|
|
|
@app.post("/webhook") |
|
|
async def webhook( |
|
|
request: Request, |
|
|
background_tasks: BackgroundTasks, |
|
|
x_line_signature=Header(None), |
|
|
): |
|
|
|
|
|
body = await request.body() |
|
|
try: |
|
|
|
|
|
background_tasks.add_task( |
|
|
line_handler.handle, body.decode("utf-8"), x_line_signature |
|
|
) |
|
|
except InvalidSignatureError: |
|
|
|
|
|
raise HTTPException(status_code=400, detail="Invalid signature") |
|
|
return "ok" |
|
|
|
|
|
|
|
|
@line_handler.add(MessageEvent, message=TextMessage) |
|
|
def handle_message(event): |
|
|
global working_status |
|
|
|
|
|
|
|
|
if event.type != "message" or event.message.type != "text": |
|
|
|
|
|
line_bot_api.reply_message( |
|
|
event.reply_token, |
|
|
TextSendMessage(text="Event type error:[No message or the message does not contain text]") |
|
|
) |
|
|
|
|
|
|
|
|
elif event.message.text == "再見,掰掰,結束,": |
|
|
|
|
|
line_bot_api.reply_message( |
|
|
event.reply_token, |
|
|
TextSendMessage(text="Bye!") |
|
|
) |
|
|
return |
|
|
|
|
|
|
|
|
elif working_status: |
|
|
try: |
|
|
|
|
|
prompt = event.message.text |
|
|
|
|
|
completion = client.predict(question=prompt, api_name="/predict") |
|
|
|
|
|
if (completion != None): |
|
|
|
|
|
out = completion |
|
|
|
|
|
elif type=='text': |
|
|
msg = json_data['events'][0]['message']['text'] |
|
|
reply = msg |
|
|
|
|
|
elif type == 'image': |
|
|
msgID = json_data['events'][0]['message']['id'] |
|
|
message_content = line_bot_api.get_message_content(msgID) |
|
|
|
|
|
with open(f'{msgID}.jpg', 'wb') as fd: |
|
|
fd.write(message_content.content) |
|
|
reply = '圖片儲存完成!' |
|
|
else: |
|
|
reply = '你傳的不是文字或圖片呦~' |
|
|
print(reply) |
|
|
line_bot_api.reply_message(tk,TextSendMessage(reply)) |
|
|
except: |
|
|
|
|
|
out = "Gemini執行出錯!請換個說法!" |
|
|
|
|
|
|
|
|
line_bot_api.reply_message( |
|
|
event.reply_token, |
|
|
TextSendMessage(text=out)) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
|
|
uvicorn.run("main:app", host="0.0.0.0", port=7860, reload=True) |