Update main.py
Browse files
main.py
CHANGED
@@ -14,7 +14,7 @@ import PIL.Image
|
|
14 |
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
|
15 |
|
16 |
# 設定生成文字的參數
|
17 |
-
generation_config = genai.types.GenerationConfig(max_output_tokens=
|
18 |
|
19 |
# 使用 Gemini-1.5-flash 模型
|
20 |
model = genai.GenerativeModel('gemini-2.0-flash-exp', system_instruction="主要用繁體中文回答,但如果用戶使用詢問英文問題,就用英文回應。你現在是個專業助理,職稱為OPEN小助理,個性活潑、樂觀,願意回答所有問題", generation_config=generation_config)
|
@@ -61,8 +61,51 @@ async def webhook(
|
|
61 |
# 處理無效的簽章錯誤
|
62 |
raise HTTPException(status_code=400, detail="Invalid signature")
|
63 |
return "ok"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
|
|
|
|
|
|
65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
def get_image_url(message_id):
|
67 |
try:
|
68 |
message_content = line_bot_api.get_message_content(message_id)
|
@@ -112,7 +155,10 @@ def get_previous_message(user_id):
|
|
112 |
# 返回最後一則訊息
|
113 |
return user_message_history[user_id][-1]
|
114 |
return None
|
115 |
-
|
|
|
|
|
|
|
116 |
# 建立 chat_sessions 字典
|
117 |
chat_sessions = {}
|
118 |
@line_handler.add(MessageEvent, message=(ImageMessage,TextMessage))
|
@@ -123,6 +169,27 @@ def handle_image_message(event):
|
|
123 |
else:
|
124 |
chat = model.start_chat(history=[])
|
125 |
chat_sessions[user_id] = chat
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
|
127 |
# 從 LINE API 獲取圖片數據
|
128 |
image_path = get_image_url(event.message.id)
|
|
|
14 |
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
|
15 |
|
16 |
# 設定生成文字的參數
|
17 |
+
generation_config = genai.types.GenerationConfig(max_output_tokens=1000, temperature=0.2, top_p=0.5, top_k=16) #2048
|
18 |
|
19 |
# 使用 Gemini-1.5-flash 模型
|
20 |
model = genai.GenerativeModel('gemini-2.0-flash-exp', system_instruction="主要用繁體中文回答,但如果用戶使用詢問英文問題,就用英文回應。你現在是個專業助理,職稱為OPEN小助理,個性活潑、樂觀,願意回答所有問題", generation_config=generation_config)
|
|
|
61 |
# 處理無效的簽章錯誤
|
62 |
raise HTTPException(status_code=400, detail="Invalid signature")
|
63 |
return "ok"
|
64 |
+
|
65 |
+
#==========================
|
66 |
+
# 使用者請求生成圖片
|
67 |
+
#==========================
|
68 |
+
|
69 |
+
def upload_image_to_imgur_with_token(image_path, access_token):
|
70 |
+
"""
|
71 |
+
使用 Imgur OAuth2 Access Token 上傳圖片
|
72 |
+
"""
|
73 |
+
try:
|
74 |
+
with open(image_path, "rb") as img_file:
|
75 |
+
image_binary = img_file.read()
|
76 |
|
77 |
+
headers = {
|
78 |
+
"Authorization": f"Bearer {access_token}"
|
79 |
+
}
|
80 |
|
81 |
+
data = {
|
82 |
+
"image": base64.b64encode(image_binary).decode("utf-8"),
|
83 |
+
"type": "base64",
|
84 |
+
}
|
85 |
+
|
86 |
+
response = httpx.post("https://api.imgur.com/3/image", headers=headers, data=data)
|
87 |
+
if response.status_code == 200:
|
88 |
+
return response.json()["data"]["link"]
|
89 |
+
else:
|
90 |
+
print("Imgur 上傳失敗:", response.text)
|
91 |
+
return None
|
92 |
+
except Exception as e:
|
93 |
+
print("圖片上傳例外:", e)
|
94 |
+
return None
|
95 |
+
|
96 |
+
def generate_image_with_gemini(prompt):
|
97 |
+
model_name = "gemini-2.0-flash-exp-image-generation"
|
98 |
+
image_model = genai.GenerativeModel(model_name)
|
99 |
+
response = image_model.generate_content(prompt)
|
100 |
+
if response.parts and hasattr(response.parts[0], "inline_data"):
|
101 |
+
image_data = response.parts[0].inline_data.data
|
102 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png", dir="/tmp") as tmp_file:
|
103 |
+
tmp_file.write(image_data)
|
104 |
+
return tmp_file.name
|
105 |
+
return None
|
106 |
+
#==========================
|
107 |
+
# 使用者上傳圖片
|
108 |
+
#==========================
|
109 |
def get_image_url(message_id):
|
110 |
try:
|
111 |
message_content = line_bot_api.get_message_content(message_id)
|
|
|
155 |
# 返回最後一則訊息
|
156 |
return user_message_history[user_id][-1]
|
157 |
return None
|
158 |
+
|
159 |
+
#==========================
|
160 |
+
# 主程式(圖片與文字)
|
161 |
+
#==========================
|
162 |
# 建立 chat_sessions 字典
|
163 |
chat_sessions = {}
|
164 |
@line_handler.add(MessageEvent, message=(ImageMessage,TextMessage))
|
|
|
169 |
else:
|
170 |
chat = model.start_chat(history=[])
|
171 |
chat_sessions[user_id] = chat
|
172 |
+
|
173 |
+
user_text = event.message.text if event.message.type == "text" else None
|
174 |
+
if user_text and user_text.startswith("請幫我生成圖片"):
|
175 |
+
prompt = user_text.replace("請幫我生成圖片", "").strip()
|
176 |
+
|
177 |
+
image_path_gen = generate_image_with_gemini(prompt)
|
178 |
+
if image_path:
|
179 |
+
# 使用 Access Token 上傳至 Imgur
|
180 |
+
IMGUR_ACCESS_TOKEN = os.environ.get("IMGUR_ACCESS_TOKEN")
|
181 |
+
image_url = upload_image_to_imgur_with_token(image_path, IMGUR_ACCESS_TOKEN)
|
182 |
+
|
183 |
+
if image_url:
|
184 |
+
line_bot_api.reply_message(
|
185 |
+
event.reply_token,
|
186 |
+
ImageSendMessage(original_content_url=image_url, preview_image_url=image_url)
|
187 |
+
)
|
188 |
+
else:
|
189 |
+
line_bot_api.reply_message(event.reply_token, TextSendMessage(text="圖片上傳失敗,請稍後再試~"))
|
190 |
+
else:
|
191 |
+
line_bot_api.reply_message(event.reply_token, TextSendMessage(text="圖片生成失敗,請稍後再試~"))
|
192 |
+
|
193 |
|
194 |
# 從 LINE API 獲取圖片數據
|
195 |
image_path = get_image_url(event.message.id)
|