-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_post.py
157 lines (116 loc) · 4.16 KB
/
user_post.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python
#
# Brief description here
#
##############################################################################
# Authors:
# - Alessio Xompero, [email protected]
#
# Created Date: 2024/08/23
# Modified Date: 2023/08/26
#
# Copyright (c) 2024 AtomicDeFake
#
##############################################################################
import streamlit as st
def set_atomic_defake():
"""API fo Atomic DeFake"""
st.session_state.stage = "atomic_defake"
def preamble():
st.title("AtomicDeFake")
social_media_buttons()
def social_media_on_click(idx, SOCIAL_MEDIA, SOCIAL_MEDIA_CHARS):
""" """
social_media_item = {"name": "", "max_chars": 0}
social_media_item["name"] = SOCIAL_MEDIA[idx].lower()
social_media_item["max_chars"] = SOCIAL_MEDIA_CHARS[idx]
st.session_state.social_media = social_media_item
st.session_state.stage = "post"
def social_media_buttons():
""" """
st.text("Where do you want to post?")
st.divider()
# TODO - move to a file to read maybe? Make sure to read once before running the app
SOCIAL_MEDIA = ["Facebook", "Twitter", "Linkedin", "Other"]
SOCIAL_MEDIA_CHARS = [63206, 280, 280, 280]
n_buttons = len(SOCIAL_MEDIA)
assert n_buttons == 4
col1, col2, col3, col4 = st.columns([1] * n_buttons)
with col1:
idx = 0
if st.button(SOCIAL_MEDIA[idx]):
social_media_on_click(idx, SOCIAL_MEDIA, SOCIAL_MEDIA_CHARS)
with col2:
idx = 1
if st.button(SOCIAL_MEDIA[idx]):
social_media_on_click(idx, SOCIAL_MEDIA, SOCIAL_MEDIA_CHARS)
with col3:
idx = 2
if st.button(SOCIAL_MEDIA[idx]):
social_media_on_click(idx, SOCIAL_MEDIA, SOCIAL_MEDIA_CHARS)
with col4:
idx = 3
if st.button(SOCIAL_MEDIA[idx]):
social_media_on_click(idx, SOCIAL_MEDIA, SOCIAL_MEDIA_CHARS)
def post_message(message_form):
st.divider()
n_max_chars_social = st.session_state.social_media["max_chars"]
with st.form(key="post_form"):
st.text_area(
message_form,
value="",
height=None,
max_chars=n_max_chars_social,
key="post",
help=None,
on_change=None,
args=None,
label_visibility="visible",
)
submit_button = st.form_submit_button(
label="AtomicDeFake",
on_click=set_atomic_defake,
)
def run_atomic_defake():
""" """
user_post = st.session_state.post
st.divider()
st.text(
"Wait for your post to be certified before being posted to {:s}".format(
st.session_state.social_media["name"]
)
)
st.session_state.atomic_defake.verify_ai(user_post)
if st.session_state.atomic_defake.get_status() == "human_responses":
st.session_state.stage = "contributor"
st.session_state.post = user_post
st.switch_page("contributor.py")
def set_output_stage():
st.divider()
assigned_adf_label, user_response = st.session_state.atomic_defake.get_output()
if assigned_adf_label == 1:
st.text("Here is your certified output:\n")
st.write(user_response)
st.session_state.stage = None
st.session_state.atomic_defake.reset()
elif assigned_adf_label == 0:
st.text("Your post has not been verified because of the following reasons:\n")
st.write(user_response)
st.session_state.stage = "edit"
############################################################################################
# APP FLOW
preamble()
if st.session_state.stage == "post":
st.session_state.n_checkers_iter = st.session_state.n_checkers
post_message("Write your post here")
if st.session_state.stage == "atomic_defake":
run_atomic_defake()
if st.session_state.stage == "output":
set_output_stage()
if st.session_state.stage == "edit":
st.session_state.n_checkers_iter = st.session_state.n_checkers
st.session_state.atomic_defake.reset()
post_message("Edit your post here and submit again for verification.")
else:
st.session_state.stage = None
st.session_state.atomic_defake.reset()