fruitpicker01 commited on
Commit
afab171
1 Parent(s): e775fc3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -45
app.py CHANGED
@@ -20,10 +20,53 @@ from transformers import AutoTokenizer, AutoModel
20
  from utils import best_text_choice
21
 
22
  import httpx
23
- import aiohttp
24
  import asyncio
25
  import ssl
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  tokenizer = AutoTokenizer.from_pretrained("Sergeyzh/rubert-tiny-turbo")
28
  model = AutoModel.from_pretrained("Sergeyzh/rubert-tiny-turbo")
29
  # Load the DataFrame used in the best_text_choice function
@@ -196,30 +239,13 @@ def clean_message(message):
196
  #def generate_message_mistral_generate(prompt, max_retries=5):
197
  async def generate_message_mistral_generate(prompt):
198
  try:
199
- # Создаём SSL-контекст для отключения проверки сертификатов
200
- # В продакшн-среде НЕ РЕКОМЕНДУЕТСЯ использовать verify=False
201
- ssl_context = ssl.create_default_context()
202
- ssl_context.check_hostname = False
203
- ssl_context.verify_mode = ssl.CERT_NONE
204
-
205
- async with aiohttp.ClientSession() as session:
206
- async with session.post(
207
- "https://gigachat-preview.devices.sberbank.ru/api/v1/",
208
- headers={
209
- "Authorization": f"Bearer {gc_key}",
210
- "Content-Type": "application/json"
211
- },
212
- json={"messages": [{"role": "system", "content": prompt}]},
213
- ssl=ssl_context # Используем ssl=ssl_context для отключения проверки
214
- ) as response:
215
- response.raise_for_status()
216
- res = await response.json()
217
- cleaned_message = clean_message(res.get("content", "").strip())
218
- return cleaned_message
219
- except aiohttp.ClientError as e:
220
- return f"HTTP ошибка при обращении к GigaChat-Pro: {e}"
221
  except Exception as e:
222
- return f"Неизвестная ошибка при обращении к GigaChat-Pro: {e}"
 
223
 
224
  # retries = 0
225
  # while retries < max_retries:
@@ -254,28 +280,12 @@ async def generate_message_mistral_generate(prompt):
254
  #def generate_message_mistral_check(prompt, max_retries=5):
255
  async def generate_message_mistral_check(prompt):
256
  try:
257
- ssl_context = ssl.create_default_context()
258
- ssl_context.check_hostname = False
259
- ssl_context.verify_mode = ssl.CERT_NONE
260
-
261
- async with aiohttp.ClientSession() as session:
262
- async with session.post(
263
- "https://gigachat-preview.devices.sberbank.ru/api/v1/",
264
- headers={
265
- "Authorization": f"Bearer {gc_key}",
266
- "Content-Type": "application/json"
267
- },
268
- json={"messages": [{"role": "system", "content": prompt}]},
269
- ssl=ssl_context
270
- ) as response:
271
- response.raise_for_status()
272
- res2 = await response.json()
273
- cleaned_message = clean_message(res2.get("content", "").strip())
274
- return cleaned_message
275
- except aiohttp.ClientError as e:
276
- return f"HTTP ошибка при обращении к GigaChat-Pro: {e}"
277
  except Exception as e:
278
- return f"Неизвестная ошибка при обращении к GigaChat-Pro: {e}"
279
  # retries = 0
280
  # while retries < max_retries:
281
  # try:
 
20
  from utils import best_text_choice
21
 
22
  import httpx
 
23
  import asyncio
24
  import ssl
25
 
26
+ class AsyncGigaChat:
27
+ def __init__(self, credentials, model, base_url, max_tokens, temperature, verify_ssl_certs=True):
28
+ self.credentials = credentials
29
+ self.model = model
30
+ self.base_url = base_url
31
+ self.max_tokens = max_tokens
32
+ self.temperature = temperature
33
+ self.verify_ssl_certs = verify_ssl_certs
34
+ self.client = httpx.AsyncClient()
35
+
36
+ async def __call__(self, messages):
37
+ headers = {
38
+ "Authorization": f"Bearer {self.credentials}",
39
+ "Content-Type": "application/json"
40
+ }
41
+ payload = {
42
+ "model": self.model,
43
+ "messages": [msg.content for msg in messages],
44
+ "max_tokens": self.max_tokens,
45
+ "temperature": self.temperature
46
+ }
47
+ response = await self.client.post(f"{self.base_url}/generate", headers=headers, json=payload)
48
+ response.raise_for_status()
49
+ return response.json()
50
+
51
+ chat_pro_async = AsyncGigaChat(
52
+ credentials=gc_key,
53
+ model='GigaChat-Pro-preview',
54
+ base_url='https://gigachat-preview.devices.sberbank.ru/api/v1/',
55
+ max_tokens=68,
56
+ temperature=1.15,
57
+ verify_ssl_certs=False
58
+ )
59
+
60
+ chat_pro_check_async = AsyncGigaChat(
61
+ credentials=gc_key,
62
+ model='GigaChat-Pro-preview',
63
+ base_url='https://gigachat-preview.devices.sberbank.ru/api/v1/',
64
+ max_tokens=3000,
65
+ temperature=0.8,
66
+ verify_ssl_certs=False
67
+ )
68
+
69
+
70
  tokenizer = AutoTokenizer.from_pretrained("Sergeyzh/rubert-tiny-turbo")
71
  model = AutoModel.from_pretrained("Sergeyzh/rubert-tiny-turbo")
72
  # Load the DataFrame used in the best_text_choice function
 
239
  #def generate_message_mistral_generate(prompt, max_retries=5):
240
  async def generate_message_mistral_generate(prompt):
241
  try:
242
+ messages = [SystemMessage(content=prompt)]
243
+ res = await chat_pro_async(messages) # Предполагается, что chat_pro_async использует httpx
244
+ cleaned_message = clean_message(res.content.strip())
245
+ return cleaned_message
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246
  except Exception as e:
247
+ return f"Ошибка при обращении к GigaChat-Pro: {e}"
248
+
249
 
250
  # retries = 0
251
  # while retries < max_retries:
 
280
  #def generate_message_mistral_check(prompt, max_retries=5):
281
  async def generate_message_mistral_check(prompt):
282
  try:
283
+ messages = [SystemMessage(content=prompt)]
284
+ res = await chat_pro_check_async(messages) # Предполагается, что chat_pro_async и��пользует httpx
285
+ cleaned_message = clean_message(res.content.strip())
286
+ return cleaned_message
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
287
  except Exception as e:
288
+ return f"Ошибка при обращении к GigaChat-Pro: {e}"
289
  # retries = 0
290
  # while retries < max_retries:
291
  # try: