Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
app = Quart(__name__)
|
12 |
+
|
13 |
+
def decode_unicode_escape(input_text):
|
14 |
+
try:
|
15 |
+
decoded_json = json.loads('{"content": ' + input_text + '}')
|
16 |
+
content = decoded_json["content"].encode("utf-8").decode("unicode-escape")
|
17 |
+
return content.encode().decode('unicode_escape')
|
18 |
+
except Exception as e:
|
19 |
+
return str(e)
|
20 |
+
|
21 |
+
async def get_completion(model, prompt):
|
22 |
+
response = await ChatCompletion.create_async(
|
23 |
+
model=model,
|
24 |
+
messages = ast.literal_eval(f"[{prompt}]")
|
25 |
+
#messages=[{"role": "user", "content": prompt}],
|
26 |
+
)
|
27 |
+
return response
|
28 |
+
|
29 |
+
@app.route('/chat/completions', methods=['POST'])
|
30 |
+
async def chat_completions():
|
31 |
+
try:
|
32 |
+
app.config['QUART_ROUTE_TIMEOUT'] = 180 # Устанавливаем таймаут в 180 секунд
|
33 |
+
app.config["JSON_AS_ASCII"] = False
|
34 |
+
app.config["JSONIFY_MIMETYPE"] = "application/json; charset=utf-8"
|
35 |
+
|
36 |
+
# Получаем данные из POST-запроса
|
37 |
+
request_data = await request.get_data()
|
38 |
+
request_data_str = request_data.decode('utf-8')
|
39 |
+
|
40 |
+
# Извлекаем значение из JSON данных
|
41 |
+
request_json = json.loads(request_data_str)
|
42 |
+
user_message = request_json.get("message", "")
|
43 |
+
model = request_json.get("model", "")
|
44 |
+
password = request_json.get("password", "")
|
45 |
+
|
46 |
+
# Проверяем код пароля
|
47 |
+
if password != "P@ssw0rd":
|
48 |
+
return jsonify({"error": "Unauthorized", "message": "Неверный код пароля"}), 401
|
49 |
+
|
50 |
+
response = await get_completion(model, user_message)
|
51 |
+
|
52 |
+
completion_id = random.randint(1, 100)
|
53 |
+
completion_timestamp = '2022-01-01 12:00:00'
|
54 |
+
data = {
|
55 |
+
"id": f"chatcmpl-{completion_id}",
|
56 |
+
"object": "chat.completion",
|
57 |
+
"created": completion_timestamp,
|
58 |
+
"model": model,
|
59 |
+
"choices": [
|
60 |
+
{
|
61 |
+
"index": 0,
|
62 |
+
"message": {
|
63 |
+
"role": "assistant",
|
64 |
+
"content": response,
|
65 |
+
},
|
66 |
+
"finish_reason": "stop",
|
67 |
+
}
|
68 |
+
],
|
69 |
+
"usage": {
|
70 |
+
"prompt_tokens": None,
|
71 |
+
"completion_tokens": None,
|
72 |
+
"total_tokens": None,
|
73 |
+
}
|
74 |
+
}
|
75 |
+
|
76 |
+
headers = {'Content-Type': 'application/json; charset=utf-8'}
|
77 |
+
|
78 |
+
# Преобразуйте данные в JSON и укажите кодировку UTF-8
|
79 |
+
response = json.dumps(data, ensure_ascii=False).encode('utf-8')
|
80 |
+
|
81 |
+
# Создайте объект Response с указанием заголовков и кодировки
|
82 |
+
return app.response_class(response, content_type='application/json; charset=utf-8')
|
83 |
+
|
84 |
+
except Exception as e:
|
85 |
+
# Вывести ошибку в консоль для отладки
|
86 |
+
# print(f"Error: {str(e)}")
|
87 |
+
# Вернуть ошибку 500 с сообщением об ошибке
|
88 |
+
return jsonify({"error": "Internal Server Error", "message": str(e)}), 500
|
89 |
+
|
90 |
+
if __name__ == '__main__':
|
91 |
+
app.run(host='0.0.0.0', port=5000, debug=True)
|