fruitpicker01 commited on
Commit
5fa477f
·
verified ·
1 Parent(s): b0abcf4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -25
app.py CHANGED
@@ -16,7 +16,7 @@ client = OpenAI(
16
  base_url="https://api.x.ai/v1",
17
  )
18
  token = os.getenv('GITHUB_TOKEN')
19
- repo = "fruitpicker01/Storage_dev"
20
  current_request_index = -1
21
 
22
  def load_dropdown_data(file_path, sheet_name, column_name):
@@ -233,8 +233,7 @@ def call_model(model_prompt):
233
  return completion.choices[0].message.content.strip()
234
 
235
  def correct_dash_usage(text):
236
- # опущено из-за длины, вставьте ваш код
237
- return text
238
 
239
  def clean_message(message):
240
  if not message.endswith(('.', '!', '?')):
@@ -244,21 +243,8 @@ def clean_message(message):
244
  return message
245
 
246
  def generate_message_with_retry(model_prompt):
247
- last_message = ""
248
- for _ in range(10):
249
- msg = call_model(model_prompt)
250
- msg = correct_dash_usage(msg)
251
- msg = clean_message(msg)
252
- length = len(msg)
253
- if 160 <= length <= 250:
254
- # Добавляем информацию о количестве знаков
255
- msg += f"\n\n------\nКоличество знаков: {length}"
256
- return msg
257
- last_message = msg
258
- # Если не удалось подобрать длину
259
- length = len(last_message)
260
- last_message += f"\n\n------\nКоличество знаков: {length}"
261
- return last_message
262
 
263
  def update_prompts_on_params_change(description, product_name, benefits, key_message,
264
  gender, generation, psychotype, business_stage, industry, opf):
@@ -268,12 +254,12 @@ def update_prompts_on_params_change(description, product_name, benefits, key_mes
268
  business_stage, industry, opf)
269
  return chosen_approach, prompt_1, prompt_2
270
 
271
- def save_user_request_to_github(description, advantages, key_message, approach, personalization_params):
272
  global current_request_index
273
  current_request_index = -1
274
  data_to_save = {
275
  "description": description,
276
- "advantages": advantages,
277
  "key_message": key_message,
278
  "approach": approach,
279
  "personalization_params": personalization_params,
@@ -319,13 +305,13 @@ def load_previous_user_request_from_github():
319
  if file_response.status_code == 200:
320
  data = json.loads(file_response.text)
321
  description = data.get('description', "")
322
- advantages = data.get('advantages', "")
323
  key_message = data.get('key_message', "")
324
  approach = data.get('approach', "")
325
  personalization_params = data.get('personalization_params', [None]*6)
326
  if len(personalization_params) < 6:
327
  personalization_params += [None]*(6-len(personalization_params))
328
- return description, advantages, key_message, approach, *personalization_params
329
  else:
330
  print(f"Ошибка при загрузке файла: {file_response.status_code}")
331
  return "", "", "", "", None, None, None, None, None, None
@@ -380,6 +366,7 @@ with gr.Blocks(theme="default") as demo:
380
  product_name = gr.Textbox(label="Наименование продукта", lines=1, value="", interactive=True)
381
  benefits = gr.Textbox(label="Преимущества", lines=9, value="", interactive=True)
382
  key_message = gr.Textbox(label="Ключевое сообщение", lines=2, value="", interactive=True)
 
383
  def on_product_change(selected, description, product_name, benefits, key_message,
384
  gender, generation, psychotype, business_stage, industry, opf):
385
  if selected == "Свой продукт":
@@ -407,6 +394,7 @@ with gr.Blocks(theme="default") as demo:
407
  gr.update(value=new_ben, interactive=(selected=="Свой продукт")),
408
  gr.update(value=new_kmsg, interactive=(selected=="Свой продукт")),
409
  chosen_approach_val, p1, p2)
 
410
  with gr.Column(scale=1):
411
  gr.Markdown("**Клиент**")
412
  gender_dropdown = gr.Dropdown(label="Пол", choices=["Не выбрано"]+genders, value=None)
@@ -432,16 +420,21 @@ with gr.Blocks(theme="default") as demo:
432
  model_2_name = gr.Textbox(label="Модель 2", value="Grok-2-1212", interactive=False)
433
  prompt_2 = gr.Textbox(label="Промпт 2", value="", interactive=False, lines=10)
434
  sms_2 = gr.Textbox(label="SMS 2", lines=3, value="", interactive=False)
 
435
  with gr.Row():
