Spaces:
Paused
Paused
Upload 4 files
Browse files
app.py
CHANGED
|
@@ -14,8 +14,6 @@ app = Flask(__name__)
|
|
| 14 |
|
| 15 |
app.secret_key = os.urandom(24)
|
| 16 |
|
| 17 |
-
PASSWORD = os.environ['password']
|
| 18 |
-
|
| 19 |
formatter = logging.Formatter('%(message)s')
|
| 20 |
|
| 21 |
logger = logging.getLogger(__name__)
|
|
@@ -76,7 +74,7 @@ GEMINI_MODELS = [
|
|
| 76 |
]
|
| 77 |
@app.route('/')
|
| 78 |
def index():
|
| 79 |
-
main_content = "Moonfanz Gemini"
|
| 80 |
html_template = """
|
| 81 |
<!DOCTYPE html>
|
| 82 |
<html>
|
|
@@ -116,7 +114,7 @@ function copyLink(event) {
|
|
| 116 |
@app.route('/hf/v1/chat/completions', methods=['POST'])
|
| 117 |
def chat_completions():
|
| 118 |
global current_api_key
|
| 119 |
-
is_authenticated, auth_error, status_code = func.authenticate_request(
|
| 120 |
if not is_authenticated:
|
| 121 |
return auth_error if auth_error else jsonify({'error': 'Unauthorized'}), status_code if status_code else 401
|
| 122 |
try:
|
|
@@ -248,14 +246,13 @@ def chat_completions():
|
|
| 248 |
|
| 249 |
@app.route('/hf/v1/models', methods=['GET'])
|
| 250 |
def list_models():
|
| 251 |
-
is_authenticated, auth_error, status_code = func.authenticate_request(
|
| 252 |
if not is_authenticated:
|
| 253 |
return auth_error if auth_error else jsonify({'error': 'Unauthorized'}), status_code if status_code else 401
|
| 254 |
response = {"object": "list", "data": GEMINI_MODELS}
|
| 255 |
return jsonify(response)
|
| 256 |
|
| 257 |
def keep_alive():
|
| 258 |
-
""" 定期向应用自身发送请求,保持活跃 """
|
| 259 |
try:
|
| 260 |
response = requests.get("http://127.0.0.1:7860/", timeout=10)
|
| 261 |
response.raise_for_status()
|
|
@@ -265,8 +262,7 @@ def keep_alive():
|
|
| 265 |
|
| 266 |
if __name__ == '__main__':
|
| 267 |
scheduler = BackgroundScheduler()
|
| 268 |
-
# 设置定时任务,每 10 分钟执行一次 keep_alive 函数
|
| 269 |
scheduler.add_job(keep_alive, 'interval', hours = 12)
|
| 270 |
-
|
| 271 |
scheduler.start()
|
| 272 |
app.run(debug=True, host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))
|
|
|
|
| 14 |
|
| 15 |
app.secret_key = os.urandom(24)
|
| 16 |
|
|
|
|
|
|
|
| 17 |
formatter = logging.Formatter('%(message)s')
|
| 18 |
|
| 19 |
logger = logging.getLogger(__name__)
|
|
|
|
| 74 |
]
|
| 75 |
@app.route('/')
|
| 76 |
def index():
|
| 77 |
+
main_content = "Moonfanz Gemini"
|
| 78 |
html_template = """
|
| 79 |
<!DOCTYPE html>
|
| 80 |
<html>
|
|
|
|
| 114 |
@app.route('/hf/v1/chat/completions', methods=['POST'])
|
| 115 |
def chat_completions():
|
| 116 |
global current_api_key
|
| 117 |
+
is_authenticated, auth_error, status_code = func.authenticate_request(request)
|
| 118 |
if not is_authenticated:
|
| 119 |
return auth_error if auth_error else jsonify({'error': 'Unauthorized'}), status_code if status_code else 401
|
| 120 |
try:
|
|
|
|
| 246 |
|
| 247 |
@app.route('/hf/v1/models', methods=['GET'])
|
| 248 |
def list_models():
|
| 249 |
+
is_authenticated, auth_error, status_code = func.authenticate_request(request)
|
| 250 |
if not is_authenticated:
|
| 251 |
return auth_error if auth_error else jsonify({'error': 'Unauthorized'}), status_code if status_code else 401
|
| 252 |
response = {"object": "list", "data": GEMINI_MODELS}
|
| 253 |
return jsonify(response)
|
| 254 |
|
| 255 |
def keep_alive():
|
|
|
|
| 256 |
try:
|
| 257 |
response = requests.get("http://127.0.0.1:7860/", timeout=10)
|
| 258 |
response.raise_for_status()
|
|
|
|
| 262 |
|
| 263 |
if __name__ == '__main__':
|
| 264 |
scheduler = BackgroundScheduler()
|
|
|
|
| 265 |
scheduler.add_job(keep_alive, 'interval', hours = 12)
|
| 266 |
+
|
| 267 |
scheduler.start()
|
| 268 |
app.run(debug=True, host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))
|
func.py
CHANGED
|
@@ -5,25 +5,27 @@ from flask import jsonify
|
|
| 5 |
import logging
|
| 6 |
import json
|
| 7 |
import re
|
| 8 |
-
|
| 9 |
logger = logging.getLogger(__name__)
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
auth_header = request.headers.get('Authorization')
|
| 13 |
|
| 14 |
if not auth_header:
|
| 15 |
-
return False, jsonify({'error': 'Authorization
|
| 16 |
|
| 17 |
try:
|
| 18 |
auth_type, pass_word = auth_header.split(' ', 1)
|
| 19 |
except ValueError:
|
| 20 |
-
return False, jsonify({'error': '
|
| 21 |
|
| 22 |
if auth_type.lower() != 'bearer':
|
| 23 |
-
return False, jsonify({'error': 'Authorization
|
| 24 |
|
| 25 |
if pass_word != password:
|
| 26 |
-
return False, jsonify({'error': '
|
| 27 |
|
| 28 |
return True, None, None
|
| 29 |
|
|
|
|
| 5 |
import logging
|
| 6 |
import json
|
| 7 |
import re
|
| 8 |
+
import os
|
| 9 |
logger = logging.getLogger(__name__)
|
| 10 |
|
| 11 |
+
password = os.environ.get('password')
|
| 12 |
+
|
| 13 |
+
def authenticate_request(request):
|
| 14 |
auth_header = request.headers.get('Authorization')
|
| 15 |
|
| 16 |
if not auth_header:
|
| 17 |
+
return False, jsonify({'error': '缺少Authorization请求头'}), 401
|
| 18 |
|
| 19 |
try:
|
| 20 |
auth_type, pass_word = auth_header.split(' ', 1)
|
| 21 |
except ValueError:
|
| 22 |
+
return False, jsonify({'error': 'Authorization请求头格式错误'}), 401
|
| 23 |
|
| 24 |
if auth_type.lower() != 'bearer':
|
| 25 |
+
return False, jsonify({'error': 'Authorization类型必须为Bearer'}), 401
|
| 26 |
|
| 27 |
if pass_word != password:
|
| 28 |
+
return False, jsonify({'error': '未授权'}), 401
|
| 29 |
|
| 30 |
return True, None, None
|
| 31 |
|