Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -14,10 +14,8 @@ openai_api_key = os.getenv('GPT_KEY')
|
|
14 |
gc_key = os.getenv('GC_KEY')
|
15 |
token = os.getenv('GITHUB_TOKEN')
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
chat_lite = GigaChat(credentials=gc_key, model='GigaChat', max_tokens=68, verify_ssl_certs=False)
|
20 |
-
chat_plus = GigaChat(credentials=gc_key, model='GigaChat-Plus', max_tokens=68, verify_ssl_certs=False)
|
21 |
|
22 |
# Загрузка данных из Excel-файла
|
23 |
try:
|
@@ -107,10 +105,10 @@ def clean_message(message):
|
|
107 |
def generate_message_gigachat_pro(prompt, temperature=0.87):
|
108 |
try:
|
109 |
messages = [SystemMessage(content=prompt)]
|
110 |
-
chat_pro =
|
111 |
res = chat_pro(messages)
|
112 |
cleaned_message = clean_message(res.content.strip())
|
113 |
-
return cleaned_message
|
114 |
except Exception as e:
|
115 |
return f"Ошибка при обращении к GigaChat-Pro: {e}"
|
116 |
|
@@ -118,10 +116,10 @@ def generate_message_gigachat_lite(prompt, temperature=0.87):
|
|
118 |
try:
|
119 |
time.sleep(2)
|
120 |
messages = [SystemMessage(content=prompt)]
|
121 |
-
chat_lite =
|
122 |
res = chat_lite(messages)
|
123 |
cleaned_message = clean_message(res.content.strip())
|
124 |
-
return cleaned_message
|
125 |
except Exception as e:
|
126 |
return f"Ошибка при обращении к GigaChat-Lite: {e}"
|
127 |
|
@@ -129,13 +127,14 @@ def generate_message_gigachat_plus(prompt, temperature=0.87):
|
|
129 |
try:
|
130 |
time.sleep(2)
|
131 |
messages = [SystemMessage(content=prompt)]
|
132 |
-
chat_plus =
|
133 |
res = chat_plus(messages)
|
134 |
cleaned_message = clean_message(res.content.strip())
|
135 |
-
return cleaned_message
|
136 |
except Exception as e:
|
137 |
return f"Ошибка при обращении к GigaChat-Plus: {e}"
|
138 |
|
|
|
139 |
def generate_message_gpt4o_with_retry(prompt):
|
140 |
for _ in range(10): # Максимум 10 попыток
|
141 |
message = generate_message_gpt4o(prompt)
|
@@ -166,7 +165,7 @@ def generate_message_gigachat_plus_with_retry(prompt):
|
|
166 |
|
167 |
|
168 |
# Обновляем генерацию сообщений для отображения в интерфейсе
|
169 |
-
def generate_messages(description, advantages, *selected_values):
|
170 |
standard_prompt = generate_standard_prompt(description, advantages, *selected_values)
|
171 |
|
172 |
results = {
|
@@ -179,26 +178,26 @@ def generate_messages(description, advantages, *selected_values):
|
|
179 |
|
180 |
yield results["prompt"], "", "", "", "", "Генерация стандартного промпта завершена"
|
181 |
|
182 |
-
results["gpt4o"] = generate_message_gpt4o_with_retry(standard_prompt)
|
183 |
gpt4o_length = len(results["gpt4o"])
|
184 |
gpt4o_display = f"{results['gpt4o']}\n\n------\nКоличество знаков: {gpt4o_length}"
|
185 |
yield results["prompt"], gpt4o_display, "", "", "", "Сообщение GPT-4o сгенерировано"
|
186 |
|
187 |
-
results["gigachat_pro"] = generate_message_gigachat_pro_with_retry(standard_prompt)
|
188 |
gigachat_pro_length = len(results["gigachat_pro"])
|
189 |
gigachat_pro_display = f"{results['gigachat_pro']}\n\n------\nКоличество знаков: {gigachat_pro_length}"
|
190 |
yield results["prompt"], gpt4o_display, gigachat_pro_display, "", "", "Сообщение GigaChat-Pro сгенерировано"
|
191 |
|
192 |
time.sleep(2)
|
193 |
|
194 |
-
results["gigachat_lite"] = generate_message_gigachat_lite_with_retry(standard_prompt)
|
195 |
gigachat_lite_length = len(results["gigachat_lite"])
|
196 |
gigachat_lite_display = f"{results['gigachat_lite']}\n\n------\nКоличество знаков: {gigachat_lite_length}"
|
197 |
yield results["prompt"], gpt4o_display, gigachat_pro_display, gigachat_lite_display, "", "Сообщение GigaChat-Lite сгенерировано"
|
198 |
|
199 |
time.sleep(2)
|
200 |
|
201 |
-
results["gigachat_plus"] = generate_message_gigachat_plus_with_retry(standard_prompt)
|
202 |
gigachat_plus_length = len(results["gigachat_plus"])
|
203 |
gigachat_plus_display = f"{results['gigachat_plus']}\n\n------\nКоличество знаков: {gigachat_plus_length}"
|
204 |
yield results["prompt"], gpt4o_display, gigachat_pro_display, gigachat_lite_display, gigachat_plus_display, "Все сообщения сгенерированы"
|
@@ -206,7 +205,6 @@ def generate_messages(description, advantages, *selected_values):
|
|
206 |
return results
|
207 |
|
208 |
|
209 |
-
|
210 |
# Функция для генерации персонализированного промпта
|
211 |
def generate_personalization_prompt(*selected_values):
|
212 |
prompt = "Адаптируй, не превышая длину сообщения в 250 знаков с пробелами, текст с учетом следующих особенностей:\n"
|
|
|
14 |
gc_key = os.getenv('GC_KEY')
|
15 |
token = os.getenv('GITHUB_TOKEN')
|
16 |
|
17 |
+
def authenticate_gigachat(model, max_tokens, temperature):
|
18 |
+
return GigaChat(credentials=gc_key, model=model, max_tokens=max_tokens, temperature=temperature, verify_ssl_certs=False)
|
|
|
|
|
19 |
|
20 |
# Загрузка данных из Excel-файла
|
21 |
try:
|
|
|
105 |
def generate_message_gigachat_pro(prompt, temperature=0.87):
|
106 |
try:
|
107 |
messages = [SystemMessage(content=prompt)]
|
108 |
+
chat_pro = authenticate_gigachat('GigaChat-Pro', max_tokens=68, temperature=temperature)
|
109 |
res = chat_pro(messages)
|
110 |
cleaned_message = clean_message(res.content.strip())
|
111 |
+
return cleaned_message + f" {temperature}"
|
112 |
except Exception as e:
|
113 |
return f"Ошибка при обращении к GigaChat-Pro: {e}"
|
114 |
|
|
|
116 |
try:
|
117 |
time.sleep(2)
|
118 |
messages = [SystemMessage(content=prompt)]
|
119 |
+
chat_lite = authenticate_gigachat('GigaChat', max_tokens=68, temperature=temperature)
|
120 |
res = chat_lite(messages)
|
121 |
cleaned_message = clean_message(res.content.strip())
|
122 |
+
return cleaned_message + f" {temperature}"
|
123 |
except Exception as e:
|
124 |
return f"Ошибка при обращении к GigaChat-Lite: {e}"
|
125 |
|
|
|
127 |
try:
|
128 |
time.sleep(2)
|
129 |
messages = [SystemMessage(content=prompt)]
|
130 |
+
chat_plus = authenticate_gigachat('GigaChat-Plus', max_tokens=68, temperature=temperature)
|
131 |
res = chat_plus(messages)
|
132 |
cleaned_message = clean_message(res.content.strip())
|
133 |
+
return cleaned_message + f" {temperature}"
|
134 |
except Exception as e:
|
135 |
return f"Ошибка при обращении к GigaChat-Plus: {e}"
|
136 |
|
137 |
+
|
138 |
def generate_message_gpt4o_with_retry(prompt):
|
139 |
for _ in range(10): # Максимум 10 попыток
|
140 |
message = generate_message_gpt4o(prompt)
|
|
|
165 |
|
166 |
|
167 |
# Обновляем генерацию сообщений для отображения в интерфейсе
|
168 |
+
def generate_messages(description, advantages, *selected_values, gpt4o_temperature, gigachat_pro_temperature, gigachat_lite_temperature, gigachat_plus_temperature):
|
169 |
standard_prompt = generate_standard_prompt(description, advantages, *selected_values)
|
170 |
|
171 |
results = {
|
|
|
178 |
|
179 |
yield results["prompt"], "", "", "", "", "Генерация стандартного промпта завершена"
|
180 |
|
181 |
+
results["gpt4o"] = generate_message_gpt4o_with_retry(standard_prompt, temperature=gpt4o_temperature)
|
182 |
gpt4o_length = len(results["gpt4o"])
|
183 |
gpt4o_display = f"{results['gpt4o']}\n\n------\nКоличество знаков: {gpt4o_length}"
|
184 |
yield results["prompt"], gpt4o_display, "", "", "", "Сообщение GPT-4o сгенерировано"
|
185 |
|
186 |
+
results["gigachat_pro"] = generate_message_gigachat_pro_with_retry(standard_prompt, temperature=gigachat_pro_temperature)
|
187 |
gigachat_pro_length = len(results["gigachat_pro"])
|
188 |
gigachat_pro_display = f"{results['gigachat_pro']}\n\n------\nКоличество знаков: {gigachat_pro_length}"
|
189 |
yield results["prompt"], gpt4o_display, gigachat_pro_display, "", "", "Сообщение GigaChat-Pro сгенерировано"
|
190 |
|
191 |
time.sleep(2)
|
192 |
|
193 |
+
results["gigachat_lite"] = generate_message_gigachat_lite_with_retry(standard_prompt, temperature=gigachat_lite_temperature)
|
194 |
gigachat_lite_length = len(results["gigachat_lite"])
|
195 |
gigachat_lite_display = f"{results['gigachat_lite']}\n\n------\nКоличество знаков: {gigachat_lite_length}"
|
196 |
yield results["prompt"], gpt4o_display, gigachat_pro_display, gigachat_lite_display, "", "Сообщение GigaChat-Lite сгенерировано"
|
197 |
|
198 |
time.sleep(2)
|
199 |
|
200 |
+
results["gigachat_plus"] = generate_message_gigachat_plus_with_retry(standard_prompt, temperature=gigachat_plus_temperature)
|
201 |
gigachat_plus_length = len(results["gigachat_plus"])
|
202 |
gigachat_plus_display = f"{results['gigachat_plus']}\n\n------\nКоличество знаков: {gigachat_plus_length}"
|
203 |
yield results["prompt"], gpt4o_display, gigachat_pro_display, gigachat_lite_display, gigachat_plus_display, "Все сообщения сгенерированы"
|
|
|
205 |
return results
|
206 |
|
207 |
|
|
|
208 |
# Функция для генерации персонализированного промпта
|
209 |
def generate_personalization_prompt(*selected_values):
|
210 |
prompt = "Адаптируй, не превышая длину сообщения в 250 знаков с пробелами, текст с учетом следующих особенностей:\n"
|