3v324v23 commited on
Commit
b859a54
·
1 Parent(s): 6c22e42
Files changed (3) hide show
  1. Dockerfile +3 -3
  2. test.py +42 -0
  3. test_requirements.txt +7 -0
Dockerfile CHANGED
@@ -2,10 +2,10 @@ FROM python:3.12.2
2
 
3
  WORKDIR /code
4
 
5
- COPY ./requirements.txt /code/requirements.txt
6
 
7
- RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
 
9
  COPY . .
10
 
11
- CMD ["gunicorn","-b", "0.0.0.0:7860", "main:app"]
 
2
 
3
  WORKDIR /code
4
 
5
+ COPY ./test_requirements.txt /code/test_requirements.txt
6
 
7
+ RUN pip install --no-cache-dir --upgrade -r /code/test_requirements.txt
8
 
9
  COPY . .
10
 
11
+ CMD ["gunicorn","-b", "0.0.0.0:7860", "test:app"]
test.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ===東吳大學資料系 2025 年 LINEBOT ===
2
+ from google import genai
3
+ from flask import Flask, send_from_directory
4
+ import os
5
+ import logging
6
+ import tempfile
7
+
8
+ # === 初始化 Google Gemini ===
9
+ GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
10
+ client = genai.Client(api_key=GOOGLE_API_KEY)
11
+ MODEL_ID = "gemini-2.0-flash"
12
+
13
+ # === 初始設定 ===
14
+ static_tmp_path = tempfile.gettempdir()
15
+ os.makedirs(static_tmp_path, exist_ok=True)
16
+ base_url = os.getenv("SPACE_HOST") # e.g., "your-space-name.hf.space"
17
+
18
+ # === Flask 應用初始化 ===
19
+ app = Flask(__name__)
20
+ logging.basicConfig(
21
+ level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
22
+ )
23
+ app.logger.setLevel(logging.INFO)
24
+
25
+ # === AI Query 包裝 ===
26
+ def query(payload):
27
+ response = client.models.generate_content(
28
+ model=MODEL_ID,
29
+ contents=payload
30
+ )
31
+ return response.text
32
+
33
+ # === 靜態圖檔路由 ===
34
+ @app.route("/images/<filename>")
35
+ def serve_image(filename):
36
+ return send_from_directory(static_tmp_path, filename)
37
+
38
+
39
+ # === LINE Webhook 接收端點 ===
40
+ @app.route("/")
41
+ def home():
42
+ return {"message": query("用繁體中文介紹你自己")}
test_requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ Flask
2
+ gunicorn
3
+ line-bot-sdk
4
+ markdown
5
+ beautifulsoup4
6
+ openai
7
+ google-genai