Spaces:
Building
Building
Upload 4 files
Browse files- app.py +12 -6
- func.py +0 -20
- requirements.txt +2 -1
app.py
CHANGED
@@ -8,6 +8,7 @@ import func
|
|
8 |
from apscheduler.schedulers.background import BackgroundScheduler
|
9 |
import requests
|
10 |
import time
|
|
|
11 |
|
12 |
os.environ['TZ'] = 'Asia/Shanghai'
|
13 |
app = Flask(__name__)
|
@@ -24,6 +25,12 @@ handler.setFormatter(formatter)
|
|
24 |
|
25 |
logger.addHandler(handler)
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
safety_settings = [
|
28 |
{
|
29 |
"category": "HARM_CATEGORY_HARASSMENT",
|
@@ -111,12 +118,11 @@ function copyLink(event) {
|
|
111 |
</html>
|
112 |
"""
|
113 |
return render_template_string(html_template, main_content=main_content)
|
|
|
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': '未授权'}), status_code if status_code else 401
|
120 |
try:
|
121 |
request_data = request.get_json()
|
122 |
messages = request_data.get('messages', [])
|
@@ -244,11 +250,11 @@ def chat_completions():
|
|
244 |
}
|
245 |
}), 500
|
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': '未授权'}), status_code if status_code else 401
|
252 |
response = {"object": "list", "data": GEMINI_MODELS}
|
253 |
return jsonify(response)
|
254 |
|
|
|
8 |
from apscheduler.schedulers.background import BackgroundScheduler
|
9 |
import requests
|
10 |
import time
|
11 |
+
from flask_httpauth import HTTPBasicAuth
|
12 |
|
13 |
os.environ['TZ'] = 'Asia/Shanghai'
|
14 |
app = Flask(__name__)
|
|
|
25 |
|
26 |
logger.addHandler(handler)
|
27 |
|
28 |
+
auth = HTTPBasicAuth()
|
29 |
+
|
30 |
+
@auth.verify_password
|
31 |
+
def verify_password(username, password):
|
32 |
+
return password == os.environ.get('password')
|
33 |
+
|
34 |
safety_settings = [
|
35 |
{
|
36 |
"category": "HARM_CATEGORY_HARASSMENT",
|
|
|
118 |
</html>
|
119 |
"""
|
120 |
return render_template_string(html_template, main_content=main_content)
|
121 |
+
|
122 |
@app.route('/hf/v1/chat/completions', methods=['POST'])
|
123 |
+
@auth.login_required
|
124 |
def chat_completions():
|
125 |
global current_api_key
|
|
|
|
|
|
|
126 |
try:
|
127 |
request_data = request.get_json()
|
128 |
messages = request_data.get('messages', [])
|
|
|
250 |
}
|
251 |
}), 500
|
252 |
|
253 |
+
|
254 |
+
|
255 |
@app.route('/hf/v1/models', methods=['GET'])
|
256 |
+
@auth.login_required
|
257 |
def list_models():
|
|
|
|
|
|
|
258 |
response = {"object": "list", "data": GEMINI_MODELS}
|
259 |
return jsonify(response)
|
260 |
|
func.py
CHANGED
@@ -8,26 +8,6 @@ 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 |
-
try:
|
19 |
-
auth_type, pass_word = auth_header.split(' ', 1)
|
20 |
-
except ValueError:
|
21 |
-
return False, jsonify({'error': 'Authorization请求头格式错误'}), 401
|
22 |
-
|
23 |
-
if auth_type.lower() != 'bearer':
|
24 |
-
return False, jsonify({'error': 'Authorization类型必须为Bearer'}), 401
|
25 |
-
|
26 |
-
if pass_word != password:
|
27 |
-
return False, jsonify({'error': '未授权'}), 401
|
28 |
-
|
29 |
-
return True, None, None
|
30 |
-
|
31 |
def process_messages_for_gemini(messages):
|
32 |
|
33 |
gemini_history = []
|
|
|
8 |
import os
|
9 |
logger = logging.getLogger(__name__)
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
def process_messages_for_gemini(messages):
|
12 |
|
13 |
gemini_history = []
|
requirements.txt
CHANGED
@@ -5,4 +5,5 @@ Werkzeug==2.0.3
|
|
5 |
google==3.0.0
|
6 |
google-generativeai==0.8.3
|
7 |
pillow==10.4.0
|
8 |
-
apscheduler
|
|
|
|
5 |
google==3.0.0
|
6 |
google-generativeai==0.8.3
|
7 |
pillow==10.4.0
|
8 |
+
apscheduler
|
9 |
+
Flask-HTTPAuth==4.8.0
|