djkesu commited on
Commit
e302830
1 Parent(s): 5e8afa4

replaced backed db with sqlite

Browse files
Files changed (2) hide show
  1. app_utils/conf.py +42 -7
  2. app_utils/funcs.py +0 -1
app_utils/conf.py CHANGED
@@ -1,10 +1,9 @@
1
- import shelve
2
  from pathlib import Path
3
- from typing import Any
4
 
5
  from pydantic import BaseModel
6
 
7
-
8
  class PersistentSettings(BaseModel):
9
  """
10
  This pydantic model will try to initialize itself from
@@ -15,16 +14,52 @@ class PersistentSettings(BaseModel):
15
  """
16
 
17
  def __init__(self, **data: Any):
18
- with shelve.open("config.db", flag="n", protocol=2, writeback=False,) as db:
19
- super().__init__(**db.get("settings", default={}), **data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  def update(self, **data: Any) -> None:
22
  """
23
  Persist the pydantic-dict that represents the model
24
  """
25
- with shelve.open("config.db", flag="n", protocol=2, writeback=False,) as db:
26
- db["settings"] = {**self.dict(), **data}
 
 
 
 
 
 
 
 
 
 
27
 
 
 
 
 
 
28
 
29
  class TortoiseConfig(PersistentSettings):
30
  EXTRA_VOICES_DIR: str = ""
 
1
+ import sqlite3
2
  from pathlib import Path
3
+ from typing import Any, Dict
4
 
5
  from pydantic import BaseModel
6
 
 
7
  class PersistentSettings(BaseModel):
8
  """
9
  This pydantic model will try to initialize itself from
 
14
  """
15
 
16
  def __init__(self, **data: Any):
17
+ # Connect to the SQLite database
18
+ self.conn = sqlite3.connect("config.db")
19
+
20
+ # Create a table for settings if it doesn't exist
21
+ self.conn.execute("""
22
+ CREATE TABLE IF NOT EXISTS settings (
23
+ key TEXT PRIMARY KEY,
24
+ value TEXT
25
+ )
26
+ """)
27
+
28
+ # Fetch settings from the database and initialize
29
+ super().__init__(**self.fetch_settings(), **data)
30
+
31
+ def fetch_settings(self) -> Dict[str, Any]:
32
+ """
33
+ Retrieve settings from the database
34
+ """
35
+ cursor = self.conn.cursor()
36
+ cursor.execute("SELECT key, value FROM settings")
37
+ settings = dict(cursor.fetchall())
38
+ cursor.close()
39
+ return settings
40
 
41
  def update(self, **data: Any) -> None:
42
  """
43
  Persist the pydantic-dict that represents the model
44
  """
45
+ cursor = self.conn.cursor()
46
+
47
+ # Update or insert each key-value pair into the database
48
+ for key, value in {**self.dict(), **data}.items():
49
+ cursor.execute(
50
+ "INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)",
51
+ (key, value)
52
+ )
53
+
54
+ # Commit the changes to the database
55
+ self.conn.commit()
56
+ cursor.close()
57
 
58
+ def close(self) -> None:
59
+ """
60
+ Close the database connection
61
+ """
62
+ self.conn.close()
63
 
64
  class TortoiseConfig(PersistentSettings):
65
  EXTRA_VOICES_DIR: str = ""
app_utils/funcs.py CHANGED
@@ -52,6 +52,5 @@ def list_voices(extra_voices_dir: Optional[str]):
52
 
53
  @st.cache_resource(max_entries=1)
54
  def load_voice_conditionings(voice, extra_voices_ls):
55
- gc.collect()
56
  voice_samples, conditioning_latents = load_voices(voice, extra_voices_ls)
57
  return voice_samples, conditioning_latents
 
52
 
53
  @st.cache_resource(max_entries=1)
54
  def load_voice_conditionings(voice, extra_voices_ls):
 
55
  voice_samples, conditioning_latents = load_voices(voice, extra_voices_ls)
56
  return voice_samples, conditioning_latents