删除日志文件改为print方便docker调试、 API_URL处理逻辑变更、akshare保持最新版本号
This commit is contained in:
15
README.md
15
README.md
@@ -18,6 +18,21 @@ docker run -d \
|
||||
-e API_URL=替换为你的api地址 \
|
||||
-e API_MODEL=替换为你的模型 \
|
||||
lanzhihong/stock-scanner:latest
|
||||
|
||||
注意⚠️: 环境变量名变更,更新版本后需要调整!!!
|
||||
|
||||
针对API_URL处理兼容更多的api地址
|
||||
API_URL 处理逻辑说明:
|
||||
1. 当 API_URL 以 / 结尾时直接追加 chat/completions,保留原有版本号:
|
||||
示例:
|
||||
输入: https://ark.cn-beijing.volces.com/api/v3/
|
||||
输出: https://ark.cn-beijing.volces.com/api/v3/chat/completions
|
||||
|
||||
2. 当 API_URL 不以 / 结尾时使用默认版本号 v1:
|
||||
示例:
|
||||
输入: https://ark.cn-beijing.volces.com/api
|
||||
输出: https://ark.cn-beijing.volces.com/api/v1/chat/completions
|
||||
|
||||
```
|
||||
默认8888端口,部署完成后访问 http://127.0.0.1:8888 即可使用。
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ pandas==2.2.2
|
||||
scipy==1.15.1
|
||||
|
||||
# 数据获取和分析库
|
||||
akshare==1.15.87
|
||||
akshare
|
||||
tqdm==4.67.1
|
||||
|
||||
|
||||
|
||||
@@ -5,14 +5,9 @@ import os
|
||||
import requests
|
||||
from typing import Dict, List, Optional, Tuple
|
||||
from dotenv import load_dotenv
|
||||
import logging
|
||||
|
||||
class StockAnalyzer:
|
||||
def __init__(self, initial_cash=1000000):
|
||||
# 设置日志
|
||||
logging.basicConfig(level=logging.INFO,
|
||||
format='%(asctime)s - %(levelname)s - %(message)s')
|
||||
self.logger = logging.getLogger(__name__)
|
||||
|
||||
# 加载环境变量
|
||||
load_dotenv()
|
||||
@@ -93,7 +88,6 @@ class StockAnalyzer:
|
||||
return df.sort_values('date')
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"获取股票数据失败: {str(e)}")
|
||||
raise Exception(f"获取股票数据失败: {str(e)}")
|
||||
|
||||
def calculate_ema(self, series, period):
|
||||
@@ -173,7 +167,7 @@ class StockAnalyzer:
|
||||
return df
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"计算技术指标时出错: {str(e)}")
|
||||
print(f"计算技术指标时出错: {str(e)}")
|
||||
raise
|
||||
|
||||
def calculate_score(self, df):
|
||||
@@ -207,7 +201,7 @@ class StockAnalyzer:
|
||||
return score
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"计算评分时出错: {str(e)}")
|
||||
print(f"计算评分时出错: {str(e)}")
|
||||
raise
|
||||
|
||||
def get_ai_analysis(self, df, stock_code):
|
||||
@@ -252,13 +246,19 @@ class StockAnalyzer:
|
||||
"messages": [{"role": "user", "content": prompt}]
|
||||
}
|
||||
|
||||
if self.API_URL.endswith('/'):
|
||||
api_url = f"{self.API_URL}chat/completions"
|
||||
else:
|
||||
api_url = f"{self.API_URL}/v1/chat/completions"
|
||||
|
||||
response = requests.post(
|
||||
f"{self.API_URL}/v1/chat/completions",
|
||||
api_url,
|
||||
headers=headers,
|
||||
json=data,
|
||||
timeout=30
|
||||
timeout=60
|
||||
)
|
||||
print(headers)
|
||||
|
||||
print(api_url)
|
||||
print(data)
|
||||
print(response.json())
|
||||
|
||||
@@ -268,7 +268,7 @@ class StockAnalyzer:
|
||||
return "AI 分析暂时无法使用"
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"AI 分析发生错误: {str(e)}")
|
||||
print(f"AI 分析发生错误: {str(e)}")
|
||||
return "AI 分析过程中发生错误"
|
||||
|
||||
def get_recommendation(self, score):
|
||||
@@ -318,7 +318,7 @@ class StockAnalyzer:
|
||||
return report
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"分析股票时出错: {str(e)}")
|
||||
print(f"分析股票时出错: {str(e)}")
|
||||
raise
|
||||
|
||||
def scan_market(self, stock_list, min_score=60, market_type='A'):
|
||||
@@ -331,7 +331,7 @@ class StockAnalyzer:
|
||||
if report['score'] >= min_score:
|
||||
recommendations.append(report)
|
||||
except Exception as e:
|
||||
self.logger.error(f"分析股票 {stock_code} 时出错: {str(e)}")
|
||||
print(f"分析股票 {stock_code} 时出错: {str(e)}")
|
||||
continue
|
||||
|
||||
# 按得分排序
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
import akshare as ak
|
||||
import pandas as pd
|
||||
import logging
|
||||
|
||||
class USStockService:
|
||||
def __init__(self):
|
||||
logging.basicConfig(level=logging.INFO,
|
||||
format='%(asctime)s - %(levelname)s - %(message)s')
|
||||
self.logger = logging.getLogger(__name__)
|
||||
|
||||
def search_us_stocks(self, keyword):
|
||||
"""
|
||||
@@ -55,5 +50,4 @@ class USStockService:
|
||||
return formatted_results
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"搜索美股代码时出错: {str(e)}")
|
||||
raise Exception(f"搜索美股代码失败: {str(e)}")
|
||||
@@ -2,26 +2,14 @@ from flask import Flask, render_template, request, jsonify
|
||||
from stock_analyzer import StockAnalyzer
|
||||
from us_stock_service import USStockService
|
||||
import threading
|
||||
import logging
|
||||
from logging.handlers import RotatingFileHandler
|
||||
import traceback
|
||||
import os
|
||||
|
||||
app = Flask(__name__)
|
||||
analyzer = StockAnalyzer()
|
||||
us_stock_service = USStockService()
|
||||
|
||||
# 配置日志
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
handler = RotatingFileHandler('flask_app.log', maxBytes=10000000, backupCount=5)
|
||||
handler.setFormatter(logging.Formatter(
|
||||
'[%(asctime)s] %(levelname)s in %(module)s: %(message)s'
|
||||
))
|
||||
app.logger.addHandler(handler)
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
# 如果环境变量不存在或为空,返回 None
|
||||
announcement = os.getenv('ANNOUNCEMENT_TEXT') or None
|
||||
return render_template('index.html', announcement=announcement)
|
||||
|
||||
@@ -42,6 +30,7 @@ def analyze():
|
||||
|
||||
return jsonify({'results': results})
|
||||
except Exception as e:
|
||||
print(f"分析股票时出错: {str(e)}")
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
@app.route('/search_us_stocks', methods=['GET'])
|
||||
@@ -55,11 +44,11 @@ def search_us_stocks():
|
||||
return jsonify({'results': results})
|
||||
|
||||
except Exception as e:
|
||||
app.logger.error(f"搜索美股代码时出错: {str(e)}")
|
||||
print(f"搜索美股代码时出错: {str(e)}")
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
if __name__ == '__main__':
|
||||
# 将 host 设置为 '0.0.0.0' 使其支持所有网络接口访问
|
||||
app.run(host='0.0.0.0', port=8888, debug=True)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user