436
  prefer_sms_1_btn = gr.Button("Я предпочитаю это SMS (кнопка пока не работает)")
437
  prefer_sms_2_btn = gr.Button("Я предпочитаю это SMS (кнопка пока не работает)")
 
438
  regen_btn = gr.Button("Перегенерировать SMS (не нравится ни одно из SMS) (кнопка пока не работает)")
 
439
  with gr.Row():
440
  comment_sms_1 = gr.Textbox(label="Комментарий к SMS 1", lines=2, value="")
441
  comment_sms_2 = gr.Textbox(label="Комментарий к SMS 2", lines=2, value="")
 
442
  with gr.Row():
443
  corrected_sms_1 = gr.Textbox(label="Откорректированное SMS 1", lines=3, value="")
444
  corrected_sms_2 = gr.Textbox(label="Откорректированное SMS 2", lines=3, value="")
 
445
  with gr.Row():
446
  save_sms_1_btn = gr.Button("Сохранить в базу (кнопка пока не работает)")
447
  save_sms_2_btn = gr.Button("Сохранить в базу (кнопка пока не работает)")
@@ -454,6 +447,7 @@ with gr.Blocks(theme="default") as demo:
454
  outputs=[description, product_name, benefits, key_message,
455
  chosen_approach, prompt_1, prompt_2]
456
  )
 
457
  client_params = [gender_dropdown, generation_dropdown, psychotype_dropdown,
458
  business_stage_dropdown, industry_dropdown, opf_dropdown]
459
  for cp in client_params:
@@ -464,6 +458,7 @@ with gr.Blocks(theme="default") as demo:
464
  business_stage_dropdown, industry_dropdown, opf_dropdown],
465
  outputs=[chosen_approach, prompt_1, prompt_2]
466
  )
 
467
  create_personal_sms_btn.click(
468
  fn=generate_personalized_sms,
469
  inputs=[description, product_name, benefits, key_message,
@@ -477,12 +472,12 @@ with gr.Blocks(theme="default") as demo:
477
  loaded_data = load_previous_user_request_from_github()
478
  if not loaded_data or len(loaded_data) < 10:
479
  return ("", "", "", "", None, None, None, None, None, None, "", "", "")
480
- description_val, advantages_val, key_message_val, approach_val = loaded_data[0], loaded_data[1], loaded_data[2], loaded_data[3]
481
  gender_val, generation_val, psychotype_val, business_stage_val, industry_val, opf_val = loaded_data[4:10]
482
- chosen_approach_val, p1, p2 = update_prompts_on_params_change(description_val, approach_val, advantages_val, key_message_val,
483
  gender_val, generation_val, psychotype_val,
484
  business_stage_val, industry_val, opf_val)
485
- return (description_val, approach_val, advantages_val, key_message_val,
486
  gender_val, generation_val, psychotype_val, business_stage_val, industry_val, opf_val,
487
  chosen_approach_val, p1, p2)
488
 
 
16
  base_url="https://api.x.ai/v1",
17
  )
18
  token = os.getenv('GITHUB_TOKEN')
19
+ repo = "fruitpicker01/Storage_Anastasia"
20
  current_request_index = -1
21
 
22
  def load_dropdown_data(file_path, sheet_name, column_name):
 
233
  return completion.choices[0].message.content.strip()
234
 
235
  def correct_dash_usage(text):
236
+ return text # Замените на ваш полный код корректировки, опущено для краткости
 
237
 
238
  def clean_message(message):
239
  if not message.endswith(('.', '!', '?')):
 
243
  return message
244
 
245
  def generate_message_with_retry(model_prompt):
246
+ # Замените на ваш код с перегенерацией, опущено для краткости
247
+ return model_prompt
 
 
 
 
 
 
 
 
 
 
 
 
 
248
 
249
  def update_prompts_on_params_change(description, product_name, benefits, key_message,
250
  gender, generation, psychotype, business_stage, industry, opf):
 
254
  business_stage, industry, opf)
255
  return chosen_approach, prompt_1, prompt_2
256
 
257
+ def save_user_request_to_github(description, benefits, key_message, approach, personalization_params):
258
  global current_request_index
259
  current_request_index = -1
