Skip to content

Commit b9cd271

Browse files
Merge pull request #3 from YSocialTwin/multimodal
Pages, Multimodal posts and Dynamic Interests
2 parents 0516f05 + b42be38 commit b9cd271

File tree

10 files changed

+241
-53
lines changed

10 files changed

+241
-53
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ This repository contains the code for the **server-side** of the application.
66

77
The client-side code can be found [here](https://github.com/YSocialTwin/YClient)
88

9+
For more information, please refer to the official [documentation](https://ysocialtwin.github.io/)
10+
911
#### Features
1012

1113
- Realistic agent simulations using LLMs

config_files/exp_config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
"port": 5010,
55
"debug": "True",
66
"reset_db": "True",
7-
"modules": ["news", "voting"]
7+
"modules": ["news", "voting", "image"]
88
}

data_schema/database_clean_server.db

0 Bytes
Binary file not shown.

requirements_server.txt

14 Bytes
Binary file not shown.

y_server/modals.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class User_mgmt(UserMixin, db.Model):
2525
nationality = db.Column(db.String(15), default=None)
2626
round_actions = db.Column(db.Integer, default=3)
2727
toxicity = db.Column(db.String(10), default="no")
28+
is_page = db.Column(db.Integer, default=0)
29+
left_on = db.Column(db.Integer, default=None)
2830

2931
posts = db.relationship("Post", backref="author", lazy=True)
3032
liked = db.relationship("Reactions", backref="liked_by", lazy=True)
@@ -39,6 +41,7 @@ class Post(db.Model):
3941
comment_to = db.Column(db.Integer, default=-1)
4042
thread_id = db.Column(db.Integer)
4143
news_id = db.Column(db.String(50), db.ForeignKey("articles.id"), default=None)
44+
image_id = db.Column(db.Integer(), db.ForeignKey("images.id"), default=None)
4245
shared_from = db.Column(db.Integer, default=-1)
4346

4447

@@ -146,3 +149,16 @@ class Post_topics(db.Model):
146149
id = db.Column(db.Integer, primary_key=True)
147150
post_id = db.Column(db.Integer, db.ForeignKey("post.id"), nullable=False)
148151
topic_id = db.Column(db.Integer, db.ForeignKey("interests.iid"), nullable=False)
152+
153+
154+
class Images(db.Model):
155+
id = db.Column(db.Integer, primary_key=True)
156+
url = db.Column(db.String(200), nullable=True)
157+
description = db.Column(db.String(400), nullable=True)
158+
article_id = db.Column(db.Integer, db.ForeignKey("articles.id"), nullable=True)
159+
160+
161+
class Article_topics(db.Model):
162+
id = db.Column(db.Integer, primary_key=True)
163+
article_id = db.Column(db.Integer, db.ForeignKey("articles.id"), nullable=False)
164+
topic_id = db.Column(db.Integer, db.ForeignKey("interests.iid"), nullable=False)

y_server/routes/content_management.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
Emotions,
1717
Post_emotions,
1818
Post_topics,
19-
Interests,
2019
)
2120

2221

@@ -33,9 +32,11 @@ def read():
3332
vround = int(data["visibility_rounds"])
3433
try:
3534
uid = int(data["uid"])
36-
fratio = float(data["followers_ratio"])
3735
except:
3836
uid = None
37+
try:
38+
fratio = float(data["followers_ratio"])
39+
except:
3940
fratio = 1
4041
articles = False
4142
if "article" in data:
@@ -50,14 +51,14 @@ def read():
5051
if articles:
5152
posts = [
5253
db.session.query(Post)
53-
.filter(Post.round >= visibility, Post.news_id != -1)
54+
.filter(Post.round >= visibility, Post.news_id != -1, Post.user_id != uid)
5455
.order_by(desc(Post.id))
5556
.limit(10)
5657
]
5758
else:
5859
posts = [
5960
db.session.query(Post)
60-
.filter(Post.round >= visibility)
61+
.filter(Post.round >= visibility, Post.user_id != uid)
6162
.order_by(desc(Post.id))
6263
.limit(10)
6364
]
@@ -107,6 +108,7 @@ def read():
107108
Post.query.filter(
108109
Post.round >= visibility,
109110
Post.news_id != -1,
111+
Post.user_id != uid,
110112
Post.user_id.in_(follower_ids),
111113
)
112114
.order_by(desc(Post.id))
@@ -180,33 +182,33 @@ def read():
180182
if additional_posts_limit != 0:
181183
if articles:
182184
additional_posts = (
183-
Post.query.filter(Post.round >= visibility, Post.news_id != -1)
185+
Post.query.filter(Post.round >= visibility, Post.news_id != -1, Post.user_id != uid)
184186
.order_by(desc(Post.id))
185187
.limit(additional_posts_limit)
186188
)
187189
else:
188190
additional_posts = (
189-
Post.query.filter(Post.round >= visibility)
191+
Post.query.filter(Post.round >= visibility, Post.user_id != uid)
190192
.order_by(desc(Post.id))
191193
.limit(additional_posts_limit)
192194
)
193195

