Spaces:
Sleeping
Sleeping
Update server.py
Browse files
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", "
|
21 |
HF_DATASET_REPO = os.getenv("HF_DATASET_REPO")
|
22 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
23 |
DB_FILE_JSON = "social_data.json"
|
@@ -104,17 +104,29 @@ def load_data():
|
|
104 |
if all([HF_DATASETS_AVAILABLE, HF_TOKEN, HF_DATASET_REPO]):
|
105 |
try:
|
106 |
ds_dict = load_dataset(HF_DATASET_REPO, token=HF_TOKEN, trust_remote_code=True)
|
107 |
-
|
108 |
-
|
109 |
-
|
|
|
|
|
|
|
|
|
110 |
except Exception as e:
|
111 |
print(f"Could not load from HF Dataset '{HF_DATASET_REPO}'. Attempting to initialize. Error: {e}")
|
112 |
try:
|
113 |
user_features = Features({'username': Value('string'), 'password': Value('string')})
|
114 |
post_features = Features({'post_id': Value('int64'), 'username': Value('string'), 'content': Value('string'), 'timestamp': Value('string')})
|
115 |
comment_features = Features({'comment_id': Value('int64'), 'post_id': Value('int64'), 'username': Value('string'), 'content': Value('string'), 'timestamp': Value('string'), 'reply_to_comment_id': Value('int64')})
|
116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
dataset_dict.push_to_hub(HF_DATASET_REPO, token=HF_TOKEN, private=True)
|
|
|
118 |
except Exception as e_push:
|
119 |
print(f"CRITICAL: Failed to create new HF Dataset. Falling back to RAM. Push Error: {e_push}")
|
120 |
STORAGE_BACKEND_CONFIG = "RAM"
|
@@ -243,4 +255,4 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Social App") as demo:
|
|
243 |
gr.Interface(api_get_feed, None, "dataframe", api_name="get_feed")
|
244 |
|
245 |
if __name__ == "__main__":
|
246 |
-
demo.queue().launch(server_name="0.0.0.0", server_port=
|
|
|
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"
|
|
|
104 |
if all([HF_DATASETS_AVAILABLE, HF_TOKEN, HF_DATASET_REPO]):
|
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:
|
117 |
user_features = Features({'username': Value('string'), 'password': Value('string')})
|
118 |
post_features = Features({'post_id': Value('int64'), 'username': Value('string'), 'content': Value('string'), 'timestamp': Value('string')})
|
119 |
comment_features = Features({'comment_id': Value('int64'), 'post_id': Value('int64'), 'username': Value('string'), 'content': Value('string'), 'timestamp': Value('string'), 'reply_to_comment_id': Value('int64')})
|
120 |
+
|
121 |
+
initial_users_df = pd.DataFrame(list(users.items()), columns=['username', 'password'])
|
122 |
+
|
123 |
+
dataset_dict = DatasetDict({
|
124 |
+
'users': Dataset.from_pandas(initial_users_df, features=user_features),
|
125 |
+
'posts': Dataset.from_pandas(posts, features=post_features),
|
126 |
+
'comments': Dataset.from_pandas(comments, features=comment_features)
|
127 |
+
})
|
128 |
dataset_dict.push_to_hub(HF_DATASET_REPO, token=HF_TOKEN, private=True)
|
129 |
+
print(f"Successfully initialized new empty HF Dataset at {HF_DATASET_REPO}.")
|
130 |
except Exception as e_push:
|
131 |
print(f"CRITICAL: Failed to create new HF Dataset. Falling back to RAM. Push Error: {e_push}")
|
132 |
STORAGE_BACKEND_CONFIG = "RAM"
|
|
|
255 |
gr.Interface(api_get_feed, None, "dataframe", api_name="get_feed")
|
256 |
|
257 |
if __name__ == "__main__":
|
258 |
+
demo.queue().launch(server_name="0.0.0.0", server_port=7861, share=False)
|