k-mktr commited on
Commit
90f246e
1 Parent(s): 6216e00

Update model_suggestions.py

Browse files
Files changed (1) hide show
  1. model_suggestions.py +30 -4
model_suggestions.py CHANGED
@@ -24,14 +24,40 @@ def validate_model_url(url: str) -> bool:
24
  return bool(re.match(pattern, url))
25
 
26
  def load_suggestions() -> Dict:
27
- """Load suggestions from local file or initialize if not exists."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  if os.path.exists(SUGGESTIONS_FILE):
29
- with open(SUGGESTIONS_FILE, 'r') as f:
30
- return json.load(f)
31
- return {
 
 
 
 
 
 
32
  "suggestions": {},
33
  "last_updated": datetime.now().isoformat()
34
  }
 
 
 
 
 
35
 
36
  def save_suggestions(suggestions: Dict) -> None:
37
  """Save suggestions to both local file and Nextcloud."""
 
24
  return bool(re.match(pattern, url))
25
 
26
  def load_suggestions() -> Dict:
27
+ """Load suggestions from Nextcloud, fallback to local file, or initialize if neither exists."""
28
+ suggestions = None
29
+
30
+ # First try to load from Nextcloud
31
+ try:
32
+ remote_data = nc.files.download(NEXTCLOUD_SUGGESTIONS_PATH)
33
+ if remote_data:
34
+ suggestions = json.loads(remote_data.decode('utf-8'))
35
+ # Update local file with Nextcloud data
36
+ with open(SUGGESTIONS_FILE, 'w') as f:
37
+ json.dump(suggestions, f, indent=2)
38
+ return suggestions
39
+ except Exception as e:
40
+ print(f"Could not load from Nextcloud: {e}")
41
+
42
+ # If Nextcloud fails, try local file
43
  if os.path.exists(SUGGESTIONS_FILE):
44
+ try:
45
+ with open(SUGGESTIONS_FILE, 'r') as f:
46
+ suggestions = json.load(f)
47
+ return suggestions
48
+ except Exception as e:
49
+ print(f"Could not load from local file: {e}")
50
+
51
+ # If both fail, initialize new suggestions
52
+ suggestions = {
53
  "suggestions": {},
54
  "last_updated": datetime.now().isoformat()
55
  }
56
+
57
+ # Save the new suggestions to both local and Nextcloud
58
+ save_suggestions(suggestions)
59
+
60
+ return suggestions
61
 
62
  def save_suggestions(suggestions: Dict) -> None:
63
  """Save suggestions to both local file and Nextcloud."""