Acasset45654 commited on
Commit
257eda4
·
verified ·
1 Parent(s): cece0f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -16
app.py CHANGED
@@ -35,6 +35,27 @@ def save_to_hf_space(json_path, repo_id=HF_REPO_ID, token=HF_TOKEN):
35
  st.warning(f"Erreur lors de la sauvegarde sur Hugging Face: {e}")
36
  return False
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  # --- Load from Hugging Face Space ---
39
  def load_from_hf_space(repo_id=HF_REPO_ID, token=HF_TOKEN):
40
  if not HF_HUB_AVAILABLE:
@@ -436,11 +457,9 @@ def save_editable_prompts_to_local_and_hf():
436
  json_string = json.dumps(data_to_save, indent=4, ensure_ascii=False)
437
  local_ok = save_to_local_file(json_string)
438
  hf_ok = False
439
- # Save to HF Space if possible
440
- with open(LOCAL_DATA_FILENAME, 'w', encoding='utf-8') as f:
441
- f.write(json_string)
442
  if HF_HUB_AVAILABLE:
443
- hf_ok = save_to_hf_space(LOCAL_DATA_FILENAME)
444
  if local_ok and hf_ok:
445
  st.toast("💾 Données sauvegardées localement et sur Hugging Face!", icon="💾")
446
  elif local_ok:
@@ -458,19 +477,10 @@ def save_to_hf_only():
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="🤗")
@@ -503,7 +513,7 @@ def load_editable_prompts_from_local_or_hf():
503
  json_string_init = json.dumps(data_to_save_init, indent=4, ensure_ascii=False)
504
  save_to_local_file(json_string_init)
505
  if HF_HUB_AVAILABLE:
506
- save_to_hf_space(LOCAL_DATA_FILENAME)
507
  st.info("Modèles par défaut sauvegardés pour initialisation.")
508
  except Exception as e: # pragma: no cover
509
  st.error(f"Erreur sauvegarde initiale: {e}")
 
35
  st.warning(f"Erreur lors de la sauvegarde sur Hugging Face: {e}")
36
  return False
37
 
38
+ # --- Save to Hugging Face Space from memory (no file write) ---
39
+ def save_to_hf_space_from_memory(json_string, repo_id=HF_REPO_ID, token=HF_TOKEN):
40
+ if not HF_HUB_AVAILABLE:
41
+ st.warning("huggingface_hub n'est pas installé. Sauvegarde sur HF désactivée.")
42
+ return False
43
+ try:
44
+ import io
45
+ api = HfApi()
46
+ api.upload_file(
47
+ path_or_fileobj=io.BytesIO(json_string.encode('utf-8')),
48
+ path_in_repo=HF_JSON_FILENAME,
49
+ repo_id=repo_id,
50
+ repo_type="space",
51
+ token=token
52
+ )
53
+ st.toast("💾 Données sauvegardées sur Hugging Face Space!", icon="🤗")
54
+ return True
55
+ except Exception as e:
56
+ st.warning(f"Erreur lors de la sauvegarde sur Hugging Face: {e}")
57
+ return False
58
+
59
  # --- Load from Hugging Face Space ---
60
  def load_from_hf_space(repo_id=HF_REPO_ID, token=HF_TOKEN):
61
  if not HF_HUB_AVAILABLE:
 
457
  json_string = json.dumps(data_to_save, indent=4, ensure_ascii=False)
458
  local_ok = save_to_local_file(json_string)
459
  hf_ok = False
460
+ # Save to HF Space from memory (no file write to avoid restart)
 
 
461
  if HF_HUB_AVAILABLE:
462
+ hf_ok = save_to_hf_space_from_memory(json_string)
463
  if local_ok and hf_ok:
464
  st.toast("💾 Données sauvegardées localement et sur Hugging Face!", icon="💾")
465
  elif local_ok:
 
477
  data_to_save = _preprocess_for_saving(st.session_state.editable_prompts)
478
  try:
479
  json_string = json.dumps(data_to_save, indent=4, ensure_ascii=False)
480
+ # Save directly from memory to avoid any file writes
 
 
 
 
 
481
  hf_ok = False
482
  if HF_HUB_AVAILABLE:
483
+ hf_ok = save_to_hf_space_from_memory(json_string)
 
 
 
 
484
 
485
  if hf_ok:
486
  st.toast("💾 Configuration sauvegardée sur Hugging Face!", icon="🤗")
 
513
  json_string_init = json.dumps(data_to_save_init, indent=4, ensure_ascii=False)
514
  save_to_local_file(json_string_init)
515
  if HF_HUB_AVAILABLE:
516
+ save_to_hf_space_from_memory(json_string_init)
517
  st.info("Modèles par défaut sauvegardés pour initialisation.")
518
  except Exception as e: # pragma: no cover
519
  st.error(f"Erreur sauvegarde initiale: {e}")