Update main.py
Browse files
main.py
CHANGED
|
@@ -11,7 +11,6 @@ import json, os
|
|
| 11 |
import io
|
| 12 |
import PIL.Image
|
| 13 |
|
| 14 |
-
from imgurpython import ImgurClient
|
| 15 |
from Image_text_generation import Image_text_Generator
|
| 16 |
from Uploading_images_file import get_image_url,get_file_url, store_user_message, analyze_with_gemini, get_previous_message
|
| 17 |
|
|
@@ -27,18 +26,12 @@ genai_gen.configure(api_key=google_api)
|
|
| 27 |
generation_config = genai_gen.types.GenerationConfig(max_output_tokens=1000, temperature=0.2, top_p=0.5, top_k=16) #2048
|
| 28 |
|
| 29 |
# 使用 gemini-2.0-flash-exp 模型
|
| 30 |
-
model = genai_gen.GenerativeModel('gemini-2.0-flash
|
| 31 |
|
| 32 |
# 設定 Line Bot 的 API 金鑰和秘密金鑰
|
| 33 |
line_bot_api = LineBotApi(os.environ["CHANNEL_ACCESS_TOKEN"])
|
| 34 |
line_handler = WebhookHandler(os.environ["CHANNEL_SECRET"])
|
| 35 |
|
| 36 |
-
# 設定 Imgur 的 API
|
| 37 |
-
client_id = os.environ.get("IMGUR_CLIENT_ID")
|
| 38 |
-
client_secret = os.environ.get("IMGUR_CLIENT_SECRET")
|
| 39 |
-
access_token = os.environ.get("IMGUR_ACCESS_TOKEN")
|
| 40 |
-
refresh_token = os.environ.get("IMGUR_REFRESH_TOKEN")
|
| 41 |
-
|
| 42 |
# 設定是否正在與使用者交談
|
| 43 |
working_status = os.getenv("DEFALUT_TALKING", default = "true").lower() == "true"
|
| 44 |
|
|
@@ -100,14 +93,12 @@ def handle_image_message(event):
|
|
| 100 |
|
| 101 |
# 先立即回覆避免token過期
|
| 102 |
line_bot_api.reply_message(event.reply_token, TextSendMessage(text="圖片生成中~ 請稍候.....✨"))
|
| 103 |
-
|
| 104 |
-
image_generator = Image_text_Generator(imgur)
|
| 105 |
# 生成圖片
|
| 106 |
image_binary = image_generator.generate_image_with_gemini(prompt)
|
| 107 |
|
| 108 |
if image_binary:
|
| 109 |
-
|
| 110 |
-
image_url = image_generator.upload_image_to_imgur(image_binary,album)
|
| 111 |
|
| 112 |
if image_url:
|
| 113 |
# 使用 push message 發送圖片,避免 reply token 超時
|
|
|
|
| 11 |
import io
|
| 12 |
import PIL.Image
|
| 13 |
|
|
|
|
| 14 |
from Image_text_generation import Image_text_Generator
|
| 15 |
from Uploading_images_file import get_image_url,get_file_url, store_user_message, analyze_with_gemini, get_previous_message
|
| 16 |
|
|
|
|
| 26 |
generation_config = genai_gen.types.GenerationConfig(max_output_tokens=1000, temperature=0.2, top_p=0.5, top_k=16) #2048
|
| 27 |
|
| 28 |
# 使用 gemini-2.0-flash-exp 模型
|
| 29 |
+
model = genai_gen.GenerativeModel('gemini-2.0-flash', system_instruction="主要用繁體中文回答,但如果用戶使用詢問英文問題,就用英文回應。你現在是個專業助理,職稱為OPEN小助理,個性活潑、樂觀,願意回答所有問題", generation_config=generation_config)
|
| 30 |
|
| 31 |
# 設定 Line Bot 的 API 金鑰和秘密金鑰
|
| 32 |
line_bot_api = LineBotApi(os.environ["CHANNEL_ACCESS_TOKEN"])
|
| 33 |
line_handler = WebhookHandler(os.environ["CHANNEL_SECRET"])
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
# 設定是否正在與使用者交談
|
| 36 |
working_status = os.getenv("DEFALUT_TALKING", default = "true").lower() == "true"
|
| 37 |
|
|
|
|
| 93 |
|
| 94 |
# 先立即回覆避免token過期
|
| 95 |
line_bot_api.reply_message(event.reply_token, TextSendMessage(text="圖片生成中~ 請稍候.....✨"))
|
| 96 |
+
image_generator = Image_text_Generator(user_id)
|
|
|
|
| 97 |
# 生成圖片
|
| 98 |
image_binary = image_generator.generate_image_with_gemini(prompt)
|
| 99 |
|
| 100 |
if image_binary:
|
| 101 |
+
image_url = image_generator.upload_image_to_imgur()
|
|
|
|
| 102 |
|
| 103 |
if image_url:
|
| 104 |
# 使用 push message 發送圖片,避免 reply token 超時
|