fruitpicker01 commited on
Commit
0c45313
1 Parent(s): eb10741

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -67
app.py CHANGED
@@ -19,56 +19,11 @@ import io
19
  from transformers import AutoTokenizer, AutoModel
20
  from utils import best_text_choice
21
 
22
- import httpx
23
  import asyncio
24
- import ssl
25
 
26
  # Авторизация в GigaChat Pro
27
  gc_key = os.getenv('GC_KEY')
28
 
29
- class AsyncGigaChat:
30
- def __init__(self, credentials, model, base_url, max_tokens, temperature, verify_ssl_certs=True):
31
- self.credentials = credentials
32
- self.model = model
33
- self.base_url = base_url
34
- self.max_tokens = max_tokens
35
- self.temperature = temperature
36
- self.verify_ssl_certs = verify_ssl_certs
37
- self.client = httpx.AsyncClient(verify=self.verify_ssl_certs)
38
-
39
- async def __call__(self, messages):
40
- headers = {
41
- "Authorization": f"Bearer {self.credentials}",
42
- "Content-Type": "application/json"
43
- }
44
- payload = {
45
- "model": self.model,
46
- "messages": [msg.content for msg in messages],
47
- "max_tokens": self.max_tokens,
48
- "temperature": self.temperature
49
- }
50
- response = await self.client.post(f"{self.base_url}/generate", headers=headers, json=payload)
51
- response.raise_for_status()
52
- return response.json()
53
-
54
- chat_pro_async = AsyncGigaChat(
55
- credentials=gc_key,
56
- model='GigaChat-Pro-preview',
57
- base_url='https://gigachat-preview.devices.sberbank.ru/api/v1/',
58
- max_tokens=68,
59
- temperature=1.15,
60
- verify_ssl_certs=False
61
- )
62
-
63
- chat_pro_check_async = AsyncGigaChat(
64
- credentials=gc_key,
65
- model='GigaChat-Pro-preview',
66
- base_url='https://gigachat-preview.devices.sberbank.ru/api/v1/',
67
- max_tokens=3000,
68
- temperature=0.8,
69
- verify_ssl_certs=False
70
- )
71
-
72
 
73
  tokenizer = AutoTokenizer.from_pretrained("Sergeyzh/rubert-tiny-turbo")
74
  model = AutoModel.from_pretrained("Sergeyzh/rubert-tiny-turbo")
@@ -86,25 +41,23 @@ client_mistral_check = Mistral(api_key=MISTRAL_API_KEY)
86
 
87
  morph = pymorphy3.MorphAnalyzer()
88
 
 
 
 
 
 
 
 
 
89
 
90
- #chat_pro = GigaChat(credentials=gc_key, model='GigaChat', max_tokens=68, temperature=1.15, verify_ssl_certs=False)
91
- #chat_pro = GigaChat(
92
- # credentials=gc_key,
93
- # model='GigaChat-Pro-preview',
94
- # base_url='https://gigachat-preview.devices.sberbank.ru/api/v1/',
95
- # max_tokens=68,
96
- # temperature=1.15,
97
- # verify_ssl_certs=False
98
- #)
99
-
100
- #chat_pro_check = GigaChat(
101
- # credentials=gc_key,
102
- # model='GigaChat-Pro-preview',
103
- # base_url='https://gigachat-preview.devices.sberbank.ru/api/v1/',
104
- # max_tokens=3000,
105
- # temperature=0.8,
106
- # verify_ssl_certs=False
107
- #)
108
 
109
  approach_stats = {
110
  "Начни сообщение с призыва к действию с продуктом. Не начинай с вопроса.": {"failed_checks": defaultdict(int), "total_attempts": 0},
@@ -242,7 +195,7 @@ def clean_message(message):
242
  async def generate_message_mistral_generate(prompt):
243
  try:
244
  messages = [SystemMessage(content=prompt)]
245
- res = await chat_pro_async(messages) # Предполагается, что chat_pro_async использует httpx
246
  cleaned_message = clean_message(res.content.strip())
247
  return cleaned_message
248
  except Exception as e:
@@ -280,11 +233,11 @@ async def generate_message_mistral_generate(prompt):
280
  # return None
281
 
282
  #def generate_message_mistral_check(prompt, max_retries=5):
283
- async def generate_message_mistral_check(prompt):
284
  try:
285
  messages = [SystemMessage(content=prompt)]
286
- res = await chat_pro_check_async(messages) # Предполагается, что chat_pro_async использует httpx
287
- cleaned_message = clean_message(res.content.strip())
288
  return cleaned_message
289
  except Exception as e:
290
  return f"Ошибка при обращении к GigaChat-Pro: {e}"
 
19
  from transformers import AutoTokenizer, AutoModel
20
  from utils import best_text_choice
21
 
 
22
  import asyncio
 
23
 
24
  # Авторизация в GigaChat Pro
25
  gc_key = os.getenv('GC_KEY')
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  tokenizer = AutoTokenizer.from_pretrained("Sergeyzh/rubert-tiny-turbo")
29
  model = AutoModel.from_pretrained("Sergeyzh/rubert-tiny-turbo")
 
41
 
42
  morph = pymorphy3.MorphAnalyzer()
43
 
44
+ chat_pro = GigaChat(
45
+ credentials=gc_key,
46
+ model='GigaChat-Pro-preview',
47
+ base_url='https://gigachat-preview.devices.sberbank.ru/api/v1/',
48
+ max_tokens=68,
49
+ temperature=1.15,
50
+ verify_ssl_certs=False
51
+ )
52
 
53
+ chat_pro_check = GigaChat(
54
+ credentials=gc_key,
55
+ model='GigaChat-Pro-preview',
56
+ base_url='https://gigachat-preview.devices.sberbank.ru/api/v1/',
57
+ max_tokens=3000,
58
+ temperature=0.8,
59
+ verify_ssl_certs=False
60
+ )
 
 
 
 
 
 
 
 
 
 
61
 
62
  approach_stats = {
63
  "Начни сообщение с призыва к действию с продуктом. Не начинай с вопроса.": {"failed_checks": defaultdict(int), "total_attempts": 0},
 
195
  async def generate_message_mistral_generate(prompt):
196
  try:
197
  messages = [SystemMessage(content=prompt)]
198
+ res = await chat_pro.ainvoke(messages)
199
  cleaned_message = clean_message(res.content.strip())
200
  return cleaned_message
201
  except Exception as e:
 
233
  # return None
234
 
235
  #def generate_message_mistral_check(prompt, max_retries=5):
236
+ async def generate_message_mistral_check(prompt):
237
  try:
238
  messages = [SystemMessage(content=prompt)]
239
+ res2 = await chat_pro_check.ainvoke(messages)
240
+ cleaned_message = clean_message(res2.content.strip())
241
  return cleaned_message
242
  except Exception as e:
243
  return f"Ошибка при обращении к GigaChat-Pro: {e}"