Acasset45654 commited on
Commit
c4b4b45
·
verified ·
1 Parent(s): 608987b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -4
app.py CHANGED
@@ -452,6 +452,33 @@ def save_editable_prompts_to_local_and_hf():
452
  except Exception as e: # pragma: no cover
453
  st.error(f"Erreur préparation données pour sauvegarde: {e}")
454
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
455
  def load_editable_prompts_from_local_or_hf():
456
  # Try HF first, fallback to local
457
  raw_content = None
@@ -1390,9 +1417,8 @@ elif st.session_state.view_mode == "generator":
1390
  current_prompt_config['template'] = new_template
1391
  current_prompt_config['description'] = new_description
1392
  current_prompt_config["updated_at"] = datetime.now().isoformat()
1393
- save_editable_prompts_to_local_and_hf()
1394
  st.success("Configuration sauvegardée!")
1395
- st.rerun()
1396
 
1397
  st.markdown("---")
1398
 
@@ -1420,9 +1446,8 @@ elif st.session_state.view_mode == "generator":
1420
  tag_list = [tag.strip() for tag in new_tags.split(',') if tag.strip()]
1421
  current_prompt_config["tags"] = sorted(list(set(tag_list)))
1422
  current_prompt_config["updated_at"] = datetime.now().isoformat()
1423
- save_editable_prompts_to_local_and_hf()
1424
  st.success("Tags sauvegardés!")
1425
- st.rerun()
1426
  elif st.session_state.view_mode == "inject_manual":
1427
  if st.button("⬅️ Retour à l'accueil", key="back_to_accueil_from_inject"):
1428
  st.session_state.view_mode = "accueil"
 
452
  except Exception as e: # pragma: no cover
453
  st.error(f"Erreur préparation données pour sauvegarde: {e}")
454
 
455
+ def save_to_hf_only():
456
+ """Save to HuggingFace only, without touching local file to prevent app restart"""
457
+ if 'editable_prompts' in st.session_state:
458
+ data_to_save = _preprocess_for_saving(st.session_state.editable_prompts)
459
+ try:
460
+ json_string = json.dumps(data_to_save, indent=4, ensure_ascii=False)
461
+ # Create temporary file for HF upload without overwriting local file
462
+ import tempfile
463
+ with tempfile.NamedTemporaryFile(mode='w', encoding='utf-8', suffix='.json', delete=False) as tmp_file:
464
+ tmp_file.write(json_string)
465
+ tmp_file_path = tmp_file.name
466
+
467
+ hf_ok = False
468
+ if HF_HUB_AVAILABLE:
469
+ hf_ok = save_to_hf_space(tmp_file_path)
470
+
471
+ # Clean up temporary file
472
+ import os
473
+ os.unlink(tmp_file_path)
474
+
475
+ if hf_ok:
476
+ st.toast("💾 Configuration sauvegardée sur Hugging Face!", icon="🤗")
477
+ else:
478
+ st.warning("Sauvegarde HuggingFace échouée.")
479
+ except Exception as e:
480
+ st.error(f"Erreur lors de la sauvegarde: {e}")
481
+
482
  def load_editable_prompts_from_local_or_hf():
483
  # Try HF first, fallback to local
484
  raw_content = None
 
1417
  current_prompt_config['template'] = new_template
1418
  current_prompt_config['description'] = new_description
1419
  current_prompt_config["updated_at"] = datetime.now().isoformat()
1420
+ save_to_hf_only()
1421
  st.success("Configuration sauvegardée!")
 
1422
 
1423
  st.markdown("---")
1424
 
 
1446
  tag_list = [tag.strip() for tag in new_tags.split(',') if tag.strip()]
1447
  current_prompt_config["tags"] = sorted(list(set(tag_list)))
1448
  current_prompt_config["updated_at"] = datetime.now().isoformat()
1449
+ save_to_hf_only()
1450
  st.success("Tags sauvegardés!")
 
1451
  elif st.session_state.view_mode == "inject_manual":
1452
  if st.button("⬅️ Retour à l'accueil", key="back_to_accueil_from_inject"):
1453
  st.session_state.view_mode = "accueil"