Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -2,24 +2,17 @@ import json, os
|
|
2 |
import gradio as gr
|
3 |
from fastapi.middleware.cors import CORSMiddleware
|
4 |
from fastapi import FastAPI, Request, Header, BackgroundTasks, HTTPException, status
|
5 |
-
# import google.generativeai as genai
|
6 |
from google import genai
|
7 |
from linebot import LineBotApi, WebhookHandler
|
8 |
from linebot.exceptions import InvalidSignatureError
|
9 |
from linebot.models import MessageEvent, TextMessage, TextSendMessage, ImageSendMessage, AudioMessage
|
10 |
|
11 |
# 設定 Google AI API 金鑰
|
12 |
-
# genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
|
13 |
client = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))
|
14 |
|
15 |
# 設定生成文字的參數
|
16 |
generation_config = genai.types.GenerateContentConfig(max_output_tokens=2048, temperature=0.2, top_p=0.5, top_k=16)
|
17 |
|
18 |
-
# 使用 Gemini-2.0-flash 模型
|
19 |
-
# model = genai.GenerativeModel('gemini-2.0-flash',
|
20 |
-
# generation_config=generation_config,
|
21 |
-
# system_instruction="你是博通古今的萬應機器人!")
|
22 |
-
|
23 |
# 設定 Line Bot 的 API 金鑰和秘密金鑰
|
24 |
line_bot_api = LineBotApi(os.environ["CHANNEL_ACCESS_TOKEN"])
|
25 |
line_handler = WebhookHandler(os.environ["CHANNEL_SECRET"])
|
@@ -88,10 +81,9 @@ def handle_message(event):
|
|
88 |
|
89 |
# 檢查是否正在與使用者交談
|
90 |
elif working_status:
|
91 |
-
|
92 |
-
# 使用 model.start_chat 來初始化 user_id 的對話
|
93 |
user_id = event.source.user_id
|
94 |
-
chat = chat_sessions.get(user_id) or client.chats.create(model="gemini-2.0-flash", config=generation_config)
|
95 |
chat_sessions[user_id] = chat
|
96 |
# 取得使用者輸入的文字
|
97 |
user_input = event.message.text
|
@@ -102,14 +94,14 @@ def handle_message(event):
|
|
102 |
out = response.text
|
103 |
else:
|
104 |
out = "Gemini沒答案!請換個說法!"
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
|
114 |
if __name__ == "__main__":
|
115 |
# 啟動 FastAPI 應用程式
|
|
|
2 |
import gradio as gr
|
3 |
from fastapi.middleware.cors import CORSMiddleware
|
4 |
from fastapi import FastAPI, Request, Header, BackgroundTasks, HTTPException, status
|
|
|
5 |
from google import genai
|
6 |
from linebot import LineBotApi, WebhookHandler
|
7 |
from linebot.exceptions import InvalidSignatureError
|
8 |
from linebot.models import MessageEvent, TextMessage, TextSendMessage, ImageSendMessage, AudioMessage
|
9 |
|
10 |
# 設定 Google AI API 金鑰
|
|
|
11 |
client = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))
|
12 |
|
13 |
# 設定生成文字的參數
|
14 |
generation_config = genai.types.GenerateContentConfig(max_output_tokens=2048, temperature=0.2, top_p=0.5, top_k=16)
|
15 |
|
|
|
|
|
|
|
|
|
|
|
16 |
# 設定 Line Bot 的 API 金鑰和秘密金鑰
|
17 |
line_bot_api = LineBotApi(os.environ["CHANNEL_ACCESS_TOKEN"])
|
18 |
line_handler = WebhookHandler(os.environ["CHANNEL_SECRET"])
|
|
|
81 |
|
82 |
# 檢查是否正在與使用者交談
|
83 |
elif working_status:
|
84 |
+
try:
|
|
|
85 |
user_id = event.source.user_id
|
86 |
+
chat = chat_sessions.get(user_id) or client.chats.create(model="gemini-2.0-flash", config=generation_config)
|
87 |
chat_sessions[user_id] = chat
|
88 |
# 取得使用者輸入的文字
|
89 |
user_input = event.message.text
|
|
|
94 |
out = response.text
|
95 |
else:
|
96 |
out = "Gemini沒答案!請換個說法!"
|
97 |
+
except:
|
98 |
+
# 處理錯誤
|
99 |
+
out = "Gemini執行出錯!請換個說法!"
|
100 |
|
101 |
+
# 回覆生成結果
|
102 |
+
line_bot_api.reply_message(
|
103 |
+
event.reply_token,
|
104 |
+
TextSendMessage(text=out))
|
105 |
|
106 |
if __name__ == "__main__":
|
107 |
# 啟動 FastAPI 應用程式
|