Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -14,10 +14,9 @@ openai_api_key = os.getenv('GPT_KEY')
|
|
14 |
gc_key = os.getenv('GC_KEY')
|
15 |
token = os.getenv('GITHUB_TOKEN')
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
chat_plus = GigaChat(credentials=gc_key, model='GigaChat-Plus', max_tokens=68, verify_ssl_certs=False)
|
21 |
|
22 |
# Загрузка данных из Excel-файла
|
23 |
try:
|
@@ -104,64 +103,54 @@ def clean_message(message):
|
|
104 |
|
105 |
# Обновленные функции генерации сообщений с учетом обрезки незаконченных предложений
|
106 |
def generate_message_gigachat_pro(prompt, temperature):
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
return clean_message(res.content.strip())
|
112 |
-
except Exception as e:
|
113 |
-
return f"Ошибка при обращении к GigaChat-Pro: {e}"
|
114 |
|
115 |
def generate_message_gigachat_lite(prompt, temperature):
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
cleaned_message = clean_message(res.content.strip())
|
121 |
-
return cleaned_message
|
122 |
-
except Exception as e:
|
123 |
-
return f"Ошибка при обращении к GigaChat-Lite: {e}"
|
124 |
|
125 |
def generate_message_gigachat_plus(prompt, temperature):
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
return cleaned_message
|
132 |
-
except Exception as e:
|
133 |
-
return f"Ошибка при обращении к GigaChat-Plus: {e}"
|
134 |
|
135 |
-
def generate_message_gpt4o_with_retry(prompt):
|
136 |
for _ in range(10): # Максимум 10 попыток
|
137 |
-
message = generate_message_gpt4o(prompt)
|
138 |
if len(message) <= 250:
|
139 |
return message
|
140 |
return message # Возвращаем последнее сгенерированное сообщение, если все попытки не удались
|
141 |
|
142 |
-
def generate_message_gigachat_pro_with_retry(prompt):
|
143 |
for _ in range(10):
|
144 |
-
message = generate_message_gigachat_pro(prompt)
|
145 |
if len(message) <= 250:
|
146 |
return message
|
147 |
return message
|
148 |
|
149 |
-
def generate_message_gigachat_lite_with_retry(prompt):
|
150 |
for _ in range(10):
|
151 |
-
message = generate_message_gigachat_lite(prompt)
|
152 |
if len(message) <= 250:
|
153 |
return message
|
154 |
return message
|
155 |
|
156 |
-
def generate_message_gigachat_plus_with_retry(prompt):
|
157 |
for _ in range(10):
|
158 |
-
message = generate_message_gigachat_plus(prompt)
|
159 |
if len(message) <= 250:
|
160 |
return message
|
161 |
return message
|
162 |
|
163 |
# Обновляем генерацию сообщений для отображения в интерфейсе
|
164 |
-
def generate_messages(description, advantages, *selected_values):
|
165 |
standard_prompt = generate_standard_prompt(description, advantages, *selected_values)
|
166 |
|
167 |
results = {
|
@@ -174,26 +163,26 @@ def generate_messages(description, advantages, *selected_values):
|
|
174 |
|
175 |
yield results["prompt"], "", "", "", "", "Генерация стандартного промпта завершена"
|
176 |
|
177 |
-
results["gpt4o"] = generate_message_gpt4o_with_retry(standard_prompt)
|
178 |
gpt4o_length = len(results["gpt4o"])
|
179 |
gpt4o_display = f"{results['gpt4o']}\n\n------\nКоличество знаков: {gpt4o_length}"
|
180 |
yield results["prompt"], gpt4o_display, "", "", "", "Сообщение GPT-4o сгенерировано"
|
181 |
|
182 |
-
results["gigachat_pro"] = generate_message_gigachat_pro_with_retry(standard_prompt)
|
183 |
gigachat_pro_length = len(results["gigachat_pro"])
|
184 |
gigachat_pro_display = f"{results['gigachat_pro']}\n\n------\nКоличество знаков: {gigachat_pro_length}"
|
185 |
yield results["prompt"], gpt4o_display, gigachat_pro_display, "", "", "Сообщение GigaChat-Pro сгенерировано"
|
186 |
|
187 |
time.sleep(2)
|
188 |
|
189 |
-
results["gigachat_lite"] = generate_message_gigachat_lite_with_retry(standard_prompt)
|
190 |
gigachat_lite_length = len(results["gigachat_lite"])
|
191 |
gigachat_lite_display = f"{results['gigachat_lite']}\n\n------\nКоличество знаков: {gigachat_lite_length}"
|
192 |
yield results["prompt"], gpt4o_display, gigachat_pro_display, gigachat_lite_display, "", "Сообщение GigaChat-Lite сгенерировано"
|
193 |
|
194 |
time.sleep(2)
|
195 |
|
196 |
-
results["gigachat_plus"] = generate_message_gigachat_plus_with_retry(standard_prompt)
|
197 |
gigachat_plus_length = len(results["gigachat_plus"])
|
198 |
gigachat_plus_display = f"{results['gigachat_plus']}\n\n------\nКоличество знаков: {gigachat_plus_length}"
|
199 |
yield results["prompt"], gpt4o_display, gigachat_pro_display, gigachat_lite_display, gigachat_plus_display, "Все сообщения сгенерированы"
|
@@ -232,13 +221,14 @@ def perform_personalization_gigachat(standard_message, personalization_prompt, m
|
|
232 |
return clean_message(result)
|
233 |
|
234 |
|
235 |
-
def perform_personalization_with_retry(standard_message, personalization_prompt
|
236 |
for _ in range(10): # Максимум 10 попыток
|
237 |
-
message = perform_personalization(standard_message, personalization_prompt
|
238 |
if len(message) <= 250:
|
239 |
return message
|
240 |
return message # Возвращаем последнее сгенерированное сообщение, если все попытки не удались
|
241 |
|
|
|
242 |
def perform_personalization_gigachat_with_retry(standard_message, personalization_prompt, model, temperature):
|
243 |
for _ in range(10):
|
244 |
message = perform_personalization_gigachat(standard_message, personalization_prompt, model, temperature)
|
@@ -326,15 +316,15 @@ def check_errors_with_yield(*personalized_messages):
|
|
326 |
|
327 |
error_check_prompt = generate_error_check_prompt()
|
328 |
yield error_check_prompt, "", "", "", "", "Промпт для проверки текста сгенерирован"
|
329 |
-
|
330 |
-
error_message_gpt4o = perform_personalization(f"{error_check_prompt}\n\n{personalized_messages[0]}",
|
331 |
yield error_check_prompt, error_message_gpt4o, "", "", "", "Результат проверки GPT-4o сгенерирован"
|
332 |
|
333 |
-
error_message_gigachat_pro = perform_personalization_gigachat(f"{error_check_prompt}\n\n{personalized_messages[1]}",
|
334 |
yield error_check_prompt, error_message_gpt4o, error_message_gigachat_pro, "", "", "Результат проверки GigaChat-Pro сгенерирован"
|
335 |
|
336 |
time.sleep(3)
|
337 |
-
error_message_gigachat_lite = perform_personalization_gigachat(f"{error_check_prompt}\n\n{personalized_messages[2]}",
|
338 |
yield error_check_prompt, error_message_gpt4o, error_message_gigachat_pro, error_message_gigachat_lite, "", "Результат проверки GigaChat-Lite сгенерирован"
|
339 |
|
340 |
try:
|
@@ -483,7 +473,7 @@ with gr.Blocks() as demo:
|
|
483 |
|
484 |
# Привязка кнопок к функциям сохранения
|
485 |
save_gpt4o_btn.click(
|
486 |
-
fn=lambda personalized_message, comment, corrected_message, description, advantages, non_personalized_prompt, non_personalized_message, gender, generation, psychotype, business_stage, industry, legal_form, temperature:
|
487 |
save_to_github(personalized_message, "GPT-4o", comment, corrected_message, description, advantages, non_personalized_prompt, non_personalized_message, gender, generation, psychotype, business_stage, industry, legal_form, temperature),
|
488 |
inputs=[
|
489 |
personalized_output_text_gpt4o,
|
|
|
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 |
|
21 |
# Загрузка данных из Excel-файла
|
22 |
try:
|
|
|
103 |
|
104 |
# Обновленные функции генерации сообщений с учетом обрезки незаконченных предложений
|
105 |
def generate_message_gigachat_pro(prompt, temperature):
|
106 |
+
messages = [SystemMessage(content=prompt)]
|
107 |
+
chat_pro = authenticate_gigachat('GigaChat-Pro', 68, temperature)
|
108 |
+
res = chat_pro(messages)
|
109 |
+
return clean_message(res.content.strip())
|
|
|
|
|
|
|
110 |
|
111 |
def generate_message_gigachat_lite(prompt, temperature):
|
112 |
+
messages = [SystemMessage(content=prompt)]
|
113 |
+
chat_lite = authenticate_gigachat('GigaChat', 68, temperature)
|
114 |
+
res = chat_lite(messages)
|
115 |
+
return clean_message(res.content.strip())
|
|
|
|
|
|
|
|
|
116 |
|
117 |
def generate_message_gigachat_plus(prompt, temperature):
|
118 |
+
messages = [SystemMessage(content=prompt)]
|
119 |
+
chat_plus = authenticate_gigachat('GigaChat-Plus', 68, temperature)
|
120 |
+
res = chat_plus(messages)
|
121 |
+
return clean_message(res.content.strip())
|
122 |
+
|
|
|
|
|
|
|
123 |
|
124 |
+
def generate_message_gpt4o_with_retry(prompt, temperature):
|
125 |
for _ in range(10): # Максимум 10 попыток
|
126 |
+
message = generate_message_gpt4o(prompt, temperature)
|
127 |
if len(message) <= 250:
|
128 |
return message
|
129 |
return message # Возвращаем последнее сгенерированное сообщение, если все попытки не удались
|
130 |
|
131 |
+
def generate_message_gigachat_pro_with_retry(prompt, temperature):
|
132 |
for _ in range(10):
|
133 |
+
message = generate_message_gigachat_pro(prompt, temperature)
|
134 |
if len(message) <= 250:
|
135 |
return message
|
136 |
return message
|
137 |
|
138 |
+
def generate_message_gigachat_lite_with_retry(prompt, temperature):
|
139 |
for _ in range(10):
|
140 |
+
message = generate_message_gigachat_lite(prompt, temperature)
|
141 |
if len(message) <= 250:
|
142 |
return message
|
143 |
return message
|
144 |
|
145 |
+
def generate_message_gigachat_plus_with_retry(prompt, temperature):
|
146 |
for _ in range(10):
|
147 |
+
message = generate_message_gigachat_plus(prompt, temperature)
|
148 |
if len(message) <= 250:
|
149 |
return message
|
150 |
return message
|
151 |
|
152 |
# Обновляем генерацию сообщений для отображения в интерфейсе
|
153 |
+
def generate_messages(description, advantages, gpt4o_temperature, gigachat_pro_temperature, gigachat_lite_temperature, gigachat_plus_temperature, *selected_values):
|
154 |
standard_prompt = generate_standard_prompt(description, advantages, *selected_values)
|
155 |
|
156 |
results = {
|
|
|
163 |
|
164 |
yield results["prompt"], "", "", "", "", "Генерация стандартного промпта завершена"
|
165 |
|
166 |
+
results["gpt4o"] = generate_message_gpt4o_with_retry(standard_prompt, gpt4o_temperature)
|
167 |
gpt4o_length = len(results["gpt4o"])
|
168 |
gpt4o_display = f"{results['gpt4o']}\n\n------\nКоличество знаков: {gpt4o_length}"
|
169 |
yield results["prompt"], gpt4o_display, "", "", "", "Сообщение GPT-4o сгенерировано"
|
170 |
|
171 |
+
results["gigachat_pro"] = generate_message_gigachat_pro_with_retry(standard_prompt, gigachat_pro_temperature)
|
172 |
gigachat_pro_length = len(results["gigachat_pro"])
|
173 |
gigachat_pro_display = f"{results['gigachat_pro']}\n\n------\nКоличество знаков: {gigachat_pro_length}"
|
174 |
yield results["prompt"], gpt4o_display, gigachat_pro_display, "", "", "Сообщение GigaChat-Pro сгенерировано"
|
175 |
|
176 |
time.sleep(2)
|
177 |
|
178 |
+
results["gigachat_lite"] = generate_message_gigachat_lite_with_retry(standard_prompt, gigachat_lite_temperature)
|
179 |
gigachat_lite_length = len(results["gigachat_lite"])
|
180 |
gigachat_lite_display = f"{results['gigachat_lite']}\n\n------\nКоличество знаков: {gigachat_lite_length}"
|
181 |
yield results["prompt"], gpt4o_display, gigachat_pro_display, gigachat_lite_display, "", "Сообщение GigaChat-Lite сгенерировано"
|
182 |
|
183 |
time.sleep(2)
|
184 |
|
185 |
+
results["gigachat_plus"] = generate_message_gigachat_plus_with_retry(standard_prompt, gigachat_plus_temperature)
|
186 |
gigachat_plus_length = len(results["gigachat_plus"])
|
187 |
gigachat_plus_display = f"{results['gigachat_plus']}\n\n------\nКоличество знаков: {gigachat_plus_length}"
|
188 |
yield results["prompt"], gpt4o_display, gigachat_pro_display, gigachat_lite_display, gigachat_plus_display, "Все сообщения сгенерированы"
|
|
|
221 |
return clean_message(result)
|
222 |
|
223 |
|
224 |
+
def perform_personalization_with_retry(standard_message, personalization_prompt):
|
225 |
for _ in range(10): # Максимум 10 попыток
|
226 |
+
message = perform_personalization(standard_message, personalization_prompt)
|
227 |
if len(message) <= 250:
|
228 |
return message
|
229 |
return message # Возвращаем последнее сгенерированное сообщение, если все попытки не удались
|
230 |
|
231 |
+
|
232 |
def perform_personalization_gigachat_with_retry(standard_message, personalization_prompt, model, temperature):
|
233 |
for _ in range(10):
|
234 |
message = perform_personalization_gigachat(standard_message, personalization_prompt, model, temperature)
|
|
|
316 |
|
317 |
error_check_prompt = generate_error_check_prompt()
|
318 |
yield error_check_prompt, "", "", "", "", "Промпт для проверки текста сгенерирован"
|
319 |
+
|
320 |
+
error_message_gpt4o = perform_personalization(f"{error_check_prompt}\n\n{personalized_messages[0]}", error_check_prompt)
|
321 |
yield error_check_prompt, error_message_gpt4o, "", "", "", "Результат проверки GPT-4o сгенерирован"
|
322 |
|
323 |
+
error_message_gigachat_pro = perform_personalization_gigachat(f"{error_check_prompt}\n\n{personalized_messages[1]}", error_check_prompt, "gigachat_pro")
|
324 |
yield error_check_prompt, error_message_gpt4o, error_message_gigachat_pro, "", "", "Результат проверки GigaChat-Pro сгенерирован"
|
325 |
|
326 |
time.sleep(3)
|
327 |
+
error_message_gigachat_lite = perform_personalization_gigachat(f"{error_check_prompt}\n\n{personalized_messages[2]}", error_check_prompt, "gigachat_lite")
|
328 |
yield error_check_prompt, error_message_gpt4o, error_message_gigachat_pro, error_message_gigachat_lite, "", "Результат проверки GigaChat-Lite сгенерирован"
|
329 |
|
330 |
try:
|
|
|
473 |
|
474 |
# Привязка кнопок к функциям сохранения
|
475 |
save_gpt4o_btn.click(
|
476 |
+
fn=lambda personalized_message, comment, corrected_message, description, advantages, non_personalized_prompt, non_personalized_message, gender, generation, psychotype, business_stage, industry, legal_form, temperature:
|
477 |
save_to_github(personalized_message, "GPT-4o", comment, corrected_message, description, advantages, non_personalized_prompt, non_personalized_message, gender, generation, psychotype, business_stage, industry, legal_form, temperature),
|
478 |
inputs=[
|
479 |
personalized_output_text_gpt4o,
|