yangtb24 commited on
Commit
ddaefda
1 Parent(s): 8f87b1b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -19
app.py CHANGED
@@ -1,23 +1,35 @@
1
- from flask import Flask, request, jsonify
 
 
 
2
 
3
- app = Flask(__name__)
 
 
4
 
5
- # 简单的示例函数
6
- @app.route("/")
7
- def index():
8
- return "Hello from Flask on Hugging Face!"
 
 
 
 
 
 
 
 
9
 
10
- # 接受 POST 请求并返回 JSON 响应的示例
11
- @app.route('/api', methods=['POST'])
12
- def api():
13
- data = request.get_json()
14
- if data is None:
15
- return jsonify({'error': 'Invalid JSON'}), 400
16
-
17
- # 在这里可以添加对数据的处理逻辑
18
- processed_data = data
19
-
20
- return jsonify({'message': 'Success', 'data': processed_data})
21
 
22
- if __name__ == "__main__":
23
- app.run(host="0.0.0.0", port=7860, debug=True)
 
 
 
1
+ import os
2
+ import time
3
+ import logging
4
+ from threading import Thread
5
 
6
+ # 配置日志记录器
7
+ logging.basicConfig(level=logging.INFO,
8
+ format='%(asctime)s - %(levelname)s - %(message)s')
9
 
10
+ def load_keys():
11
+ """
12
+ 从环境变量中加载 API 密钥,并在日志中输出。
13
+ """
14
+ keys_str = os.environ.get("KEYS")
15
+ if keys_str:
16
+ keys = [key.strip() for key in keys_str.split(',')]
17
+ logging.info("已加载的 API 密钥:")
18
+ for key in keys:
19
+ logging.info(key)
20
+ else:
21
+ logging.warning("未找到名为 KEYS 的环境变量")
22
 
23
+ def scheduled_task():
24
+ """
25
+ 每小时执行一次的定时任务。
26
+ """
27
+ while True:
28
+ load_keys()
29
+ # 休眠一小时
30
+ time.sleep(3600)
 
 
 
31
 
32
+ # 启动定时任务
33
+ thread = Thread(target=scheduled_task)
34
+ thread.daemon = True
35
+ thread.start()