tianlong12 commited on
Commit
6504035
·
verified ·
1 Parent(s): 812e31e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, Response, jsonify
2
+ import requests
3
+
4
+ app = Flask(__name__)
5
+
6
+ @app.route('/')
7
+ def index():
8
+ return "Hello, this is the root page of your Flask application!"
9
+
10
+ @app.route('/hf/v1/chat/completions', methods=['POST'])
11
+ def chat():
12
+ try:
13
+ # 从请求头中获取 API 密钥和基础 URL
14
+ auth_header = request.headers.get('Authorization')
15
+ if not auth_header or not auth_header.startswith('Bearer '):
16
+ return Response("Unauthorized", status=401)
17
+
18
+ api_key, base_url = auth_header.split(" ")[1:]
19
+
20
+ # 构建目标 URL
21
+ target_url = f"{base_url}/v1/chat/completions"
22
+
23
+ # 准备请求头
24
+ headers = {
25
+ 'Authorization': f'Bearer {api_key}',
26
+ 'Content-Type': 'application/json'
27
+ }
28
+
29
+ # 检查是否为流式请求
30
+ is_stream = request.json.get('stream', False)
31
+
32
+ # 转发请求
33
+ response = requests.post(
34
+ target_url,
35
+ headers=headers,
36
+ json=request.json,
37
+ stream=is_stream
38
+ )
39
+
40
+ if is_stream:
41
+ # 流式响应
42
+ def generate():
43
+ for chunk in response.iter_content(chunk_size=1024):
44
+ yield chunk
45
+
46
+ return Response(
47
+ generate(),
48
+ content_type=response.headers['Content-Type'],
49
+ status=response.status_code
50
+ )
51
+ else:
52
+ # 非流式响应
53
+ return Response(
54
+ response.content,
55
+ content_type=response.headers['Content-Type'],
56
+ status=response.status_code
57
+ )
58
+
59
+ except Exception as e:
60
+ print("Exception:", e)
61
+ return jsonify({"error": str(e)}), 500
62
+
63
+ if __name__ == "__main__":
64
+ app.run(host='0.0.0.0', port=7860, threaded=True)
65
+