Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
|
@@ -9,6 +9,7 @@ import markdown
|
|
| 9 |
from flask import Flask, request, abort, send_from_directory
|
| 10 |
from bs4 import BeautifulSoup
|
| 11 |
import google.generativeai as genai
|
|
|
|
| 12 |
|
| 13 |
from linebot.v3 import WebhookHandler
|
| 14 |
from linebot.v3.exceptions import InvalidSignatureError
|
|
@@ -32,6 +33,10 @@ GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
|
| 32 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 33 |
model = genai.GenerativeModel("gemini-2.0-flash")
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
# === 初始設定 ===
|
| 36 |
static_tmp_path = "/tmp"
|
| 37 |
os.makedirs(static_tmp_path, exist_ok=True)
|
|
@@ -84,18 +89,54 @@ def callback():
|
|
| 84 |
# === 處理文字訊息 ===
|
| 85 |
@handler.add(MessageEvent, message=TextMessageContent)
|
| 86 |
def handle_text_message(event):
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
)
|
| 98 |
-
)
|
| 99 |
|
| 100 |
# === 處理圖片訊息 ===
|
| 101 |
@handler.add(MessageEvent, message=ImageMessageContent)
|
|
|
|
| 9 |
from flask import Flask, request, abort, send_from_directory
|
| 10 |
from bs4 import BeautifulSoup
|
| 11 |
import google.generativeai as genai
|
| 12 |
+
import openai
|
| 13 |
|
| 14 |
from linebot.v3 import WebhookHandler
|
| 15 |
from linebot.v3.exceptions import InvalidSignatureError
|
|
|
|
| 33 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 34 |
model = genai.GenerativeModel("gemini-2.0-flash")
|
| 35 |
|
| 36 |
+
# === 初始化OpenAI模型 ===
|
| 37 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 38 |
+
|
| 39 |
+
|
| 40 |
# === 初始設定 ===
|
| 41 |
static_tmp_path = "/tmp"
|
| 42 |
os.makedirs(static_tmp_path, exist_ok=True)
|
|
|
|
| 89 |
# === 處理文字訊息 ===
|
| 90 |
@handler.add(MessageEvent, message=TextMessageContent)
|
| 91 |
def handle_text_message(event):
|
| 92 |
+
user_input = event.message.text.strip()
|
| 93 |
+
if user_input.startswith("AI "):
|
| 94 |
+
prompt = user_input[3:].strip()
|
| 95 |
+
try:
|
| 96 |
+
response = openai.Image.create(
|
| 97 |
+
prompt=prompt,
|
| 98 |
+
model="dall-e-3",
|
| 99 |
+
n=1,
|
| 100 |
+
size="1024x1024",
|
| 101 |
+
response_format="url"
|
| 102 |
+
)
|
| 103 |
+
image_url = response['data'][0]['url']
|
| 104 |
+
with ApiClient(configuration) as api_client:
|
| 105 |
+
line_bot_api = MessagingApi(api_client)
|
| 106 |
+
line_bot_api.reply_message(
|
| 107 |
+
ReplyMessageRequest(
|
| 108 |
+
reply_token=event.reply_token,
|
| 109 |
+
messages=[
|
| 110 |
+
ImageMessage(
|
| 111 |
+
original_content_url=image_url,
|
| 112 |
+
preview_image_url=image_url
|
| 113 |
+
)
|
| 114 |
+
]
|
| 115 |
+
)
|
| 116 |
+
)
|
| 117 |
+
except Exception as e:
|
| 118 |
+
app.logger.error(f"DALL·E 3 API error: {e}")
|
| 119 |
+
with ApiClient(configuration) as api_client:
|
| 120 |
+
line_bot_api = MessagingApi(api_client)
|
| 121 |
+
line_bot_api.reply_message(
|
| 122 |
+
ReplyMessageRequest(
|
| 123 |
+
reply_token=event.reply_token,
|
| 124 |
+
messages=[TextMessage(text="抱歉,生成圖像時發生錯誤。")]
|
| 125 |
+
)
|
| 126 |
+
)
|
| 127 |
+
else:
|
| 128 |
+
with ApiClient(configuration) as api_client:
|
| 129 |
+
line_bot_api = MessagingApi(api_client)
|
| 130 |
+
response = query(event.message.text)
|
| 131 |
+
html_msg = markdown.markdown(response)
|
| 132 |
+
soup = BeautifulSoup(html_msg, "html.parser")
|
| 133 |
+
|
| 134 |
+
line_bot_api.reply_message_with_http_info(
|
| 135 |
+
ReplyMessageRequest(
|
| 136 |
+
reply_token=event.reply_token,
|
| 137 |
+
messages=[TextMessage(text=soup.get_text())]
|
| 138 |
+
)
|
| 139 |
)
|
|
|
|
| 140 |
|
| 141 |
# === 處理圖片訊息 ===
|
| 142 |
@handler.add(MessageEvent, message=ImageMessageContent)
|