Spaces:
Sleeping
Sleeping
# ===東吳大學資料系 2025 年 LINEBOT === | |
from google import genai | |
from flask import Flask, send_from_directory | |
import os | |
import logging | |
import tempfile | |
# === 初始化 Google Gemini === | |
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY") | |
client = genai.Client(api_key=GOOGLE_API_KEY) | |
MODEL_ID = "gemini-2.0-flash" | |
# === 初始設定 === | |
static_tmp_path = tempfile.gettempdir() | |
os.makedirs(static_tmp_path, exist_ok=True) | |
base_url = os.getenv("SPACE_HOST") # e.g., "your-space-name.hf.space" | |
# === Flask 應用初始化 === | |
app = Flask(__name__) | |
logging.basicConfig( | |
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s" | |
) | |
app.logger.setLevel(logging.INFO) | |
# === AI Query 包裝 === | |
def query(payload): | |
response = client.models.generate_content( | |
model=MODEL_ID, | |
contents=payload | |
) | |
return response.text | |
# === 靜態圖檔路由 === | |
def serve_image(filename): | |
return send_from_directory(static_tmp_path, filename) | |
# === LINE Webhook 接收端點 === | |
def home(): | |
return {"message": query("用繁體中文介紹你自己")} | |