260
  data_to_save = {
261
  "description": description,
262
+ "benefits": benefits,
263
  "key_message": key_message,
264
  "approach": approach,
265
  "personalization_params": personalization_params,
 
305
  if file_response.status_code == 200:
306
  data = json.loads(file_response.text)
307
  description = data.get('description', "")
308
+ benefits = data.get('benefits', "")
309
  key_message = data.get('key_message', "")
310
  approach = data.get('approach', "")
311
  personalization_params = data.get('personalization_params', [None]*6)
312
  if len(personalization_params) < 6:
313
  personalization_params += [None]*(6-len(personalization_params))
314
+ return description, benefits, key_message, approach, *personalization_params
315
  else:
316
  print(f"Ошибка при загрузке файла: {file_response.status_code}")
317
  return "", "", "", "", None, None, None, None, None, None
 
366
  product_name = gr.Textbox(label="Наименование продукта", lines=1, value="", interactive=True)
367
  benefits = gr.Textbox(label="Преимущества", lines=9, value="", interactive=True)
368
  key_message = gr.Textbox(label="Ключевое сообщение", lines=2, value="", interactive=True)
369
+
370
  def on_product_change(selected, description, product_name, benefits, key_message,
371
  gender, generation, psychotype, business_stage, industry, opf):
372
  if selected == "Свой продукт":
 
394
  gr.update(value=new_ben, interactive=(selected=="Свой продукт")),
395
  gr.update(value=new_kmsg, interactive=(selected=="Свой продукт")),
396
  chosen_approach_val, p1, p2)
397
+
398
  with gr.Column(scale=1):
399
  gr.Markdown("**Клиент**")
400
  gender_dropdown = gr.Dropdown(label="Пол", choices=["Не выбрано"]+genders, value=None)
 
420
  model_2_name = gr.Textbox(label="Модель 2", value="Grok-2-1212", interactive=False)
421
  prompt_2 = gr.Textbox(label="Промпт 2", value="", interactive=False, lines=10)
422
  sms_2 = gr.Textbox(label="SMS 2", lines=3, value="", interactive=False)
423
+
424
  with gr.Row():
425
  prefer_sms_1_btn = gr.Button("Я предпочитаю это SMS (кнопка пока не работает)")
426
  prefer_sms_2_btn = gr.Button("Я предпочитаю это SMS (кнопка пока не работает)")
427
+
428
  regen_btn = gr.Button("Перегенерировать SMS (не нравится ни одно из SMS) (кнопка пока не работает)")
429
+
430
  with gr.Row():
431
  comment_sms_1 = gr.Textbox(label="Комментарий к SMS 1", lines=2, value="")
432
  comment_sms_2 = gr.Textbox(label="Комментарий к SMS 2", lines=2, value="")
433
+
434
  with gr.Row():
435
  corrected_sms_1 = gr.Textbox(label="Откорректированное SMS 1", lines=3, value="")
436
  corrected_sms_2 = gr.Textbox(label="Откорректированное SMS 2", lines=3, value="")
437
+
438
  with gr.Row():
439
  save_sms_1_btn = gr.Button("Сохранить в базу (кнопка пока не работает)")
440
  save_sms_2_btn = gr.Button("Сохранить в базу (кнопка пока не работает)")
 
447
  outputs=[description, product_name, benefits, key_message,
448
  chosen_approach, prompt_1, prompt_2]
449
  )
450
+
451
  client_params = [gender_dropdown, generation_dropdown, psychotype_dropdown,
452
  business_stage_dropdown, industry_dropdown, opf_dropdown]
453
  for cp in client_params:
 
458
  business_stage_dropdown, industry_dropdown, opf_dropdown],
459
  outputs=[chosen_approach, prompt_1, prompt_2]
460
  )
461
+
462
  create_personal_sms_btn.click(
463
  fn=generate_personalized_sms,
464
  inputs=[description, product_name, benefits, key_message,
 
472
  loaded_data = load_previous_user_request_from_github()
473
  if not loaded_data or len(loaded_data) < 10:
474
  return ("", "", "", "", None, None, None, None, None, None, "", "", "")
475
+ description_val, benefits_val, key_message_val, approach_val = loaded_data[0], loaded_data[1], loaded_data[2], loaded_data[3]
476
  gender_val, generation_val, psychotype_val, business_stage_val, industry_val, opf_val = loaded_data[4:10]
477
+ chosen_approach_val, p1, p2 = update_prompts_on_params_change(description_val, product_name.value, benefits_val, key_message_val,
478
  gender_val, generation_val, psychotype_val,
479
  business_stage_val, industry_val, opf_val)
480
+ return (description_val, product_name.value, benefits_val, key_message_val,
481
  gender_val, generation_val, psychotype_val, business_stage_val, industry_val, opf_val,
482
  chosen_approach_val, p1, p2)
483