Spaces:
Running
on
Zero
Running
on
Zero
ai-forever
commited on
Commit
•
2adb577
1
Parent(s):
e759a3a
add system for i2v and max_tokens=128 param
Browse files- src/gigachat.py +153 -142
src/gigachat.py
CHANGED
@@ -1,143 +1,154 @@
|
|
1 |
-
import requests
|
2 |
-
import base64
|
3 |
-
import uuid
|
4 |
-
import json
|
5 |
-
import time
|
6 |
-
from typing import Dict, Optional, Any
|
7 |
-
from dotenv import load_dotenv
|
8 |
-
import os
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
# print(f"
|
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 |
return response_str
|
|
|
1 |
+
import requests
|
2 |
+
import base64
|
3 |
+
import uuid
|
4 |
+
import json
|
5 |
+
import time
|
6 |
+
from typing import Dict, Optional, Any
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
import os
|
9 |
+
from prompts import giga_system_prompt_i2v
|
10 |
+
|
11 |
+
# Load environment variables from .env file
|
12 |
+
load_dotenv()
|
13 |
+
|
14 |
+
AUTH_TOKEN = os.getenv("AUTH_TOKEN")
|
15 |
+
COOKIE = os.getenv("COOKIE")
|
16 |
+
|
17 |
+
# print(f"AUTH_TOKEN: {AUTH_TOKEN}")
|
18 |
+
# print(f"COOKIE: {COOKIE}")
|
19 |
+
|
20 |
+
def get_auth_token(timeout: float = 2) -> Dict[str, Any]:
|
21 |
+
"""
|
22 |
+
Get authentication token.
|
23 |
+
|
24 |
+
Args:
|
25 |
+
timeout (float): Timeout duration in seconds.
|
26 |
+
|
27 |
+
Returns:
|
28 |
+
Dict[str, Any]: Dictionary containing the access token and its expiration time.
|
29 |
+
"""
|
30 |
+
url = "https://beta.saluteai.sberdevices.ru/v1/token"
|
31 |
+
payload = 'scope=GIGACHAT_API_CORP'
|
32 |
+
headers = {
|
33 |
+
'Content-Type': 'application/x-www-form-urlencoded',
|
34 |
+
'Accept': 'application/json',
|
35 |
+
'RqUID': str(uuid.uuid4()),
|
36 |
+
'Cookie': COOKIE,
|
37 |
+
'Authorization': f'Basic {AUTH_TOKEN}'
|
38 |
+
}
|
39 |
+
response = requests.post(url, headers=headers, data=payload, timeout=timeout)
|
40 |
+
response_dict = response.json()
|
41 |
+
return {
|
42 |
+
'access_token': response_dict['tok'],
|
43 |
+
'expires_at': response_dict['exp']
|
44 |
+
}
|
45 |
+
|
46 |
+
def check_auth_token(token_data: Dict[str, Any]) -> bool:
|
47 |
+
"""
|
48 |
+
Check if the authentication token is valid.
|
49 |
+
|
50 |
+
Args:
|
51 |
+
token_data (Dict[str, Any]): Dictionary containing token data.
|
52 |
+
|
53 |
+
Returns:
|
54 |
+
bool: True if the token is valid, False otherwise.
|
55 |
+
"""
|
56 |
+
return token_data['expires_at'] - time.time() > 5
|
57 |
+
|
58 |
+
token_data: Optional[Dict[str, Any]] = None
|
59 |
+
|
60 |
+
def get_response(
|
61 |
+
prompt: str,
|
62 |
+
model: str,
|
63 |
+
timeout: int = 120,
|
64 |
+
n: int = 1,
|
65 |
+
fuse_key_word: Optional[str] = None,
|
66 |
+
use_giga_censor: bool = False,
|
67 |
+
max_tokens: int = 512,
|
68 |
+
) -> requests.Response:
|
69 |
+
"""
|
70 |
+
Send a text generation request to the API.
|
71 |
+
|
72 |
+
Args:
|
73 |
+
prompt (str): The input prompt.
|
74 |
+
model (str): The model to be used for generation.
|
75 |
+
timeout (int): Timeout duration in seconds.
|
76 |
+
n (int): Number of responses.
|
77 |
+
fuse_key_word (Optional[str]): Additional keyword to include in the prompt.
|
78 |
+
use_giga_censor (bool): Whether to use profanity filtering.
|
79 |
+
max_tokens (int): Maximum number of tokens in the response.
|
80 |
+
|
81 |
+
Returns:
|
82 |
+
requests.Response: API response.
|
83 |
+
"""
|
84 |
+
global token_data
|
85 |
+
|
86 |
+
url = "https://beta.saluteai.sberdevices.ru/v1/chat/completions"
|
87 |
+
payload = json.dumps({
|
88 |
+
"model": model,
|
89 |
+
# "messages": [
|
90 |
+
# {
|
91 |
+
# "role": "user",
|
92 |
+
# "content": ' '.join([fuse_key_word, prompt]) if fuse_key_word else prompt
|
93 |
+
# }
|
94 |
+
# ],
|
95 |
+
"messages": [
|
96 |
+
{
|
97 |
+
"role": "system",
|
98 |
+
"content": giga_system_prompt_i2v
|
99 |
+
},
|
100 |
+
{
|
101 |
+
"role": "user",
|
102 |
+
"content": prompt
|
103 |
+
}
|
104 |
+
],
|
105 |
+
"temperature": 0.87,
|
106 |
+
"top_p": 0.47,
|
107 |
+
"n": n,
|
108 |
+
"stream": False,
|
109 |
+
"max_tokens": max_tokens,
|
110 |
+
"repetition_penalty": 1.07,
|
111 |
+
"profanity_check": use_giga_censor
|
112 |
+
})
|
113 |
+
|
114 |
+
if token_data is None or not check_auth_token(token_data):
|
115 |
+
token_data = get_auth_token()
|
116 |
+
|
117 |
+
headers = {
|
118 |
+
'Content-Type': 'application/json',
|
119 |
+
'Accept': 'application/json',
|
120 |
+
'Authorization': f'Bearer {token_data["access_token"]}'
|
121 |
+
}
|
122 |
+
response = requests.post(url, headers=headers, data=payload, timeout=timeout)
|
123 |
+
return response
|
124 |
+
|
125 |
+
def giga_generate(
|
126 |
+
prompt: str,
|
127 |
+
model_version: str = "GigaChat-Max",
|
128 |
+
max_tokens: int = 2048
|
129 |
+
) -> str:
|
130 |
+
"""
|
131 |
+
Generate text using the GigaChat model.
|
132 |
+
|
133 |
+
Args:
|
134 |
+
prompt (str): The input prompt.
|
135 |
+
model_version (str): The version of the model to use.
|
136 |
+
max_tokens (int): Maximum number of tokens in the response.
|
137 |
+
|
138 |
+
Returns:
|
139 |
+
str: Generated text.
|
140 |
+
"""
|
141 |
+
response = get_response(
|
142 |
+
prompt,
|
143 |
+
model_version,
|
144 |
+
use_giga_censor=False,
|
145 |
+
max_tokens=128,
|
146 |
+
)
|
147 |
+
response_dict = response.json()
|
148 |
+
|
149 |
+
if response_dict['choices'][0]['finish_reason'] == 'blacklist':
|
150 |
+
print('GigaCensor triggered!')
|
151 |
+
return 'Censored Text'
|
152 |
+
else:
|
153 |
+
response_str = response_dict['choices'][0]['message']['content']
|
154 |
return response_str
|