Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,15 @@
|
|
1 |
import asyncio
|
2 |
-
#asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
|
3 |
-
|
4 |
from quart import Quart, request, jsonify
|
5 |
import random
|
6 |
from g4f import ChatCompletion
|
7 |
-
|
8 |
import json
|
9 |
import ast
|
10 |
-
|
11 |
import re
|
|
|
12 |
|
13 |
app = Quart(__name__)
|
14 |
|
|
|
15 |
def decode_unicode_escape(input_text):
|
16 |
try:
|
17 |
decoded_json = json.loads('{"content": ' + input_text + '}')
|
@@ -39,7 +37,7 @@ async def get_completion(model, prompt):
|
|
39 |
@app.route('/chat/completions', methods=['POST'])
|
40 |
async def chat_completions():
|
41 |
try:
|
42 |
-
app.config['QUART_ROUTE_TIMEOUT'] = 180
|
43 |
app.config["JSON_AS_ASCII"] = False
|
44 |
app.config["JSONIFY_MIMETYPE"] = "application/json; charset=utf-8"
|
45 |
|
@@ -83,19 +81,19 @@ async def chat_completions():
|
|
83 |
}
|
84 |
}
|
85 |
|
86 |
-
|
87 |
-
|
88 |
-
# Преобразуйте данные в JSON и укажите кодировку UTF-8
|
89 |
response = json.dumps(data, ensure_ascii=False).encode('utf-8')
|
|
|
90 |
|
91 |
# Создайте объект Response с указанием заголовков и кодировки
|
92 |
return app.response_class(response, content_type='application/json; charset=utf-8')
|
93 |
|
|
|
|
|
|
|
94 |
except Exception as e:
|
95 |
-
# Вывести ошибку в консоль для отладки
|
96 |
-
# print(f"Error: {str(e)}")
|
97 |
# Вернуть ошибку 500 с сообщением об ошибке
|
98 |
return jsonify({"error": "Internal Server Error", "message": str(e)}), 500
|
99 |
|
100 |
if __name__ == '__main__':
|
101 |
-
app.run(host='0.0.0.0', port=7860, debug=True)
|
|
|
1 |
import asyncio
|
|
|
|
|
2 |
from quart import Quart, request, jsonify
|
3 |
import random
|
4 |
from g4f import ChatCompletion
|
|
|
5 |
import json
|
6 |
import ast
|
|
|
7 |
import re
|
8 |
+
import html
|
9 |
|
10 |
app = Quart(__name__)
|
11 |
|
12 |
+
|
13 |
def decode_unicode_escape(input_text):
|
14 |
try:
|
15 |
decoded_json = json.loads('{"content": ' + input_text + '}')
|
|
|
37 |
@app.route('/chat/completions', methods=['POST'])
|
38 |
async def chat_completions():
|
39 |
try:
|
40 |
+
app.config['QUART_ROUTE_TIMEOUT'] = 180
|
41 |
app.config["JSON_AS_ASCII"] = False
|
42 |
app.config["JSONIFY_MIMETYPE"] = "application/json; charset=utf-8"
|
43 |
|
|
|
81 |
}
|
82 |
}
|
83 |
|
84 |
+
# Экранирование HTML-символов в JSON-данных
|
|
|
|
|
85 |
response = json.dumps(data, ensure_ascii=False).encode('utf-8')
|
86 |
+
response = html.escape(response.decode('utf-8'))
|
87 |
|
88 |
# Создайте объект Response с указанием заголовков и кодировки
|
89 |
return app.response_class(response, content_type='application/json; charset=utf-8')
|
90 |
|
91 |
+
except json.JSONDecodeError as e:
|
92 |
+
return jsonify({"error": "Bad Request", "message": "Неверный формат JSON в запросе"}), 400
|
93 |
+
|
94 |
except Exception as e:
|
|
|
|
|
95 |
# Вернуть ошибку 500 с сообщением об ошибке
|
96 |
return jsonify({"error": "Internal Server Error", "message": str(e)}), 500
|
97 |
|
98 |
if __name__ == '__main__':
|
99 |
+
app.run(host='0.0.0.0', port=7860, debug=True)
|