broadfield-dev commited on
Commit
cfc3154
·
verified ·
1 Parent(s): 8793199

Update server.py

Browse files
Files changed (1) hide show
  1. server.py +11 -7
server.py CHANGED
@@ -17,7 +17,7 @@ except ImportError:
17
  HF_DATASETS_AVAILABLE = False
18
  Features, Value = None, None
19
 
20
- STORAGE_BACKEND_CONFIG = os.getenv("STORAGE_BACKEND", "JSON").upper()
21
  HF_DATASET_REPO = os.getenv("HF_DATASET_REPO")
22
  HF_TOKEN = os.getenv("HF_TOKEN")
23
  DB_FILE_JSON = "social_data.json"
@@ -74,10 +74,15 @@ def handle_persistence_after_change():
74
  def load_data():
75
  global STORAGE_BACKEND_CONFIG
76
  storage_backend = STORAGE_BACKEND_CONFIG
 
 
 
 
 
77
  with db_lock:
78
  users = {"admin": "password"}
79
- posts = pd.DataFrame(columns=["post_id", "username", "content", "timestamp"])
80
- comments = pd.DataFrame(columns=["comment_id", "post_id", "username", "content", "timestamp", "reply_to_comment_id"])
81
 
82
  if storage_backend == "SQLITE":
83
  try:
@@ -105,12 +110,11 @@ def load_data():
105
  try:
106
  ds_dict = load_dataset(HF_DATASET_REPO, token=HF_TOKEN, trust_remote_code=True)
107
  if ds_dict and all(k in ds_dict for k in ['users', 'posts', 'comments']):
108
- users = dict(zip(ds_dict['users']['username'], ds_dict['users']['password']))
109
  posts = ds_dict['posts'].to_pandas()
110
  comments = ds_dict['comments'].to_pandas()
111
  print("Successfully loaded data from HF Dataset.")
112
- else:
113
- raise ValueError("Dataset dictionary is empty or malformed.")
114
  except Exception as e:
115
  print(f"Could not load from HF Dataset '{HF_DATASET_REPO}'. Attempting to initialize. Error: {e}")
116
  try:
@@ -135,7 +139,7 @@ def load_data():
135
  STORAGE_BACKEND_CONFIG = "RAM"
136
 
137
  if "reply_to_comment_id" not in comments.columns:
138
- comments["reply_to_comment_id"] = None
139
 
140
  post_counter = int(posts['post_id'].max()) if not posts.empty else 0
141
  comment_counter = int(comments['comment_id'].max()) if not comments.empty else 0
 
17
  HF_DATASETS_AVAILABLE = False
18
  Features, Value = None, None
19
 
20
+ STORAGE_BACKEND_CONFIG = os.getenv("STORAGE_BACKEND", "HF_DATASET").upper()
21
  HF_DATASET_REPO = os.getenv("HF_DATASET_REPO")
22
  HF_TOKEN = os.getenv("HF_TOKEN")
23
  DB_FILE_JSON = "social_data.json"
 
74
  def load_data():
75
  global STORAGE_BACKEND_CONFIG
76
  storage_backend = STORAGE_BACKEND_CONFIG
77
+
78
+ # Define explicit dtypes for empty dataframes to ensure schema consistency
79
+ posts_schema = {"post_id": "int64", "username": "object", "content": "object", "timestamp": "object"}
80
+ comments_schema = {"comment_id": "int64", "post_id": "int64", "username": "object", "content": "object", "timestamp": "object", "reply_to_comment_id": "float64"} # Use float for optional int
81
+
82
  with db_lock:
83
  users = {"admin": "password"}
84
+ posts = pd.DataFrame({k: pd.Series(dtype=v) for k, v in posts_schema.items()})
85
+ comments = pd.DataFrame({k: pd.Series(dtype=v) for k, v in comments_schema.items()})
86
 
87
  if storage_backend == "SQLITE":
88
  try:
 
110
  try:
111
  ds_dict = load_dataset(HF_DATASET_REPO, token=HF_TOKEN, trust_remote_code=True)
112
  if ds_dict and all(k in ds_dict for k in ['users', 'posts', 'comments']):
113
+ users = dict(zip(ds_dict['users']['username'], ds_dict['users']['password'])) if ds_dict['users'].num_rows > 0 else {"admin":"password"}
114
  posts = ds_dict['posts'].to_pandas()
115
  comments = ds_dict['comments'].to_pandas()
116
  print("Successfully loaded data from HF Dataset.")
117
+ else: raise ValueError("Dataset dictionary is empty or malformed.")
 
118
  except Exception as e:
119
  print(f"Could not load from HF Dataset '{HF_DATASET_REPO}'. Attempting to initialize. Error: {e}")
120
  try:
 
139
  STORAGE_BACKEND_CONFIG = "RAM"
140
 
141
  if "reply_to_comment_id" not in comments.columns:
142
+ comments["reply_to_comment_id"] = pd.Series(dtype='float64')
143
 
144
  post_counter = int(posts['post_id'].max()) if not posts.empty else 0
145
  comment_counter = int(comments['comment_id'].max()) if not comments.empty else 0