Upload 4 files
Browse files- app.py +47 -9
- func.py +1 -2
- templates/login.html +20 -0
- templates/manage.html +33 -0
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
|
2 |
-
from flask import Flask, request, jsonify, Response, stream_with_context
|
|
|
3 |
import google.generativeai as genai
|
4 |
import json
|
5 |
from datetime import datetime
|
@@ -10,15 +11,16 @@ import func
|
|
10 |
|
11 |
os.environ['TZ'] = 'Asia/Shanghai'
|
12 |
app = Flask(__name__)
|
13 |
-
if 'API_KEYS' not in os.environ:
|
14 |
-
raise EnvironmentError("API_KEYS environment variable not set")
|
15 |
-
if 'HF_API_KEY' not in os.environ:
|
16 |
-
raise EnvironmentError("HF_API_KEY environment variable not set")
|
17 |
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
logger = logging.getLogger(__name__)
|
21 |
-
logger.setLevel(logging.INFO)
|
22 |
|
23 |
handler = logging.StreamHandler()
|
24 |
handler.setFormatter(formatter)
|
@@ -73,11 +75,47 @@ GEMINI_MODELS = [
|
|
73 |
{"id": "gemini-2.0-exp"},
|
74 |
{"id": "gemini-2.0-pro-exp"},
|
75 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
@app.route('/hf/v1/chat/completions', methods=['POST'])
|
78 |
def chat_completions():
|
79 |
global current_api_key
|
80 |
-
is_authenticated, auth_error, status_code = func.authenticate_request(request)
|
81 |
if not is_authenticated:
|
82 |
return auth_error if auth_error else jsonify({'error': 'Unauthorized'}), status_code if status_code else 401
|
83 |
try:
|
@@ -217,7 +255,7 @@ def chat_completions():
|
|
217 |
|
218 |
@app.route('/hf/v1/models', methods=['GET'])
|
219 |
def list_models():
|
220 |
-
is_authenticated, auth_error, status_code = func.authenticate_request(request)
|
221 |
if not is_authenticated:
|
222 |
return auth_error if auth_error else jsonify({'error': 'Unauthorized'}), status_code if status_code else 401
|
223 |
response = {"object": "list", "data": GEMINI_MODELS}
|
|
|
1 |
|
2 |
+
from flask import Flask, request, jsonify, Response, stream_with_context, render_template, session, redirect, url_for
|
3 |
+
|
4 |
import google.generativeai as genai
|
5 |
import json
|
6 |
from datetime import datetime
|
|
|
11 |
|
12 |
os.environ['TZ'] = 'Asia/Shanghai'
|
13 |
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
app.secret_key = os.urandom(24)
|
16 |
+
|
17 |
+
hf_api_keys = os.environ['HF_API_KEY'].split(',')
|
18 |
+
|
19 |
+
formatter = logging.Formatter('%(message)s')
|
20 |
+
ADMIN_PASSWORD = os.environ.get('AD_PASSWORD')
|
21 |
|
22 |
logger = logging.getLogger(__name__)
|
23 |
+
logger.setLevel(logging.INFO)
|
24 |
|
25 |
handler = logging.StreamHandler()
|
26 |
handler.setFormatter(formatter)
|
|
|
75 |
{"id": "gemini-2.0-exp"},
|
76 |
{"id": "gemini-2.0-pro-exp"},
|
77 |
]
|
78 |
+
# Login route
|
79 |
+
@app.route("/", methods=["GET", "POST"])
|
80 |
+
def login():
|
81 |
+
if request.method == "POST":
|
82 |
+
password = request.form.get("password")
|
83 |
+
if password == ADMIN_PASSWORD:
|
84 |
+
session["logged_in"] = True
|
85 |
+
return redirect(url_for("manage_keys"))
|
86 |
+
else:
|
87 |
+
return render_template("login.html", error="Incorrect password")
|
88 |
+
return render_template("login.html", error=None)
|
89 |
+
|
90 |
+
# Manage API keys route (requires authentication)
|
91 |
+
@app.route("/manage", methods=["GET", "POST"])
|
92 |
+
def manage_keys():
|
93 |
+
if not session.get("logged_in"):
|
94 |
+
return redirect(url_for("login"))
|
95 |
+
|
96 |
+
if request.method == "POST":
|
97 |
+
action = request.form.get("action")
|
98 |
+
if action == "add":
|
99 |
+
new_key = request.form.get("new_key")
|
100 |
+
if new_key:
|
101 |
+
hf_api_keys.append(new_key)
|
102 |
+
elif action == "delete":
|
103 |
+
key_to_delete = request.form.get("key_to_delete")
|
104 |
+
if key_to_delete in hf_api_keys:
|
105 |
+
hf_api_keys.remove(key_to_delete)
|
106 |
+
|
107 |
+
return render_template("manage.html", keys=hf_api_keys)
|
108 |
+
|
109 |
+
# Logout route
|
110 |
+
@app.route("/logout")
|
111 |
+
def logout():
|
112 |
+
session.pop("logged_in", None)
|
113 |
+
return redirect(url_for("login"))
|
114 |
|
115 |
@app.route('/hf/v1/chat/completions', methods=['POST'])
|
116 |
def chat_completions():
|
117 |
global current_api_key
|
118 |
+
is_authenticated, auth_error, status_code = func.authenticate_request(hf_api_keys, request)
|
119 |
if not is_authenticated:
|
120 |
return auth_error if auth_error else jsonify({'error': 'Unauthorized'}), status_code if status_code else 401
|
121 |
try:
|
|
|
255 |
|
256 |
@app.route('/hf/v1/models', methods=['GET'])
|
257 |
def list_models():
|
258 |
+
is_authenticated, auth_error, status_code = func.authenticate_request(hf_api_keys, request)
|
259 |
if not is_authenticated:
|
260 |
return auth_error if auth_error else jsonify({'error': 'Unauthorized'}), status_code if status_code else 401
|
261 |
response = {"object": "list", "data": GEMINI_MODELS}
|
func.py
CHANGED
@@ -22,9 +22,8 @@ GEMINI_MODELS = [
|
|
22 |
|
23 |
logger = logging.getLogger(__name__)
|
24 |
|
25 |
-
def authenticate_request(request):
|
26 |
auth_header = request.headers.get('Authorization')
|
27 |
-
hf_api_key = os.environ.get('HF_API_KEY').split(',')
|
28 |
|
29 |
if not auth_header:
|
30 |
return False, jsonify({'error': 'Authorization header is missing'}), 401
|
|
|
22 |
|
23 |
logger = logging.getLogger(__name__)
|
24 |
|
25 |
+
def authenticate_request(hf_api_key, request):
|
26 |
auth_header = request.headers.get('Authorization')
|
|
|
27 |
|
28 |
if not auth_header:
|
29 |
return False, jsonify({'error': 'Authorization header is missing'}), 401
|
templates/login.html
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html>
|
3 |
+
|
4 |
+
<head>
|
5 |
+
<title>Login</title>
|
6 |
+
</head>
|
7 |
+
|
8 |
+
<body>
|
9 |
+
<h1>Admin Login</h1>
|
10 |
+
{% if error %}
|
11 |
+
<p style="color: red;">{{ error }}</p>
|
12 |
+
{% endif %}
|
13 |
+
<form method="POST">
|
14 |
+
<label for="password">Password:</label>
|
15 |
+
<input type="password" name="password" id="password" required>
|
16 |
+
<button type="submit">Login</button>
|
17 |
+
</form>
|
18 |
+
</body>
|
19 |
+
|
20 |
+
</html>
|
templates/manage.html
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html>
|
3 |
+
|
4 |
+
<head>
|
5 |
+
<title>Manage API Keys</title>
|
6 |
+
</head>
|
7 |
+
|
8 |
+
<body>
|
9 |
+
<h1>Manage Hugging Face API Keys</h1>
|
10 |
+
<h2>Current Keys:</h2>
|
11 |
+
<ul>
|
12 |
+
{% for key in keys %}
|
13 |
+
<li>
|
14 |
+
{{ key }}
|
15 |
+
<form method="POST" style="display: inline;">
|
16 |
+
<input type="hidden" name="action" value="delete">
|
17 |
+
<input type="hidden" name="key_to_delete" value="{{ key }}">
|
18 |
+
<button type="submit">Delete</button>
|
19 |
+
</form>
|
20 |
+
</li>
|
21 |
+
{% endfor %}
|
22 |
+
</ul>
|
23 |
+
<h2>Add New Key:</h2>
|
24 |
+
<form method="POST">
|
25 |
+
<input type="hidden" name="action" value="add">
|
26 |
+
<input type="text" name="new_key" placeholder="Enter new API key">
|
27 |
+
<button type="submit">Add</button>
|
28 |
+
</form>
|
29 |
+
<br>
|
30 |
+
<a href="{{ url_for('logout') }}">Logout</a>
|
31 |
+
</body>
|
32 |
+
|
33 |
+
</html>
|