-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
289 lines (260 loc) · 11 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
from flask import Flask, request, jsonify, Response # Ensure jsonify is imported
from flask_cors import CORS
from openai import OpenAI
import json
from modifyUsers import modifyUserFunc
from checkCardPIN import checkCardPINFunc
from sys import argv
from os import path
# todo: change the directory of the config file
thisDir = path.dirname(path.realpath(argv[0]))
configFile = path.join(thisDir, "config.json")
cardPINFile = path.join(thisDir, "cardPIN.json")
fileLogPath = path.join(thisDir, "fileLog.csv")
with open("config.json", "r", encoding="utf-8") as file:
config = json.load(file)
api_key_get = config.get("backend", {}).get("api-key", "api-key-error")
api_key_get = str(api_key_get)
port_default = config.get("backend", {}).get("port", "5000") # Default port
model_default = config.get("backend", {}).get(
"model", "qwen-plus-0806"
) # Default model
app = Flask(__name__)
PRICE_LEVEL = 100
PRICE_INPUT_PER_THOUSAND_TURBO = 0.0003 / 1000 * PRICE_LEVEL
PRICE_INPUT_PER_THOUSAND_PLUS = 0.0008 / 1000 * PRICE_LEVEL
PRICE_INPUT_PER_THOUSAND_MAX = 0.02 / 1000 * PRICE_LEVEL
PRICE_OUTPUT_PER_THOUSAND_PLUS = 0.0008 / 1000 * PRICE_LEVEL
PRICE_OUTPUT_PER_THOUSAND_MAX = 0.06 / 1000 * PRICE_LEVEL
PRICE_OUTPUT_PER_THOUSAND_TURBO = 0.0006 / 1000 * PRICE_LEVEL
# THIS IS NOT FOR SAFETY USE.
# YOU SHOULD USE A WHITELIST TO CONTROL ACCESS.
CORS(app, origins="*") # Allow all origins
# This allows cross-origin requests from any domain, suitable for development purposes.
# Even if origins are restricted to trusted domains, non-browser clients (e.g., bots, scripts)
# can still access the server. CORS only controls browser-based cross-origin requests
# and does not prevent unauthorized or malicious access.
# For better security, implement additional measures like authentication, IP restrictions, or rate limiting.
with app.app_context():
client = OpenAI(
api_key=api_key_get,
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)
@app.route("/chat", methods=["POST"])
def chat():
try:
data = request.json
userAccount = data.get("userAccount")
userPassword = data.get("userPassword")
if (
userAccount is None
or userAccount == ""
or userPassword is None
or userPassword == ""
):
return jsonify({"error": "手机号或密码不能为空"}), 400
dataJudgment = {
"todo": "isAuthored",
"userAccount": userAccount,
"userPassword": userPassword,
}
try:
returnValue = modifyUserFunc(dataJudgment)
except Exception as e:
return jsonify({"error": str(e)}), 501
if returnValue is None or returnValue == False:
return jsonify({"error": "账号或密码错误"}), 401
messages = data.get("messages")
systemContent = data.get("systemContent")
model_get = data.get("model")
if model_get is None or model_get == "":
model_set = model_default
print(
"no model set has been recieved, use default model: " + str(model_set)
)
else:
model_set = model_get
print("model get is: " + str(model_set))
if "turbo" in model_set:
priceInput = PRICE_INPUT_PER_THOUSAND_TURBO
priceOutput = PRICE_OUTPUT_PER_THOUSAND_TURBO
elif "plus" in model_set:
priceInput = PRICE_INPUT_PER_THOUSAND_PLUS
priceOutput = PRICE_OUTPUT_PER_THOUSAND_PLUS
else:
priceInput = PRICE_INPUT_PER_THOUSAND_MAX
priceOutput = PRICE_OUTPUT_PER_THOUSAND_MAX
send_message = [
{
"role": "system",
"content": systemContent,
},
{
"role": "user",
"content": messages,
},
]
completion = client.chat.completions.create(
model=model_set,
messages=send_message,
stream=False,
response_format={"type": "json_object"},
)
def generate():
if completion:
print(completion.model_dump_json())
print("completed as above")
# input tokens
promptTokens = completion.usage.prompt_tokens
# output tokens
completionTokens = completion.usage.completion_tokens
totalPrice = promptTokens * priceInput + completionTokens * priceOutput
data = {
"todo": "findUserField",
"field": "currentBalance",
"userAccount": userAccount,
"userPassword": userPassword,
}
# 添加余额显示功能和更新逻辑:在主窗口中显示当前余额,并实现从后端获取余额的功能
currentBalance = modifyUserFunc(data)
# 如果不能赊账,就直接返回,把这部分注释去掉
# 如果允许赊账,就无需修改
# if currentBalance < totalPrice:
# return jsonify({"error": "余额不足"}), 401
data = {
"todo": "addUsage",
"userAccount": userAccount,
"userPassword": userPassword,
"addNum": totalPrice,
}
modifyUserFunc(data)
return completion.choices[0].message.content
# 调用生成器之前,先判断余额是否足够
data = {
"todo": "findUserField",
"field": "currentBalance",
"userAccount": userAccount,
"userPassword": userPassword,
}
if modifyUserFunc(data) < 0:
return jsonify({"error": "余额不足"}), 401
# print(completion.model_dump_json())
return Response(generate(), content_type="text/plain")
except Exception as e:
return jsonify({"error": str(e)}), 502
@app.route("/register", methods=["POST"])
def register():
data = request.json
userAccount = data.get("userAccount")
userPassword = data.get("userPassword")
if not userAccount or not userPassword:
return jsonify({"error": "手机号或密码不能为空"}), 400
data["todo"] = "findUserField"
data["field"] = "userAccount"
# Check if user already exists
existing_user = modifyUserFunc(data)
# print("is the user existed?:" + str(existing_user))
if existing_user is not None and existing_user != "":
return jsonify({"error": "用户已存在"}), 400
# Register new user
new_user = {"todo": "addUser", "userAccount": userAccount, "userPassword": userPassword}
result = modifyUserFunc(new_user)
if result.get("UID") is not None or result.get("UID") != "":
return jsonify({"message": "注册成功", "UID": result.get("UID")}), 201
else:
return jsonify({"error": "注册失败"}), 503
@app.route("/login", methods=["POST"])
def login():
dataFromWeb = request.json
dataFromWeb["todo"] = "isAuthored"
try:
returnValue = modifyUserFunc(dataFromWeb)
except Exception as e:
return jsonify({"error": str(e)}), 504
if returnValue:
# token for keep login status
# dataFromWeb["todo"] = "generateUserTempToken"
# expirationTime = 24 * 14 # 14 days
# dataFromWeb["expirationTime"] = expirationTime
# token = modifyUserFunc(dataFromWeb)
return (
jsonify(
{
"message": "登录成功",
# "userTempToken": token,
# "expirationTime": expirationTime,
}
),
200,
)
else:
return jsonify({"error": "账号或密码错误"}), 401
@app.route("/charge", methods=["POST"])
def charge():
data = request.json
userAccount = data.get("userAccount")
if userAccount is None or userAccount == "":
return jsonify({"error": "手机号不能为空"}), 400
todo = data.get("todo")
if todo == "getBalance":
data["todo"] = "findUserField"
data["field"] = "currentBalance"
currentBalance = modifyUserFunc(data)
print("currentBalance: " + str(currentBalance))
return jsonify({"currentBalance": float(currentBalance)}), 200
if todo == "charge":
print("now is in charge")
cardPIN = data.get("cardPIN")
if cardPIN is None or cardPIN == "":
return jsonify({"error": "充值码不能为空"}), 400
value1, value2, value3 = checkCardPINFunc(cardPIN)
if value1 == -1:
return jsonify({"error": "无效:" + str(value2)}), 400
if value1 == 0 or value1 == 1 or value1 == 2:
data["todo"] = "findUserField"
data["field"] = "UID"
UID = modifyUserFunc(data)
data["UID"] = UID
if UID == "" or UID is None:
print("UID is None")
return jsonify({"error": "错误 找不到UID"}), 400
if value1 == 0:
# only this UID is valid
if UID != value3:
print("UID is not equal to value3")
return jsonify({"error": "无效充值码"}), 400
elif value1 == 1 or value1 == 2:
# all UIDs are valid
pass
else:
return jsonify({"error": "无效充值码"}), 400
data["todo"] = "rechargeAccount"
value2 = int(value2)
data["addNum"] = value2
try:
returnValue = modifyUserFunc(data)
except Exception as e:
print("rechargeAccount error" + str(e))
return jsonify({"error": str(e)}), 505
if returnValue != 0: # 不等于原来的余额就是成功
parsed_price = value2
# delete the card PIN from the database
with open("cardPIN.json", "r") as f:
data = json.load(f)
dataPrice = data.get(str(parsed_price))
# Assume price_level is the price level you determined earlier
if dataPrice:
for pin in dataPrice:
if pin == cardPIN:
value2 = 0
# delete the card PIN from the database
dataPrice.remove(pin)
data[str(parsed_price)] = dataPrice
with open("cardPIN.json", "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=4)
break
return jsonify({"message": "充值成功"}), 200
else:
return jsonify({"error": "充值失败"}), 506
if __name__ == "__main__":
app.run(host="0.0.0.0", port=port_default)