Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,8 @@
|
|
1 |
import os
|
2 |
-
from flask import Flask, request, jsonify
|
3 |
from functools import wraps
|
4 |
import requests
|
|
|
5 |
|
6 |
app = Flask(__name__)
|
7 |
|
@@ -9,7 +10,7 @@ AZURE_API_KEY = os.environ.get("AZURE_API_KEY")
|
|
9 |
AZURE_API_BASE = "https://openai-skyline-jp.openai.azure.com"
|
10 |
AZURE_API_VERSION = "2023-05-15"
|
11 |
AZURE_DEPLOYMENT_NAME = "GPT-4"
|
12 |
-
API_TOKEN = os.environ.get("API_TOKEN")
|
13 |
|
14 |
def token_required(f):
|
15 |
@wraps(f)
|
@@ -28,10 +29,31 @@ def token_required(f):
|
|
28 |
return f(*args, **kwargs)
|
29 |
return decorated
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
@app.route('/hf/v1/chat/completions', methods=['POST'])
|
32 |
@token_required
|
33 |
def chat_completions():
|
34 |
openai_data = request.json
|
|
|
35 |
|
36 |
azure_url = f"{AZURE_API_BASE}/openai/deployments/{AZURE_DEPLOYMENT_NAME}/chat/completions?api-version={AZURE_API_VERSION}"
|
37 |
|
@@ -40,23 +62,47 @@ def chat_completions():
|
|
40 |
"api-key": AZURE_API_KEY
|
41 |
}
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
"
|
52 |
-
"model": openai_data.get("model", "gpt-4"),
|
53 |
-
"choices": azure_response.get("choices", []),
|
54 |
-
"usage": azure_response.get("usage", {})
|
55 |
-
}
|
56 |
-
|
57 |
-
return jsonify(openai_response), 200
|
58 |
else:
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
if __name__ == "__main__":
|
62 |
app.run(host="0.0.0.0", port=7860)
|
|
|
1 |
import os
|
2 |
+
from flask import Flask, request, jsonify, Response, stream_with_context
|
3 |
from functools import wraps
|
4 |
import requests
|
5 |
+
import json
|
6 |
|
7 |
app = Flask(__name__)
|
8 |
|
|
|
10 |
AZURE_API_BASE = "https://openai-skyline-jp.openai.azure.com"
|
11 |
AZURE_API_VERSION = "2023-05-15"
|
12 |
AZURE_DEPLOYMENT_NAME = "GPT-4"
|
13 |
+
API_TOKEN = os.environ.get("API_TOKEN")
|
14 |
|
15 |
def token_required(f):
|
16 |
@wraps(f)
|
|
|
29 |
return f(*args, **kwargs)
|
30 |
return decorated
|
31 |
|
32 |
+
def convert_to_openai_format(azure_response):
|
33 |
+
openai_response = {
|
34 |
+
"id": azure_response.get("id", ""),
|
35 |
+
"object": "chat.completion",
|
36 |
+
"created": azure_response.get("created", 0),
|
37 |
+
"model": azure_response.get("model", "gpt-4"),
|
38 |
+
"choices": [],
|
39 |
+
"usage": azure_response.get("usage", {})
|
40 |
+
}
|
41 |
+
|
42 |
+
for choice in azure_response.get("choices", []):
|
43 |
+
openai_choice = {
|
44 |
+
"index": choice.get("index", 0),
|
45 |
+
"message": choice.get("message", {}),
|
46 |
+
"finish_reason": choice.get("finish_reason")
|
47 |
+
}
|
48 |
+
openai_response["choices"].append(openai_choice)
|
49 |
+
|
50 |
+
return {k: v for k, v in openai_response.items() if v is not None}
|
51 |
+
|
52 |
@app.route('/hf/v1/chat/completions', methods=['POST'])
|
53 |
@token_required
|
54 |
def chat_completions():
|
55 |
openai_data = request.json
|
56 |
+
stream = openai_data.get('stream', False)
|
57 |
|
58 |
azure_url = f"{AZURE_API_BASE}/openai/deployments/{AZURE_DEPLOYMENT_NAME}/chat/completions?api-version={AZURE_API_VERSION}"
|
59 |
|
|
|
62 |
"api-key": AZURE_API_KEY
|
63 |
}
|
64 |
|
65 |
+
if not stream:
|
66 |
+
response = requests.post(azure_url, json=openai_data, headers=headers)
|
67 |
+
|
68 |
+
if response.status_code == 200:
|
69 |
+
azure_response = response.json()
|
70 |
+
openai_response = convert_to_openai_format(azure_response)
|
71 |
+
return jsonify(openai_response), 200
|
72 |
+
else:
|
73 |
+
return jsonify({"error": "Azure OpenAI API request failed", "details": response.text}), response.status_code
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
else:
|
75 |
+
def generate():
|
76 |
+
with requests.post(azure_url, json=openai_data, headers=headers, stream=True) as response:
|
77 |
+
if response.status_code != 200:
|
78 |
+
yield f"data: {json.dumps({'error': 'Azure OpenAI API request failed', 'details': response.text})}\n\n"
|
79 |
+
return
|
80 |
+
|
81 |
+
for line in response.iter_lines():
|
82 |
+
if line:
|
83 |
+
azure_data = json.loads(line.decode('utf-8').replace('data: ', ''))
|
84 |
+
|
85 |
+
openai_data = {
|
86 |
+
"id": azure_data.get("id", ""),
|
87 |
+
"object": azure_data.get("object", ""),
|
88 |
+
"created": azure_data.get("created", 0),
|
89 |
+
"model": azure_data.get("model", "gpt-4"),
|
90 |
+
"choices": [
|
91 |
+
{
|
92 |
+
"index": choice.get("index", 0),
|
93 |
+
"delta": choice.get("delta", {}),
|
94 |
+
"finish_reason": choice.get("finish_reason")
|
95 |
+
} for choice in azure_data.get("choices", [])
|
96 |
+
]
|
97 |
+
}
|
98 |
+
|
99 |
+
openai_data = {k: v for k, v in openai_data.items() if v is not None}
|
100 |
+
|
101 |
+
yield f"data: {json.dumps(openai_data)}\n\n"
|
102 |
+
|
103 |
+
yield "data: [DONE]\n\n"
|
104 |
+
|
105 |
+
return Response(stream_with_context(generate()), content_type='text/event-stream')
|
106 |
|
107 |
if __name__ == "__main__":
|
108 |
app.run(host="0.0.0.0", port=7860)
|