andykr1k commited on
Commit
20b3c7f
·
1 Parent(s): 5ae989a

created feed recommender

Browse files
Files changed (4) hide show
  1. .gitignore +436 -0
  2. Dockerfile +5 -0
  3. app.py +256 -0
  4. requirements.txt +9 -0
.gitignore ADDED
@@ -0,0 +1,436 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### Python ###
2
+ # Byte-compiled / optimized / DLL files
3
+ __pycache__/
4
+ *.py[cod]
5
+ *$py.class
6
+
7
+ # C extensions
8
+ *.so
9
+
10
+ # Distribution / packaging
11
+ .Python
12
+ build/
13
+ develop-eggs/
14
+ dist/
15
+ downloads/
16
+ eggs/
17
+ .eggs/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # poetry
98
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102
+ #poetry.lock
103
+
104
+ # pdm
105
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106
+ #pdm.lock
107
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108
+ # in version control.
109
+ # https://pdm.fming.dev/#use-with-ide
110
+ .pdm.toml
111
+
112
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
113
+ __pypackages__/
114
+
115
+ # Celery stuff
116
+ celerybeat-schedule
117
+ celerybeat.pid
118
+
119
+ # SageMath parsed files
120
+ *.sage.py
121
+
122
+ # Environments
123
+ .env
124
+ .venv
125
+ env/
126
+ venv/
127
+ ENV/
128
+ env.bak/
129
+ venv.bak/
130
+
131
+ # Spyder project settings
132
+ .spyderproject
133
+ .spyproject
134
+
135
+ # Rope project settings
136
+ .ropeproject
137
+
138
+ # mkdocs documentation
139
+ /site
140
+
141
+ # mypy
142
+ .mypy_cache/
143
+ .dmypy.json
144
+ dmypy.json
145
+
146
+ # Pyre type checker
147
+ .pyre/
148
+
149
+ # pytype static type analyzer
150
+ .pytype/
151
+
152
+ # Cython debug symbols
153
+ cython_debug/
154
+
155
+ # PyCharm
156
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
157
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
159
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
160
+ #.idea/
161
+
162
+ ### Python Patch ###
163
+ # Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration
164
+ poetry.toml
165
+
166
+ # ruff
167
+ .ruff_cache/
168
+
169
+ # LSP config files
170
+ pyrightconfig.json
171
+
172
+ ### react ###
173
+ .DS_*
174
+ logs
175
+ **/*.backup.*
176
+ **/*.back.*
177
+
178
+ node_modules
179
+ bower_components
180
+
181
+ *.sublime*
182
+
183
+ psd
184
+ thumb
185
+ sketch
186
+
187
+ ### ReactNative ###
188
+ # React Native Stack Base
189
+
190
+ .expo
191
+ __generated__
192
+
193
+ ### ReactNative.Xcode Stack ###
194
+ ## User settings
195
+ xcuserdata/
196
+
197
+ ## Xcode 8 and earlier
198
+ *.xcscmblueprint
199
+ *.xccheckout
200
+
201
+ ### ReactNative.Gradle Stack ###
202
+ .gradle
203
+ **/build/
204
+ !src/**/build/
205
+
206
+ # Ignore Gradle GUI config
207
+ gradle-app.setting
208
+
209
+ # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
210
+ !gradle-wrapper.jar
211
+
212
+ # Avoid ignore Gradle wrappper properties
213
+ !gradle-wrapper.properties
214
+
215
+ # Cache of project
216
+ .gradletasknamecache
217
+
218
+ # Eclipse Gradle plugin generated files
219
+ # Eclipse Core
220
+ .project
221
+ # JDT-specific (Eclipse Java Development Tools)
222
+ .classpath
223
+
224
+ ### ReactNative.macOS Stack ###
225
+ # General
226
+ .DS_Store
227
+ .AppleDouble
228
+ .LSOverride
229
+
230
+ # Icon must end with two \r
231
+ Icon
232
+
233
+
234
+ # Thumbnails
235
+ ._*
236
+
237
+ # Files that might appear in the root of a volume
238
+ .DocumentRevisions-V100
239
+ .fseventsd
240
+ .Spotlight-V100
241
+ .TemporaryItems
242
+ .Trashes
243
+ .VolumeIcon.icns
244
+ .com.apple.timemachine.donotpresent
245
+
246
+ # Directories potentially created on remote AFP share
247
+ .AppleDB
248
+ .AppleDesktop
249
+ Network Trash Folder
250
+ Temporary Items
251
+ .apdisk
252
+
253
+ ### ReactNative.Linux Stack ###
254
+ *~
255
+
256
+ # temporary files which can be created if a process still has a handle open of a deleted file
257
+ .fuse_hidden*
258
+
259
+ # KDE directory preferences
260
+ .directory
261
+
262
+ # Linux trash folder which might appear on any partition or disk
263
+ .Trash-*
264
+
265
+ # .nfs files are created when an open file is removed but is still being accessed
266
+ .nfs*
267
+
268
+ ### ReactNative.Node Stack ###
269
+ # Logs
270
+ npm-debug.log*
271
+ yarn-debug.log*
272
+ yarn-error.log*
273
+ lerna-debug.log*
274
+ .pnpm-debug.log*
275
+
276
+ # Diagnostic reports (https://nodejs.org/api/report.html)
277
+ report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
278
+
279
+ # Runtime data
280
+ pids
281
+ *.pid
282
+ *.seed
283
+ *.pid.lock
284
+
285
+ # Directory for instrumented libs generated by jscoverage/JSCover
286
+ lib-cov
287
+
288
+ # Coverage directory used by tools like istanbul
289
+ coverage
290
+ *.lcov
291
+
292
+ # nyc test coverage
293
+ .nyc_output
294
+
295
+ # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
296
+ .grunt
297
+
298
+ # Bower dependency directory (https://bower.io/)
299
+
300
+ # node-waf configuration
301
+ .lock-wscript
302
+
303
+ # Compiled binary addons (https://nodejs.org/api/addons.html)
304
+ build/Release
305
+
306
+ # Dependency directories
307
+ node_modules/
308
+ jspm_packages/
309
+
310
+ # Snowpack dependency directory (https://snowpack.dev/)
311
+ web_modules/
312
+
313
+ # TypeScript cache
314
+ *.tsbuildinfo
315
+
316
+ # Optional npm cache directory
317
+ .npm
318
+
319
+ # Optional eslint cache
320
+ .eslintcache
321
+
322
+ # Optional stylelint cache
323
+ .stylelintcache
324
+
325
+ # Microbundle cache
326
+ .rpt2_cache/
327
+ .rts2_cache_cjs/
328
+ .rts2_cache_es/
329
+ .rts2_cache_umd/
330
+
331
+ # Optional REPL history
332
+ .node_repl_history
333
+
334
+ # Output of 'npm pack'
335
+ *.tgz
336
+
337
+ # Yarn Integrity file
338
+ .yarn-integrity
339
+
340
+ # dotenv environment variable files
341
+ .env.development.local
342
+ .env.test.local
343
+ .env.production.local
344
+ .env.local
345
+
346
+ # parcel-bundler cache (https://parceljs.org/)
347
+ .parcel-cache
348
+
349
+ # Next.js build output
350
+ .next
351
+ out
352
+
353
+ # Nuxt.js build / generate output
354
+ .nuxt
355
+ dist
356
+
357
+ # Gatsby files
358
+ .cache/
359
+ # Comment in the public line in if your project uses Gatsby and not Next.js
360
+ # https://nextjs.org/blog/next-9-1#public-directory-support
361
+ # public
362
+
363
+ # vuepress build output
364
+ .vuepress/dist
365
+
366
+ # vuepress v2.x temp and cache directory
367
+ .temp
368
+
369
+ # Docusaurus cache and generated files
370
+ .docusaurus
371
+
372
+ # Serverless directories
373
+ .serverless/
374
+
375
+ # FuseBox cache
376
+ .fusebox/
377
+
378
+ # DynamoDB Local files
379
+ .dynamodb/
380
+
381
+ # TernJS port file
382
+ .tern-port
383
+
384
+ # Stores VSCode versions used for testing VSCode extensions
385
+ .vscode-test
386
+
387
+ # yarn v2
388
+ .yarn/cache
389
+ .yarn/unplugged
390
+ .yarn/build-state.yml
391
+ .yarn/install-state.gz
392
+ .pnp.*
393
+
394
+ ### ReactNative.Buck Stack ###
395
+ buck-out/
396
+ .buckconfig.local
397
+ .buckd/
398
+ .buckversion
399
+ .fakebuckversion
400
+
401
+ ### ReactNative.Android Stack ###
402
+ # Gradle files
403
+ .gradle/
404
+
405
+ # Local configuration file (sdk path, etc)
406
+ local.properties
407
+
408
+ # Log/OS Files
409
+
410
+ # Android Studio generated files and folders
411
+ captures/
412
+ .externalNativeBuild/
413
+ .cxx/
414
+ *.apk
415
+ output.json
416
+
417
+ # IntelliJ
418
+ *.iml
419
+ .idea/
420
+ misc.xml
421
+ deploymentTargetDropDown.xml
422
+ render.experimental.xml
423
+
424
+ # Keystore files
425
+ *.jks
426
+ *.keystore
427
+
428
+ # Google Services (e.g. APIs or Firebase)
429
+ google-services.json
430
+
431
+ # Android Profiling
432
+ *.hprof
433
+
434
+ # Personal
435
+ data
436
+ package-lock.json
Dockerfile ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ FROM python:3.10.9
2
+ COPY . .
3
+ WORKDIR /
4
+ RUN pip install --no-cache-dir --upgrade -r /requirements.txt
5
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,256 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import random
3
+ import numpy as np
4
+ import pandas as pd
5
+ import networkx as nx
6
+ import torch
7
+ import torch.nn as nn
8
+ import torch.nn.functional as F
9
+ import torch.optim as optim
10
+ from torch_geometric.utils import from_networkx
11
+ from torch_geometric.nn import SAGEConv
12
+ from supabase import create_client
13
+ from fastapi import FastAPI, HTTPException, Query
14
+ from fastapi.middleware.cors import CORSMiddleware
15
+ from dotenv import load_dotenv
16
+
17
+ load_dotenv()
18
+
19
+ app = FastAPI()
20
+
21
+ # Enable CORS for all origins (adjust as needed)
22
+ app.add_middleware(
23
+ CORSMiddleware,
24
+ allow_origins=["*"],
25
+ allow_credentials=True,
26
+ allow_methods=["*"],
27
+ allow_headers=["*"],
28
+ )
29
+
30
+ SEED = 42
31
+ random.seed(SEED)
32
+ np.random.seed(SEED)
33
+ torch.manual_seed(SEED)
34
+ if torch.cuda.is_available():
35
+ torch.cuda.manual_seed_all(SEED)
36
+
37
+ # Global variables for our GNN-based post recommender
38
+ global G, features, user_nodes, post_nodes, node2idx, pyg_data, trained_model
39
+ G = None # Bipartite graph (users and posts)
40
+ features = None # Node features (we use identity)
41
+ user_nodes = None # Sorted list of user node IDs
42
+ post_nodes = None # Sorted list of post node IDs
43
+ node2idx = None # Mapping from node ID to index (for features)
44
+ pyg_data = None # PyTorch Geometric data object
45
+ trained_model = None # Trained GNN model
46
+
47
+ SUPABASE_ID = os.getenv('supabaseID')
48
+ SUPABASE_URL = os.getenv('supabaseUrl')
49
+ SUPABASE_KEY = os.getenv('supabaseAnonKey')
50
+
51
+ def get_supabase_client():
52
+ return create_client(SUPABASE_URL, SUPABASE_KEY)
53
+
54
+ def load_and_preprocess_data_for_posts():
55
+ """
56
+ Build a bipartite directed graph from Supabase data:
57
+ - Users: derived from profiles (via posts and likes)
58
+ - Posts: from the posts table.
59
+ Edges:
60
+ - From user to post if the user created the post.
61
+ - From user to post if the user liked the post.
62
+ """
63
+ supabase = get_supabase_client()
64
+
65
+ # Load profiles (users)
66
+ profiles_response = supabase.table('profiles').select('id, username').execute()
67
+ df_profiles = pd.DataFrame(profiles_response.data)
68
+ # Create mapping from user id to username
69
+ uuid_to_username = dict(zip(df_profiles['id'], df_profiles['username']))
70
+
71
+ # Load posts (each with an author)
72
+ posts_response = supabase.table('posts').select('id, author').execute()
73
+ df_posts = pd.DataFrame(posts_response.data)
74
+ # Map post authors to usernames
75
+ df_posts['username'] = df_posts['author'].map(uuid_to_username)
76
+
77
+ # Load likes: records of (user_id, post_id)
78
+ likes_response = supabase.table('likes').select('user_id, post_id').execute()
79
+ df_likes = pd.DataFrame(likes_response.data)
80
+ df_likes['username'] = df_likes['user_id'].map(uuid_to_username)
81
+
82
+ # Build bipartite graph (directed: from user to post)
83
+ bipartite = nx.DiGraph()
84
+
85
+ # Determine set of users (only those who appear in posts or likes)
86
+ user_set = set(df_posts['username'].dropna().tolist()) | set(df_likes['username'].dropna().tolist())
87
+ # Determine set of posts (by id)
88
+ post_set = set(df_posts['id'].tolist())
89
+
90
+ # Add user nodes with attribute type 'user'
91
+ for user in user_set:
92
+ if user: # ensure non-empty
93
+ bipartite.add_node(user, type='user')
94
+ # Add post nodes with attribute type 'post'
95
+ for post in post_set:
96
+ bipartite.add_node(post, type='post')
97
+
98
+ # Add edges from post creation: user -> post
99
+ for _, row in df_posts.iterrows():
100
+ user = row['username']
101
+ post = row['id']
102
+ if user and post:
103
+ bipartite.add_edge(user, post)
104
+
105
+ # Add edges from likes: user -> post
106
+ for _, row in df_likes.iterrows():
107
+ user = row['username']
108
+ post = row['post_id']
109
+ if user and post:
110
+ bipartite.add_edge(user, post)
111
+
112
+ return bipartite
113
+
114
+ # GNN Model using GraphSAGE
115
+ class GraphRecommender(nn.Module):
116
+ def __init__(self, input_dim, hidden_dim=128, output_dim=64):
117
+ super().__init__()
118
+ self.conv1 = SAGEConv(input_dim, hidden_dim)
119
+ self.conv2 = SAGEConv(hidden_dim, output_dim)
120
+ self.dropout = nn.Dropout(0.3)
121
+
122
+ def forward(self, x, edge_index):
123
+ x = F.relu(self.conv1(x, edge_index))
124
+ x = self.dropout(x)
125
+ x = self.conv2(x, edge_index)
126
+ return x
127
+
128
+ def prepare_training_data(G, node2idx, user_nodes, post_nodes):
129
+ """
130
+ Create positive edges for training.
131
+ Only consider edges from a user node to a post node.
132
+ """
133
+ pos_edges = []
134
+ for u, v in G.edges():
135
+ # Only include if u is a user and v is a post
136
+ if G.nodes[u].get('type') == 'user' and G.nodes[v].get('type') == 'post':
137
+ pos_edges.append((node2idx[u], node2idx[v]))
138
+ pos_edge_index = torch.tensor(pos_edges).T # shape: [2, num_pos_edges]
139
+
140
+ # For negative sampling, form all possible user->post pairs and subtract positive edges.
141
+ all_possible = [(node2idx[u], node2idx[p]) for u in user_nodes for p in post_nodes]
142
+ pos_set = set(pos_edges)
143
+ neg_candidates = [pair for pair in all_possible if pair not in pos_set]
144
+ # Sample as many negatives as positives (if available)
145
+ neg_sample_size = min(len(pos_edges), len(neg_candidates))
146
+ neg_edges = random.sample(neg_candidates, neg_sample_size)
147
+ neg_edge_index = torch.tensor(neg_edges).T
148
+ return pos_edge_index, neg_edge_index
149
+
150
+ def train_model(model, data, pos_edges, neg_edges, epochs=200):
151
+ optimizer = optim.Adam(model.parameters(), lr=0.005, weight_decay=1e-4)
152
+ best_loss = float('inf')
153
+ patience_counter = 0
154
+
155
+ for epoch in range(epochs):
156
+ model.train()
157
+ optimizer.zero_grad()
158
+
159
+ embeddings = model(data.x, data.edge_index)
160
+
161
+ # Compute scores for positive and negative edges via dot product
162
+ pos_scores = (embeddings[pos_edges[0]] * embeddings[pos_edges[1]]).sum(1)
163
+ neg_scores = (embeddings[neg_edges[0]] * embeddings[neg_edges[1]]).sum(1)
164
+
165
+ pos_loss = F.binary_cross_entropy_with_logits(pos_scores, torch.ones_like(pos_scores))
166
+ neg_loss = F.binary_cross_entropy_with_logits(neg_scores, torch.zeros_like(neg_scores))
167
+ reg_loss = torch.norm(embeddings, p=2)
168
+
169
+ total_loss = pos_loss + neg_loss + 0.001 * reg_loss
170
+
171
+ total_loss.backward()
172
+ optimizer.step()
173
+
174
+ if total_loss < best_loss:
175
+ best_loss = total_loss
176
+ patience_counter = 0
177
+ else:
178
+ patience_counter += 1
179
+ if patience_counter >= 20:
180
+ break
181
+
182
+ return model
183
+
184
+ def rebuild_model():
185
+ """
186
+ Loads the bipartite user-post graph, computes node features,
187
+ prepares training data, trains the GNN model, and updates globals.
188
+ """
189
+ global G, features, user_nodes, post_nodes, node2idx, pyg_data, trained_model
190
+ G = load_and_preprocess_data_for_posts()
191
+
192
+ # Get sorted lists of user and post nodes
193
+ user_nodes = sorted([n for n, attr in G.nodes(data=True) if attr.get('type') == 'user'])
194
+ post_nodes = sorted([n for n, attr in G.nodes(data=True) if attr.get('type') == 'post'])
195
+ all_nodes = sorted(G.nodes())
196
+ node2idx = {node: i for i, node in enumerate(all_nodes)}
197
+
198
+ # Use identity features (one-hot) for all nodes
199
+ features = torch.eye(len(all_nodes))
200
+ pyg_data = from_networkx(G)
201
+ pyg_data.x = features
202
+
203
+ pos_edge_index, neg_edge_index = prepare_training_data(G, node2idx, user_nodes, post_nodes)
204
+
205
+ input_dim = features.shape[1]
206
+ model = GraphRecommender(input_dim=input_dim, hidden_dim=128, output_dim=64)
207
+ trained_model = train_model(model, pyg_data, pos_edge_index, neg_edge_index)
208
+
209
+ def get_recommendations(username, model, data, G, user_nodes, post_nodes, node2idx, top_k=10):
210
+ """
211
+ For a given username, compute the user's embedding and rank candidate posts (that the user hasn't interacted with).
212
+ """
213
+ if username not in user_nodes:
214
+ return []
215
+ user_idx = node2idx[username]
216
+
217
+ # Find posts the user already interacted with (edges from username)
218
+ user_interacted = set()
219
+ for _, v in G.out_edges(username):
220
+ if G.nodes[v].get('type') == 'post':
221
+ user_interacted.add(v)
222
+
223
+ with torch.no_grad():
224
+ embeddings = model(data.x, data.edge_index)
225
+ user_embed = embeddings[user_idx]
226
+
227
+ candidate_scores = []
228
+ for post in post_nodes:
229
+ if post in user_interacted:
230
+ continue
231
+ post_idx = node2idx[post]
232
+ score = torch.dot(user_embed, embeddings[post_idx]).item()
233
+ candidate_scores.append((post, score))
234
+ candidate_scores = sorted(candidate_scores, key=lambda x: x[1], reverse=True)
235
+ top_posts = [post for post, score in candidate_scores[:top_k]]
236
+ return top_posts
237
+
238
+ # Endpoints
239
+ @app.post("/rebuild")
240
+ async def rebuild_handler():
241
+ rebuild_model()
242
+ return {"status": "success", "message": "Model and data rebuilt successfully"}
243
+
244
+ @app.get("/recommend/feed")
245
+ async def get_recommendations_handler(username: str = Query(...)):
246
+ if trained_model is None:
247
+ raise HTTPException(status_code=500, detail="Model not initialized, please rebuild first.")
248
+ recs = get_recommendations(username, trained_model, pyg_data, G, user_nodes, post_nodes, node2idx)
249
+ return {"status": "success", "recommendations": recs}
250
+
251
+ @app.get("/")
252
+ async def health_check():
253
+ return {"status": "success", "message": "Recommendation service operational"}
254
+
255
+ # Optionally, rebuild the model on startup
256
+ rebuild_model()
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ numpy
2
+ pandas
3
+ torch
4
+ networkx[default]
5
+ torch_geometric
6
+ supabase
7
+ fastapi
8
+ python-dotenv
9
+ uvicorn