broadfield-dev commited on
Commit
f51755e
·
verified ·
1 Parent(s): d7ef573

Create server.py

Browse files
Files changed (1) hide show
  1. server.py +318 -0
server.py ADDED
@@ -0,0 +1,318 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import threading
4
+ from datetime import datetime
5
+ import os
6
+ import json
7
+ import sqlite3
8
+ import time
9
+ from dotenv import load_dotenv
10
+
11
+ DEMO_MODE = os.getenv("DEMO_MODE", "true").lower() == 'true'
12
+ # --- Load Environment & Configuration ---
13
+ load_dotenv()
14
+ try:
15
+ from datasets import load_dataset, Dataset, DatasetDict, Features, Value
16
+ HF_DATASETS_AVAILABLE = True
17
+ except ImportError:
18
+ HF_DATASETS_AVAILABLE = False
19
+ Features, Value = None, None # Define placeholders if import fails
20
+
21
+ STORAGE_BACKEND_CONFIG = os.getenv("STORAGE_BACKEND", "HF_DATASET").upper()
22
+ HF_DATASET_REPO = os.getenv("HF_DATASET_REPO")
23
+ HF_TOKEN = os.getenv("HF_TOKEN")
24
+ HF_BACKUP_THRESHOLD = int(os.getenv("HF_BACKUP_THRESHOLD", 10))
25
+ DB_FILE_JSON = "social_data.json"
26
+ DB_FILE_SQLITE = "social_data.db"
27
+
28
+ db_lock = threading.Lock()
29
+ dirty_operations_count = 0
30
+
31
+ # --- Database Initialization and Persistence ---
32
+
33
+ def force_persist_data():
34
+ global dirty_operations_count
35
+ with db_lock:
36
+ storage_backend = STORAGE_BACKEND_CONFIG # Use a local copy for thread safety
37
+ if storage_backend == "RAM":
38
+ return True, "RAM backend. No persistence."
39
+ elif storage_backend == "SQLITE":
40
+ with sqlite3.connect(DB_FILE_SQLITE) as conn:
41
+ users_df = pd.DataFrame(list(users_db.items()), columns=['username', 'password'])
42
+ users_df.to_sql('users', conn, if_exists='replace', index=False)
43
+ posts_df.to_sql('posts', conn, if_exists='replace', index=False)
44
+ comments_df.to_sql('comments', conn, if_exists='replace', index=False)
45
+ return True, "Successfully saved to SQLite."
46
+ elif storage_backend == "JSON":
47
+ with open(DB_FILE_JSON, "w") as f:
48
+ json.dump({"users": users_db, "posts": posts_df.to_dict('records'), "comments": comments_df.to_dict('records')}, f, indent=2)
49
+ return True, "Successfully saved to JSON file."
50
+ elif storage_backend == "HF_DATASET":
51
+ if not all([HF_DATASETS_AVAILABLE, HF_TOKEN, HF_DATASET_REPO]):
52
+ return False, "HF_DATASET backend is not configured correctly."
53
+ try:
54
+ print("Pushing data to Hugging Face Hub...")
55
+ dataset_dict = DatasetDict({
56
+ 'users': Dataset.from_pandas(pd.DataFrame(list(users_db.items()), columns=['username', 'password'])),
57
+ 'posts': Dataset.from_pandas(posts_df),
58
+ 'comments': Dataset.from_pandas(comments_df)
59
+ })
60
+ dataset_dict.push_to_hub(HF_DATASET_REPO, token=HF_TOKEN, private=True)
61
+ dirty_operations_count = 0
62
+ return True, f"Successfully pushed data to {HF_DATASET_REPO}."
63
+ except Exception as e:
64
+ return False, f"Error pushing to Hugging Face Hub: {e}"
65
+ return False, "Unknown backend."
66
+
67
+ def handle_persistence_after_change():
68
+ global dirty_operations_count
69
+ storage_backend = STORAGE_BACKEND_CONFIG
70
+ if storage_backend in ["JSON", "SQLITE"]:
71
+ force_persist_data()
72
+ elif storage_backend == "HF_DATASET":
73
+ with db_lock:
74
+ dirty_operations_count += 1
75
+ print(f"HF_DATASET: {dirty_operations_count}/{HF_BACKUP_THRESHOLD} operations until next auto-backup.")
76
+ if dirty_operations_count >= HF_BACKUP_THRESHOLD:
77
+ print(f"Threshold of {HF_BACKUP_THRESHOLD} reached. Triggering auto-backup.")
78
+ force_persist_data()
79
+
80
+ def load_data():
81
+ global STORAGE_BACKEND_CONFIG
82
+ storage_backend = STORAGE_BACKEND_CONFIG
83
+ with db_lock:
84
+ # Default empty structures
85
+ users, posts, comments = {"admin": "password"}, pd.DataFrame(columns=["post_id", "username", "content", "timestamp"]), pd.DataFrame(columns=["comment_id", "post_id", "username", "content", "timestamp"])
86
+
87
+ if storage_backend == "SQLITE":
88
+ try:
89
+ with sqlite3.connect(DB_FILE_SQLITE) as conn:
90
+ cursor = conn.cursor()
91
+ cursor.execute("CREATE TABLE IF NOT EXISTS users (username TEXT PRIMARY KEY, password TEXT NOT NULL)")
92
+ cursor.execute("CREATE TABLE IF NOT EXISTS posts (post_id INTEGER PRIMARY KEY, username TEXT, content TEXT, timestamp TEXT)")
93
+ cursor.execute("CREATE TABLE IF NOT EXISTS comments (comment_id INTEGER PRIMARY KEY, post_id INTEGER, username TEXT, content TEXT, timestamp TEXT)")
94
+ cursor.execute("INSERT OR IGNORE INTO users (username, password) VALUES (?, ?)", ("admin", "password"))
95
+ conn.commit()
96
+ users = dict(conn.execute("SELECT username, password FROM users").fetchall())
97
+ posts = pd.read_sql_query("SELECT * FROM posts", conn)
98
+ comments = pd.read_sql_query("SELECT * FROM comments", conn)
99
+ except Exception as e:
100
+ print(f"CRITICAL: Failed to load or create SQLite DB at '{DB_FILE_SQLITE}'. Falling back to RAM. Error: {e}")
101
+ STORAGE_BACKEND_CONFIG = "RAM"
102
+
103
+ elif storage_backend == "JSON":
104
+ if os.path.exists(DB_FILE_JSON):
105
+ try:
106
+ with open(DB_FILE_JSON, "r") as f:
107
+ data = json.load(f)
108
+ users, posts, comments = data.get("users", users), pd.DataFrame(data.get("posts", [])), pd.DataFrame(data.get("comments", []))
109
+ except (json.JSONDecodeError, KeyError):
110
+ print(f"Warning: JSON file '{DB_FILE_JSON}' is corrupted or empty. Starting with fresh data.")
111
+ else:
112
+ print(f"JSON file '{DB_FILE_JSON}' not found. Will be created on first change.")
113
+
114
+ elif storage_backend == "HF_DATASET":
115
+ if all([HF_DATASETS_AVAILABLE, HF_TOKEN, HF_DATASET_REPO]):
116
+ try:
117
+ print(f"Attempting to load data from HF Dataset: {HF_DATASET_REPO}")
118
+ ds_dict = load_dataset(HF_DATASET_REPO, token=HF_TOKEN, trust_remote_code=True)
119
+ users = dict(zip(ds_dict['users']['username'], ds_dict['users']['password']))
120
+ posts = ds_dict['posts'].to_pandas()
121
+ comments = ds_dict['comments'].to_pandas()
122
+ print("Successfully loaded data from HF Dataset.")
123
+ except Exception as e:
124
+ print(f"Could not load from HF Dataset '{HF_DATASET_REPO}'. Attempting to initialize a new one. Error: {e}")
125
+ try:
126
+ user_features = Features({'username': Value('string'), 'password': Value('string')})
127
+ post_features = Features({'post_id': Value('int64'), 'username': Value('string'), 'content': Value('string'), 'timestamp': Value('string')})
128
+ comment_features = Features({'comment_id': Value('int64'), 'post_id': Value('int64'), 'username': Value('string'), 'content': Value('string'), 'timestamp': Value('string')})
129
+
130
+ dataset_dict = DatasetDict({
131
+ 'users': Dataset.from_pandas(pd.DataFrame(list(users.items()), columns=['username', 'password']), features=user_features),
132
+ 'posts': Dataset.from_pandas(posts, features=post_features),
133
+ 'comments': Dataset.from_pandas(comments, features=comment_features)
134
+ })
135
+ dataset_dict.push_to_hub(HF_DATASET_REPO, token=HF_TOKEN, private=True)
136
+ print(f"Successfully initialized new empty HF Dataset at {HF_DATASET_REPO}.")
137
+ except Exception as e_push:
138
+ print(f"CRITICAL: Failed to create new HF Dataset. Falling back to RAM for this session. Push Error: {e_push}")
139
+ STORAGE_BACKEND_CONFIG = "RAM"
140
+ else:
141
+ print("HF_DATASET backend not fully configured (check env vars and library install). Falling back to RAM for this session.")
142
+ STORAGE_BACKEND_CONFIG = "RAM"
143
+
144
+ # Final validation of DataFrame structures
145
+ if not isinstance(posts, pd.DataFrame) or "post_id" not in posts.columns:
146
+ posts = pd.DataFrame(columns=["post_id", "username", "content", "timestamp"])
147
+ if not isinstance(comments, pd.DataFrame) or "comment_id" not in comments.columns:
148
+ comments = pd.DataFrame(columns=["comment_id", "post_id", "username", "content", "timestamp"])
149
+
150
+ post_counter = int(posts['post_id'].max()) if not posts.empty else 0
151
+ comment_counter = int(comments['comment_id'].max()) if not comments.empty else 0
152
+ return users, posts, comments, post_counter, comment_counter
153
+
154
+ users_db, posts_df, comments_df, post_counter, comment_counter = load_data()
155
+
156
+ # --- API Functions ---
157
+ def api_register(username, password):
158
+ if not username or not password: return "[Auth API] Failed: Username/password cannot be empty."
159
+ with db_lock:
160
+ if username in users_db: return f"[Auth API] Failed: Username '{username}' already exists."
161
+ users_db[username] = password
162
+ handle_persistence_after_change()
163
+ return f"[Auth API] Success: User '{username}' registered."
164
+
165
+ def api_login(username, password):
166
+ return f"{username}:{password}" if username in users_db and users_db.get(username) == password else "[Auth API] Failed: Invalid credentials."
167
+
168
+ def _get_user_from_token(auth_token):
169
+ if not auth_token or ':' not in auth_token: return None
170
+ try:
171
+ username, password = auth_token.split(':', 1)
172
+ return username if username in users_db and users_db.get(username) == password else None
173
+ except (ValueError, TypeError): return None
174
+
175
+ def api_create_post(auth_token, content):
176
+ global posts_df, post_counter
177
+ username = _get_user_from_token(auth_token)
178
+ if not username: return "[Post API] Failed: Invalid auth token."
179
+ if not content or not content.strip(): return "[Post API] Failed: Post content cannot be empty."
180
+ with db_lock:
181
+ post_counter += 1
182
+ new_post = pd.DataFrame([{"post_id": post_counter, "username": username, "content": content, "timestamp": datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")}])
183
+ posts_df = pd.concat([posts_df, new_post], ignore_index=True)
184
+ handle_persistence_after_change()
185
+ return f"[Post API] Success: Post created with ID {post_counter}."
186
+
187
+ def api_create_comment(auth_token, post_id, content):
188
+ global comments_df, comment_counter
189
+ username = _get_user_from_token(auth_token)
190
+ if not username: return "[Comment API] Failed: Invalid auth token."
191
+ if not content or not content.strip(): return "[Comment API] Failed: Comment content cannot be empty."
192
+ with db_lock:
193
+ try: target_post_id = int(post_id)
194
+ except (ValueError, TypeError): return f"[Comment API] Failed: Post ID must be a number."
195
+ if target_post_id not in posts_df['post_id'].values: return f"[Comment API] Failed: Post with ID {post_id} not found."
196
+ comment_counter += 1
197
+ new_comment = pd.DataFrame([{"comment_id": comment_counter, "post_id": target_post_id, "username": username, "content": content, "timestamp": datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")}])
198
+ comments_df = pd.concat([comments_df, new_comment], ignore_index=True)
199
+ handle_persistence_after_change()
200
+ return f"[Comment API] Success: Comment created on post {post_id}."
201
+
202
+ def api_get_feed(search_query: str = None):
203
+ with db_lock:
204
+ current_posts, current_comments = posts_df.copy(), comments_df.copy()
205
+ if current_posts.empty: return pd.DataFrame(columns=["post_id", "username", "content", "timestamp", "comments"])
206
+ display_posts = current_posts[current_posts['content'].str.contains(search_query, case=False, na=False)] if search_query and not search_query.isspace() else current_posts
207
+ sorted_posts = display_posts.sort_values(by="timestamp", ascending=False)
208
+ feed_data = [{"post_id": post['post_id'], "username": post['username'], "content": post['content'], "timestamp": post['timestamp'], "comments": "\n".join([f" - @{c['username']}: {c['content']}" for _, c in current_comments[current_comments['post_id'] == post['post_id']].iterrows()])} for _, post in sorted_posts.iterrows()]
209
+ return pd.DataFrame(feed_data) if feed_data else pd.DataFrame(columns=["post_id", "username", "content", "timestamp", "comments"])
210
+
211
+ # --- UI Helper Functions ---
212
+ def ui_manual_post(username, password, content):
213
+ if not username or not password:
214
+ return "Username and password are required.", api_get_feed()
215
+ auth_token = api_login(username, password)
216
+ if "Failed" in auth_token:
217
+ return "Login failed. Check credentials.", api_get_feed()
218
+ result = api_create_post(auth_token, content)
219
+ return result, api_get_feed()
220
+
221
+ def ui_manual_comment(username, password, post_id, content):
222
+ if not username or not password:
223
+ return "Username and password are required.", api_get_feed()
224
+ auth_token = api_login(username, password)
225
+ if "Failed" in auth_token:
226
+ return "Login failed. Check credentials.", api_get_feed()
227
+ result = api_create_comment(auth_token, post_id, content)
228
+ return result, api_get_feed()
229
+
230
+ with gr.Blocks(theme=gr.themes.Soft(), title="Social App") as demo:
231
+ gr.Markdown("# Dummy Social Media Platform")
232
+ gr.Markdown(f"This app provides an API for iLearn agents to interact with. **Storage Backend: `{STORAGE_BACKEND_CONFIG}`**")
233
+
234
+ with gr.Tabs():
235
+ with gr.TabItem("Live Feed"):
236
+ feed_df_display = gr.DataFrame(label="Feed", headers=["post_id", "username", "content", "timestamp", "comments"], interactive=False, wrap=True)
237
+ refresh_btn = gr.Button("Refresh Feed")
238
+
239
+ with gr.TabItem("Manual Actions & Settings"):
240
+ manual_action_status = gr.Textbox(label="Action Status", interactive=False)
241
+ with gr.Row():
242
+ with gr.Group():
243
+ gr.Markdown("### Manually Create Post")
244
+ post_user = gr.Textbox(label="Username", value="admin")
245
+ post_pass = gr.Textbox(label="Password", type="password", value="password")
246
+ post_content = gr.Textbox(label="Post Content", lines=3, placeholder="What's on your mind?")
247
+ post_button = gr.Button("Submit Post", variant="primary")
248
+ with gr.Group():
249
+ gr.Markdown("### Manually Create Comment")
250
+ comment_user = gr.Textbox(label="Username", value="admin")
251
+ comment_pass = gr.Textbox(label="Password", type="password", value="password")
252
+ comment_post_id = gr.Number(label="Target Post ID", precision=0)
253
+ comment_content = gr.Textbox(label="Comment Content", lines=2, placeholder="Add a comment...")
254
+ comment_button = gr.Button("Submit Comment", variant="primary")
255
+ with gr.Group():
256
+ gr.Markdown("### Settings")
257
+ feed_refresh_interval_slider = gr.Slider(minimum=5, maximum=120, value=15, step=5, label="Feed Refresh Interval (seconds)")
258
+
259
+ with gr.TabItem("Admin", visible=(STORAGE_BACKEND_CONFIG == "HF_DATASET")):
260
+ gr.Markdown("### Hugging Face Dataset Control")
261
+ backup_btn = gr.Button("Force Backup to Hugging Face Hub", visible=not DEMO_MODE)
262
+ backup_status = gr.Textbox(label="Backup Status", interactive=False)
263
+
264
+ # Event Handlers
265
+ post_button.click(
266
+ fn=ui_manual_post,
267
+ inputs=[post_user, post_pass, post_content],
268
+ outputs=[manual_action_status, feed_df_display]
269
+ )
270
+ comment_button.click(
271
+ fn=ui_manual_comment,
272
+ inputs=[comment_user, comment_pass, comment_post_id, comment_content],
273
+ outputs=[manual_action_status, feed_df_display]
274
+ )
275
+
276
+ last_refresh = time.time()
277
+ def timed_feed_refresh(interval):
278
+ global last_refresh
279
+ if time.time() - last_refresh > interval:
280
+ last_refresh = time.time()
281
+ return api_get_feed()
282
+ return gr.update()
283
+
284
+ # A fast-ticking timer that calls our function. The function itself decides if it's time to refresh.
285
+ gr.Timer(1).tick(
286
+ fn=timed_feed_refresh,
287
+ inputs=[feed_refresh_interval_slider],
288
+ outputs=[feed_df_display]
289
+ )
290
+
291
+ refresh_btn.click(api_get_feed, None, feed_df_display)
292
+
293
+ def admin_backup_handler():
294
+ success, message = force_persist_data()
295
+ return message
296
+
297
+ if STORAGE_BACKEND_CONFIG == "HF_DATASET":
298
+ backup_btn.click(admin_backup_handler, None, backup_status)
299
+
300
+ demo.load(api_get_feed, None, feed_df_display)
301
+
302
+ # Hidden API interfaces for the agent
303
+ with gr.Column(visible=False if DEMO_MODE else True):
304
+ for name, func, inputs, outputs in [
305
+ ("register", api_register, ["text", gr.Textbox(type="password")], "text"),
306
+ ("login", api_login, ["text", gr.Textbox(type="password")], "text"),
307
+ ("create_post", api_create_post, ["text", "text"], "text"),
308
+ ("create_comment", api_create_comment, ["text", "number", "text"], "text"),
309
+ ("get_feed", api_get_feed, ["text"], "dataframe")
310
+ ]:
311
+ gr.Interface(func, inputs, outputs, api_name=name, allow_flagging="never")
312
+
313
+ if __name__ == "__main__":
314
+ print(f"Starting Social Media App server with {STORAGE_BACKEND_CONFIG} backend.")
315
+ if STORAGE_BACKEND_CONFIG == "HF_DATASET" and not HF_DATASETS_AVAILABLE:
316
+ print("\nWARNING: 'datasets' library not found. Please run `pip install datasets huggingface_hub` to use the HF_DATASET backend.\n")
317
+ app_port = int(os.getenv("GRADIO_PORT", 7860))
318
+ demo.queue().launch(server_name="0.0.0.0", server_port=app_port, share=False)