7
7
8
8
from datetime import datetime
9
9
from pprint import pprint
10
+ from functools import wraps
10
11
11
12
from config import *
12
13
from game import Game
@@ -27,8 +28,8 @@ class Generator(object):
27
28
GENERATOR_STATUS = False
28
29
29
30
def __init__ (self , token : str ):
30
- self .__token = token
31
- self .__auth_headers = asyncio .run (self .__get_auth ())
31
+ self ._token = token
32
+ self ._auth_headers = asyncio .run (self ._get_auth ())
32
33
33
34
self .reg = re .compile ("{(.*?)}" )
34
35
self .list_reg = re .compile (r"\[\s*['\"][^'\"]*['\"]\s*(?:,\s*['\"][^'\"]*['\"]\s*)*]" )
@@ -57,6 +58,7 @@ def get_age():
57
58
@staticmethod
58
59
def check_current_generation ():
59
60
def generator (func ):
61
+ @wraps (func )
60
62
async def wrapper (self , * args , ** kwargs ):
61
63
if self .GENERATOR_STATUS :
62
64
raise KeyError ("The generator is busy. Please, try again later." )
@@ -69,7 +71,7 @@ async def wrapper(self, *args, **kwargs):
69
71
return wrapper
70
72
return generator
71
73
72
- def __token_processing (self , response : httpx .Response ) -> dict :
74
+ def _token_processing (self , response : httpx .Response ) -> dict :
73
75
result = response .json ()["result" ]["alternatives" ][- 1 ]["message" ]["text" ]
74
76
result = result .replace ("\n " , "" )
75
77
result = result .replace ("\t " , "" )
@@ -103,16 +105,16 @@ async def generate_data_list(self, model_data):
103
105
resp = await client .post (
104
106
self .URL ,
105
107
json = data ,
106
- headers = self .__auth_headers , timeout = 80
108
+ headers = self ._auth_headers , timeout = 80
107
109
)
108
110
# print(resp.text)
109
111
if resp .status_code == 401 :
110
- self .__auth_headers = await self .__get_auth ()
112
+ self ._auth_headers = await self ._get_auth ()
111
113
async with httpx .AsyncClient () as client :
112
114
resp = await client .post (
113
115
self .URL ,
114
116
json = data ,
115
- headers = self .__auth_headers , timeout = 80
117
+ headers = self ._auth_headers , timeout = 80
116
118
)
117
119
if resp .status_code != 200 :
118
120
raise KeyError ("Something went wrong, try again later."
@@ -128,18 +130,18 @@ async def generate_data_list(self, model_data):
128
130
result = json .loads (result [0 ])
129
131
return result
130
132
131
- async def __get_auth (self ):
132
- if "y0_" in self .__token :
133
+ async def _get_auth (self ):
134
+ if "y0_" in self ._token :
133
135
async with httpx .AsyncClient () as client :
134
136
response = await client .post (
135
137
"https://iam.api.cloud.yandex.net/iam/v1/tokens/" ,
136
- json = {"yandexPassportOauthToken" : self .__token }, timeout = 10
138
+ json = {"yandexPassportOauthToken" : self ._token }, timeout = 10
137
139
)
138
140
return {
139
141
"Authorization" : f"Bearer { str (response .json ()['iamToken' ])} "
140
142
}
141
143
return {
142
- "Authorization" : f"Api-Key { self .__token } "
144
+ "Authorization" : f"Api-Key { self ._token } "
143
145
}
144
146
145
147
@check_current_generation ()
@@ -162,22 +164,22 @@ async def generate_catastrophe(self):
162
164
resp = await client .post (
163
165
self .URL ,
164
166
json = data ,
165
- headers = self .__auth_headers , timeout = 80
167
+ headers = self ._auth_headers , timeout = 80
166
168
)
167
169
if resp .status_code == 401 :
168
- self .__auth_headers = await self .__get_auth ()
170
+ self ._auth_headers = await self ._get_auth ()
169
171
async with httpx .AsyncClient () as client :
170
172
resp = await client .post (
171
173
self .URL ,
172
174
json = data ,
173
- headers = self .__auth_headers , timeout = 80
175
+ headers = self ._auth_headers , timeout = 80
174
176
)
175
177
self .process_generation (False )
176
178
if resp .status_code != 200 :
177
179
raise KeyError ("Something went wrong, try again later."
178
180
"It should be Yandex Cloud error (generation error), but may not be." )
179
181
self .tokens += int (resp .json ()["result" ]["usage" ]["totalTokens" ])
180
- result = self .__token_processing (resp )
182
+ result = self ._token_processing (resp )
181
183
return result
182
184
183
185
@check_current_generation ()
@@ -264,23 +266,23 @@ async def generate_player(self, game_code):
264
266
resp = await client .post (
265
267
self .URL ,
266
268
json = data ,
267
- headers = self .__auth_headers , timeout = 80
269
+ headers = self ._auth_headers , timeout = 80
268
270
)
269
271
if resp .status_code == 401 :
270
- self .__auth_headers = await self .__get_auth ()
272
+ self ._auth_headers = await self ._get_auth ()
271
273
async with httpx .AsyncClient () as client :
272
274
resp = await client .post (
273
275
self .URL ,
274
276
json = data ,
275
- headers = self .__auth_headers , timeout = 80
277
+ headers = self ._auth_headers , timeout = 80
276
278
)
277
279
self .process_generation (False )
278
280
if resp .status_code != 200 :
279
281
raise KeyError ("Something went wrong, try again later."
280
282
"It should be Yandex Cloud error (generation error), but may not be." )
281
283
# print(resp.json())
282
284
self .tokens += int (resp .json ()["result" ]["usage" ]["totalTokens" ])
283
- result = self .__token_processing (resp )
285
+ result = self ._token_processing (resp )
284
286
285
287
result ["age" ] = round (self .get_age ())
286
288
active_card = self .games [game_code ].active_card
@@ -322,18 +324,18 @@ async def generate_bunker(self):
322
324
)
323
325
324
326
async with httpx .AsyncClient () as client :
325
- resp = await client .post (self .URL , json = data , headers = self .__auth_headers , timeout = 80 )
327
+ resp = await client .post (self .URL , json = data , headers = self ._auth_headers , timeout = 80 )
326
328
if resp .status_code == 401 :
327
- self .__auth_headers = await self .__get_auth ()
329
+ self ._auth_headers = await self ._get_auth ()
328
330
async with httpx .AsyncClient () as client :
329
- resp = await client .post (self .URL , json = data , headers = self .__auth_headers , timeout = 80 )
331
+ resp = await client .post (self .URL , json = data , headers = self ._auth_headers , timeout = 80 )
330
332
self .process_generation (False )
331
333
if resp .status_code != 200 :
332
334
raise KeyError ("Something went wrong, try again later."
333
335
"It should be Yandex Cloud error (generation error), but may not be." )
334
336
335
337
self .tokens += int (resp .json ()["result" ]["usage" ]["totalTokens" ])
336
- result = self .__token_processing (resp )
338
+ result = self ._token_processing (resp )
337
339
return result
338
340
339
341
@staticmethod
@@ -378,19 +380,19 @@ async def generate_bunker_result(self, game_data):
378
380
}
379
381
)
380
382
async with httpx .AsyncClient () as client :
381
- resp = await client .post (self .URL , json = data , headers = self .__auth_headers , timeout = 80 )
383
+ resp = await client .post (self .URL , json = data , headers = self ._auth_headers , timeout = 80 )
382
384
# print(resp.json())
383
385
if resp .status_code == 401 :
384
- self .__auth_headers = await self .__get_auth ()
386
+ self ._auth_headers = await self ._get_auth ()
385
387
async with httpx .AsyncClient () as client :
386
- resp = await client .post (self .URL , json = data , headers = self .__auth_headers , timeout = 80 )
388
+ resp = await client .post (self .URL , json = data , headers = self ._auth_headers , timeout = 80 )
387
389
self .process_generation (False )
388
390
if resp .status_code != 200 :
389
391
raise KeyError ("Something went wrong, try again later."
390
392
"It should be Yandex Cloud error (generation error), but may not be." )
391
393
392
394
self .tokens += int (resp .json ()["result" ]["usage" ]["totalTokens" ])
393
- result = self .__token_processing (resp )
395
+ result = self ._token_processing (resp )
394
396
return result
395
397
396
398
@check_current_generation ()
@@ -413,24 +415,24 @@ async def generate_result(self, game_data):
413
415
)
414
416
415
417
async with httpx .AsyncClient () as client :
416
- resp = await client .post (self .URL , json = data , headers = self .__auth_headers , timeout = 80 )
418
+ resp = await client .post (self .URL , json = data , headers = self ._auth_headers , timeout = 80 )
417
419
# print(resp.json())
418
420
if resp .status_code == 401 :
419
- self .__auth_headers = await self .__get_auth ()
421
+ self ._auth_headers = await self ._get_auth ()
420
422
async with httpx .AsyncClient () as client :
421
- resp = await client .post (self .URL , json = data , headers = self .__auth_headers , timeout = 80 )
423
+ resp = await client .post (self .URL , json = data , headers = self ._auth_headers , timeout = 80 )
422
424
self .process_generation (False )
423
425
424
426
if resp .status_code != 200 :
425
427
raise KeyError ("Something went wrong, try again later."
426
428
"It should be Yandex Cloud error (generation error), but may not be." )
427
429
428
430
self .tokens += int (resp .json ()["result" ]["usage" ]["totalTokens" ])
429
- result = self .__token_processing (resp )
431
+ result = self ._token_processing (resp )
430
432
return result
431
433
432
434
433
- if __name__ == "__main__ " :
435
+ if __name__ == "_main_ " :
434
436
with open ("../secrets/gpt-reserve/api_key.txt" ) as file :
435
437
model_token = file .read ().strip ()
436
438
with open ("../secrets/gpt-reserve/model_uri.txt" ) as file :
0 commit comments