Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, Response
|
| 2 |
+
from flask_cors import CORS
|
| 3 |
+
import requests
|
| 4 |
+
import json
|
| 5 |
+
from datetime import datetime, timedelta
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
app = Flask(__name__)
|
| 9 |
+
CORS(app)
|
| 10 |
+
|
| 11 |
+
CHATANYWHERE_ENDPOINT = 'https://api.chatanywhere.tech'
|
| 12 |
+
|
| 13 |
+
class APIKeyManager:
|
| 14 |
+
def __init__(self):
|
| 15 |
+
self.api_keys = os.environ.get('API_KEYS', '').split(',')
|
| 16 |
+
self.daily_uses = {key: 0 for key in self.api_keys}
|
| 17 |
+
self.last_reset = datetime.now().date()
|
| 18 |
+
|
| 19 |
+
def reset_daily_uses(self):
|
| 20 |
+
today = datetime.now().date()
|
| 21 |
+
if today > self.last_reset:
|
| 22 |
+
self.daily_uses = {key: 0 for key in self.api_keys}
|
| 23 |
+
self.last_reset = today
|
| 24 |
+
|
| 25 |
+
def get_available_key(self):
|
| 26 |
+
self.reset_daily_uses()
|
| 27 |
+
for key in self.api_keys:
|
| 28 |
+
if self.daily_uses[key] < 200: # 假设每个密钥每天可用200次
|
| 29 |
+
self.daily_uses[key] += 1
|
| 30 |
+
return key
|
| 31 |
+
return None
|
| 32 |
+
|
| 33 |
+
def get_key_info(self):
|
| 34 |
+
self.reset_daily_uses()
|
| 35 |
+
return [{"key": key, "uses": uses} for key, uses in self.daily_uses.items()]
|
| 36 |
+
|
| 37 |
+
key_manager = APIKeyManager()
|
| 38 |
+
|
| 39 |
+
@app.route('/v1/models', methods=['GET'])
|
| 40 |
+
def list_models():
|
| 41 |
+
models = [
|
| 42 |
+
{
|
| 43 |
+
"id": "gpt-3.5-turbo",
|
| 44 |
+
"object": "model",
|
| 45 |
+
"created": 1677610602,
|
| 46 |
+
"owned_by": "openai",
|
| 47 |
+
"permission": [{
|
| 48 |
+
"id": "modelperm-M56FXnG1AsIr3SXq8BYPvXJA",
|
| 49 |
+
"object": "model_permission",
|
| 50 |
+
"created": 1679602088,
|
| 51 |
+
"allow_create_engine": False,
|
| 52 |
+
"allow_sampling": True,
|
| 53 |
+
"allow_logprobs": True,
|
| 54 |
+
"allow_search_indices": False,
|
| 55 |
+
"allow_view": True,
|
| 56 |
+
"allow_fine_tuning": False,
|
| 57 |
+
"organization": "*",
|
| 58 |
+
"group": None,
|
| 59 |
+
"is_blocking": False
|
| 60 |
+
}],
|
| 61 |
+
"root": "gpt-3.5-turbo",
|
| 62 |
+
"parent": None
|
| 63 |
+
}
|
| 64 |
+
]
|
| 65 |
+
return Response(json.dumps({"data": models, "object": "list"}), mimetype='application/json')
|
| 66 |
+
|
| 67 |
+
@app.route('/v1/chat/completions', methods=['POST'])
|
| 68 |
+
def proxy_request():
|
| 69 |
+
api_key = key_manager.get_available_key()
|
| 70 |
+
if not api_key:
|
| 71 |
+
return Response(json.dumps({"error": "No available API key"}), status=429, mimetype='application/json')
|
| 72 |
+
|
| 73 |
+
headers = {
|
| 74 |
+
'Authorization': f'Bearer {api_key}',
|
| 75 |
+
'Content-Type': 'application/json'
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
response = requests.post(f'{CHATANYWHERE_ENDPOINT}/v1/chat/completions', headers=headers, data=request.data)
|
| 79 |
+
return Response(response.content, status=response.status_code, mimetype='application/json')
|
| 80 |
+
|
| 81 |
+
@app.route('/key_info', methods=['GET'])
|
| 82 |
+
def key_info():
|
| 83 |
+
return Response(json.dumps(key_manager.get_key_info()), mimetype='application/json')
|
| 84 |
+
|
| 85 |
+
if __name__ == '__main__':
|
| 86 |
+
app.run(host='0.0.0.0', port=7860)
|