Spaces:
Sleeping
Sleeping
Junhui Ji
commited on
Commit
·
b564c7d
1
Parent(s):
8fae477
update user_key dialogue
Browse files
main.py
CHANGED
@@ -21,7 +21,7 @@ import uvicorn
|
|
21 |
from collections import defaultdict
|
22 |
from PIL import Image
|
23 |
from Crypto.PublicKey import RSA
|
24 |
-
from Crypto.Cipher import
|
25 |
from datetime import datetime
|
26 |
from pymongo.mongo_client import MongoClient
|
27 |
from pymongo.server_api import ServerApi
|
@@ -213,17 +213,28 @@ async def analyze_feedback(request: AnalysisRequest):
|
|
213 |
logging.error(f'Error: {e}, traceback: {traceback.format_exc()}')
|
214 |
raise HTTPException(status_code=500, detail=f'Error: {e}, traceback: {traceback.format_exc()}')
|
215 |
|
216 |
-
|
|
|
217 |
try:
|
218 |
-
|
219 |
-
cipher =
|
220 |
-
|
221 |
-
#
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
227 |
except Exception as e:
|
228 |
logging.error(f'Error decrypting user key: {e}')
|
229 |
return None
|
@@ -282,15 +293,8 @@ async def optimize_design(request: OptimizationRequest):
|
|
282 |
# 检查用户密钥
|
283 |
user_key = None
|
284 |
if request.user_key:
|
285 |
-
|
286 |
-
if not
|
287 |
-
raise HTTPException(
|
288 |
-
status_code=400,
|
289 |
-
detail="无效的用户密钥"
|
290 |
-
)
|
291 |
-
try:
|
292 |
-
user_info = eval(user_key)
|
293 |
-
except:
|
294 |
raise HTTPException(
|
295 |
status_code=400,
|
296 |
detail="无效的用户密钥"
|
@@ -340,12 +344,15 @@ async def optimize_design(request: OptimizationRequest):
|
|
340 |
request_model_id=request.request_model_id,
|
341 |
openai_key=request.openai_key
|
342 |
)
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
|
|
|
|
|
|
349 |
|
350 |
return JSONResponse(response)
|
351 |
except HTTPException as he:
|
|
|
21 |
from collections import defaultdict
|
22 |
from PIL import Image
|
23 |
from Crypto.PublicKey import RSA
|
24 |
+
from Crypto.Cipher import PKCS1_v1_5
|
25 |
from datetime import datetime
|
26 |
from pymongo.mongo_client import MongoClient
|
27 |
from pymongo.server_api import ServerApi
|
|
|
213 |
logging.error(f'Error: {e}, traceback: {traceback.format_exc()}')
|
214 |
raise HTTPException(status_code=500, detail=f'Error: {e}, traceback: {traceback.format_exc()}')
|
215 |
|
216 |
+
|
217 |
+
def decrypt_user_key(encrypted_key: str) -> dict|None:
|
218 |
try:
|
219 |
+
key = RSA.import_key(SECRET_KEY)
|
220 |
+
cipher = PKCS1_v1_5.new(key)
|
221 |
+
|
222 |
+
# Decode the base64 encrypted text
|
223 |
+
encrypted_bytes = base64.b64decode(encrypted_key)
|
224 |
+
|
225 |
+
# Decrypt the message
|
226 |
+
decrypted_bytes = cipher.decrypt(encrypted_bytes, None)
|
227 |
+
|
228 |
+
# Convert bytes to string
|
229 |
+
decrypted_text = decrypted_bytes.decode('utf-8')
|
230 |
+
|
231 |
+
# Try to parse as JSON for pretty printing
|
232 |
+
try:
|
233 |
+
data = json.loads(decrypted_text)
|
234 |
+
return data
|
235 |
+
except Exception as e:
|
236 |
+
logging.error(f'Error: {e}, traceback: {traceback.format_exc()}')
|
237 |
+
return None
|
238 |
except Exception as e:
|
239 |
logging.error(f'Error decrypting user key: {e}')
|
240 |
return None
|
|
|
293 |
# 检查用户密钥
|
294 |
user_key = None
|
295 |
if request.user_key:
|
296 |
+
user_info = decrypt_user_key(request.user_key)
|
297 |
+
if not user_info:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
298 |
raise HTTPException(
|
299 |
status_code=400,
|
300 |
detail="无效的用户密钥"
|
|
|
344 |
request_model_id=request.request_model_id,
|
345 |
openai_key=request.openai_key
|
346 |
)
|
347 |
+
|
348 |
+
if user_key:
|
349 |
+
# Update credit count by decrementing it by 1
|
350 |
+
user_info_db = client.user_info
|
351 |
+
user_keys_collection = user_info_db['user_keys']
|
352 |
+
user_keys_collection.update_one(
|
353 |
+
{"user_key": request.user_key},
|
354 |
+
{"$inc": {"credit": -1}}
|
355 |
+
)
|
356 |
|
357 |
return JSONResponse(response)
|
358 |
except HTTPException as he:
|