From c967531e95c2dc1c3179ec9606e30c2baf5059e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=B0=E5=BF=97=E5=AE=8F?= Date: Mon, 3 Mar 2025 18:28:11 +0800 Subject: [PATCH] =?UTF-8?q?ADD:=20=E5=85=AC=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 7 ++++--- README.md | 6 +++--- docker-compose.yml | 6 ++++-- stock_analyzer.py | 10 +++++----- templates/index.html | 20 ++++++++++++++++++++ web_server.py | 5 ++++- 6 files changed, 40 insertions(+), 14 deletions(-) diff --git a/.env b/.env index 1664e9d..42413d2 100644 --- a/.env +++ b/.env @@ -1,3 +1,4 @@ -GEMINI_API_KEY= -GEMINI_API_URL= -GEMINI_API_MODEL= +API_KEY= +API_URL= +API_MODEL= +ANNOUNCEMENT_TEXT= \ No newline at end of file diff --git a/README.md b/README.md index c46ea71..1cd37bd 100644 --- a/README.md +++ b/README.md @@ -14,9 +14,9 @@ docker run -d \ --name stock-scanner \ -p 8888:8888 \ - -e GEMINI_API_KEY=替换为你的key \ - -e GEMINI_API_URL=替换为你的api地址 \ - -e GEMINI_API_MODEL=替换为你的模型 \ + -e API_KEY=替换为你的key \ + -e API_URL=替换为你的api地址 \ + -e API_MODEL=替换为你的模型 \ lanzhihong/stock-scanner:latest ``` 默认8888端口,部署完成后访问 http://127.0.0.1:8888 即可使用。 diff --git a/docker-compose.yml b/docker-compose.yml index 0b5b3c1..a89085b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,9 +4,11 @@ services: stock-analyzer: build: . ports: - - "50684:8080" + - "8888:8888" environment: - - GEMINI_API_KEY=${GEMINI_API_KEY} + - API_KEY=${API_KEY} + - API_URL=${API_URL} + - API_MODEL=${API_MODEL} volumes: - .:/app restart: unless-stopped diff --git a/stock_analyzer.py b/stock_analyzer.py index 867a52b..815f4b2 100644 --- a/stock_analyzer.py +++ b/stock_analyzer.py @@ -18,8 +18,8 @@ class StockAnalyzer: load_dotenv() # 设置 Gemini API - self.gemini_api_url = os.getenv('GEMINI_API_URL') - self.gemini_api_key = os.getenv('GEMINI_API_KEY') + self.API_URL = os.getenv('API_URL') + self.API_KEY = os.getenv('API_KEY') # 配置参数 self.params = { @@ -243,17 +243,17 @@ class StockAnalyzer: """ headers = { - "Authorization": f"Bearer {self.gemini_api_key}", + "Authorization": f"Bearer {self.API_KEY}", "Content-Type": "application/json" } data = { - "model": os.getenv('GEMINI_API_MODEL'), + "model": os.getenv('API_MODEL'), "messages": [{"role": "user", "content": prompt}] } response = requests.post( - f"{self.gemini_api_url}/v1/chat/completions", + f"{self.API_URL}/v1/chat/completions", headers=headers, json=data, timeout=30 diff --git a/templates/index.html b/templates/index.html index 4b0b060..aee282d 100644 --- a/templates/index.html +++ b/templates/index.html @@ -10,6 +10,26 @@

股票分析系统

+ + {% if announcement %} +
+
+
+
+ + + +
+
+

+ {{ announcement }} +

+
+
+
+
+ {% endif %} +
diff --git a/web_server.py b/web_server.py index 2cb843c..31a6299 100644 --- a/web_server.py +++ b/web_server.py @@ -5,6 +5,7 @@ import threading import logging from logging.handlers import RotatingFileHandler import traceback +import os app = Flask(__name__) analyzer = StockAnalyzer() @@ -20,7 +21,9 @@ app.logger.addHandler(handler) @app.route('/') def index(): - return render_template('index.html') + # 如果环境变量不存在或为空,返回 None + announcement = os.getenv('ANNOUNCEMENT_TEXT') or None + return render_template('index.html', announcement=announcement) @app.route('/analyze', methods=['POST']) def analyze():