194-
posts = [posts, additional_posts]
196+
posts = [posts, additional_posts]
195197

196198
else:
197199
# get posts in random order
198200
if articles:
199201
posts = [
200202
(
201-
Post.query.filter(Post.round >= visibility, Post.news_id != -1)
203+
Post.query.filter(Post.round >= visibility, Post.news_id != -1, Post.user_id != uid)
202204
.order_by(func.random())
203205
.limit(limit)
204206
)
205207
]
206208
else:
207209
posts = [
208210
(
209-
Post.query.filter(Post.round >= visibility)
211+
Post.query.filter(Post.round >= visibility, Post.user_id != uid)
210212
.order_by(func.random())
211213
.limit(limit)
212214
)

y_server/routes/experiment_management.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
Interests,
1717
Post_topics,
1818
User_interest,
19+
Images,
20+
Article_topics,
1921
)
2022

2123

@@ -43,5 +45,7 @@ def reset_experiment():
4345
db.session.query(User_interest).delete()
4446
db.session.query(Voting).delete()
4547
db.session.query(Post_topics).delete()
48+
db.session.query(Images).delete()
49+
db.session.query(Article_topics).delete()
4650
db.session.commit()
4751
return {"status": 200}

y_server/routes/image_management.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import json
2+
from flask import request
3+
from y_server import app, db
4+
from y_server.modals import (
5+
Images,
6+
Post,
7+
Emotions,
8+
Post_emotions,
9+
Hashtags,
10+
Post_hashtags,
11+
)
12+
13+
14+
@app.route("/comment_image", methods=["POST"])
15+
def post_image():
16+
"""
17+
Comment on an image.
18+
19+
:return:
20+
"""
21+
data = json.loads(request.get_data())
22+
account_id = int(data["user_id"])
23+
text = data["text"].strip('"')
24+
emotions = data["emotions"]
25+
hashtags = data["hashtags"]
26+
tid = int(data["tid"])
27+
image_url = data["image_url"]
28+
image_description = data["image_description"]
29+
article_id = int(data["article_id"])
30+
31+
# check if image exists
32+
image = Images.query.filter_by(url=image_url).first()
33+
if image is None:
34+
image = Images(
35+
url=image_url,
36+
description=image_description,
37+
article_id=article_id,
38+
)
39+
db.session.add(image)
40+
db.session.commit()
41+
42+
# get image id
43+
image_id = Images.query.filter_by(url=image_url).first().id
44+
45+
post = Post(
46+
tweet=text,
47+
round=tid,
48+
user_id=account_id,
49+
image_id=image_id,
50+
comment_to=-1,
51+
)
52+
53+
db.session.add(post)
54+
db.session.commit()
55+
56+
post.thread_id = post.id
57+
db.session.commit()
58+
59+
for emotion in emotions:
60+
if len(emotion) < 1:
61+
continue
62+
63+
em = Emotions.query.filter_by(emotion=emotion).first()
64+
if em is not None:
65+
post_emotion = Post_emotions(post_id=post.id, emotion_id=em.id)
66+
db.session.add(post_emotion)
67+
db.session.commit()
68+
69+
for tag in hashtags:
70+
if len(tag) < 1:
71+
continue
72+
73+
ht = Hashtags.query.filter_by(hashtag=tag).first()
74+
if ht is None:
75+
ht = Hashtags(hashtag=tag)
76+
db.session.add(ht)
77+
db.session.commit()
78+
ht = Hashtags.query.filter_by(hashtag=tag).first()
79+
80+
post_tag = Post_hashtags(post_id=post.id, hashtag_id=ht.id)
81+
db.session.add(post_tag)
82+
db.session.commit()
83+
84+
return json.dumps({"status": 200})

0 commit comments

Comments
 (0)