ff98 commited on
Commit
041cde8
1 Parent(s): f1623ef

UI updated

Browse files
Files changed (4) hide show
  1. app.py +109 -60
  2. styles/detectiveMag.svg +2 -2
  3. styles/robot.png +0 -0
  4. styles/style.css +62 -62
app.py CHANGED
@@ -1,4 +1,6 @@
1
  # imports
 
 
2
  import streamlit as st
3
  import tensorflow as tf
4
  from tensorflow.keras.models import Sequential
@@ -24,6 +26,7 @@ st.markdown(
24
  unsafe_allow_html=True
25
  )
26
 
 
27
  @st.cache_resource
28
  def load_models():
29
  # Load all models at once
@@ -36,7 +39,6 @@ def load_models():
36
  eff_net_model, eff_net_art_model, cnn_model = load_models()
37
 
38
  # CNN model
39
-
40
  def run_cnn(img_arr):
41
  my_model = Sequential()
42
  my_model.add(Conv2D(
@@ -70,6 +72,7 @@ def run_cnn(img_arr):
70
  prediction = my_model.predict(img_arr)
71
  return prediction
72
 
 
73
  def run_effNet(img_arr):
74
  try:
75
  resolver = tf.distribute.cluster_resolver.TPUClusterResolver()
@@ -82,6 +85,7 @@ def run_effNet(img_arr):
82
  prediction = eff_net_model.predict(img_arr)
83
  return prediction
84
 
 
85
  def run_effNet_Art(img_arr):
86
  try:
87
  resolver = tf.distribute.cluster_resolver.TPUClusterResolver()
@@ -94,13 +98,14 @@ def run_effNet_Art(img_arr):
94
  prediction = eff_net_art_model.predict(img_arr)
95
  return prediction
96
 
 
97
  def pre_process_img_effNet(image):
98
  img = load_img(image, target_size=(300, 300)) # Resize image to model input size
99
  img_arr = img_to_array(img) # Convert to array
100
  img_arr = np.expand_dims(img_arr, axis=0) # Add batch dimension
101
  result = run_effNet(img_arr)
102
  return result
103
-
104
  def pre_process_img_effNetArt(image):
105
  img = load_img(image, target_size=(224, 224)) # Resize image to model input size
106
  img_arr = img_to_array(img) # Convert to array
@@ -116,66 +121,110 @@ def pre_process_img(image):
116
  img_arr = img_arr.reshape((1, 256, 256, 3)) # Add batch dimension
117
  result = run_cnn(img_arr)
118
  return result
119
- # title
120
- st.markdown(
121
- """<p class = "title"> AI vs REAL Image Detection </p>""",
122
- unsafe_allow_html= True
123
- )
124
-
125
- # upload image
126
- st.markdown(
127
- """<p class = "upload_line"> Please upload the image </p>""",
128
- unsafe_allow_html= True
129
- )
130
-
131
- # introduce states
132
- if "prev_image" not in st.session_state:
133
- st.session_state.prev_image = None
134
- if "reset_model" not in st.session_state:
135
- st.session_state.reset_model = False
136
- if "model_key" not in st.session_state:
137
- st.session_state.model_key = "default_model_key"
138
-
139
- user_image = st.file_uploader("png, jpg, or jpeg image", ['png', 'jpg', 'jpeg'], label_visibility='hidden')
140
- if user_image != st.session_state.prev_image:
141
- if st.session_state.prev_image is not None:
142
- st.session_state.model_key = "reset_model_key" if st.session_state.model_key == "default_model_key" else "default_model_key"
143
- st.session_state.reset_model = True
144
- st.session_state.prev_image = user_image # set prev image to current image
145
-
146
- model_name = st.selectbox(
147
- 'Choose a model',
148
- ['CNN', 'Efficientnet', 'Efficientnet Art'],
149
- index=None,
150
- placeholder='choose an option',
151
- key=st.session_state.model_key
152
- )
153
- result_placeholder = st.empty()
154
-
155
- # design animation elements
156
- with open("styles/detectiveMag.svg", "r") as file:
157
- svg_content_detective_Mag = file.read()
158
 
159
- # First magnifying glass starts at bottom-right
160
- st.markdown(
161
- f"<div class='detectiveMag1' style='bottom: 0%; right: 0%;'>{svg_content_detective_Mag}</div>",
162
- unsafe_allow_html=True
163
- )
164
-
165
- # Second magnifying glass starts slightly higher up the diagonal
166
- st.markdown(
167
- f"<div class='detectiveMag2' style='bottom: 10%; right: 10%;'>{svg_content_detective_Mag}</div>",
168
- unsafe_allow_html=True
169
- )
170
 
171
- # Third magnifying glass starts further up the diagonal
172
- st.markdown(
173
- f"<div class='detectiveMag3' style='bottom: 20%; right: 20%;'>{svg_content_detective_Mag}</div>",
174
- unsafe_allow_html=True
175
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176
 
177
  if user_image is not None and model_name is not None:
178
  predictions = []
 
179
  if model_name == 'CNN':
180
  print('CNN is running')
181
  predictions = pre_process_img(user_image)
@@ -187,14 +236,14 @@ if user_image is not None and model_name is not None:
187
  predictions = pre_process_img_effNetArt(user_image)
188
 
189
  if predictions[0] < 0.5:
190
- result_word = "FAKE"
191
  else:
192
  result_word = "REAL"
193
 
 
194
  if user_image is not None:
195
  if len(predictions) > 0:
196
- result_placeholder.markdown(f"<div class='result'> <span class = 'prediction'>Prediction: {predictions[0][0]}</span> <br> It is a <span class = resultword> {result_word} </span> image. </div>", unsafe_allow_html=True)
197
-
198
 
199
  print(model_name)
200
  print(predictions[0])
 
1
  # imports
2
+ import base64
3
+ import os
4
  import streamlit as st
5
  import tensorflow as tf
6
  from tensorflow.keras.models import Sequential
 
26
  unsafe_allow_html=True
27
  )
28
 
29
+ # load model weights
30
  @st.cache_resource
31
  def load_models():
32
  # Load all models at once
 
39
  eff_net_model, eff_net_art_model, cnn_model = load_models()
40
 
41
  # CNN model
 
42
  def run_cnn(img_arr):
43
  my_model = Sequential()
44
  my_model.add(Conv2D(
 
72
  prediction = my_model.predict(img_arr)
73
  return prediction
74
 
75
+ # efficientnet model
76
  def run_effNet(img_arr):
77
  try:
78
  resolver = tf.distribute.cluster_resolver.TPUClusterResolver()
 
85
  prediction = eff_net_model.predict(img_arr)
86
  return prediction
87
 
88
+ # efficientnet art model
89
  def run_effNet_Art(img_arr):
90
  try:
91
  resolver = tf.distribute.cluster_resolver.TPUClusterResolver()
 
98
  prediction = eff_net_art_model.predict(img_arr)
99
  return prediction
100
 
101
+ # preprocess images for efficient net
102
  def pre_process_img_effNet(image):
103
  img = load_img(image, target_size=(300, 300)) # Resize image to model input size
104
  img_arr = img_to_array(img) # Convert to array
105
  img_arr = np.expand_dims(img_arr, axis=0) # Add batch dimension
106
  result = run_effNet(img_arr)
107
  return result
108
+ # preprocess images for efficient net art
109
  def pre_process_img_effNetArt(image):
110
  img = load_img(image, target_size=(224, 224)) # Resize image to model input size
111
  img_arr = img_to_array(img) # Convert to array
 
121
  img_arr = img_arr.reshape((1, 256, 256, 3)) # Add batch dimension
122
  result = run_cnn(img_arr)
123
  return result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
 
 
 
 
 
 
 
 
 
 
 
 
125
 
126
+ #UI
127
+
128
+ #title
129
+ col1, col2, col3,col4, col5 = st.columns([4,1,3,3,1], gap="small")
130
+
131
+ # In the first column, display the image
132
+ with col1:
133
+ st.write('')
134
+ with col2:
135
+ st.image("styles/robot.png")
136
+
137
+ # In the second column, display the text
138
+ with col3:
139
+ st.markdown(
140
+ """
141
+ <p class="title"> AI vs REAL Image Detection </p>
142
+ """,
143
+ unsafe_allow_html=True
144
+ )
145
+ with col4:
146
+ st.write('')
147
+ with col5:
148
+ st.write('')
149
+
150
+ # division between photo and other widget component
151
+ main_col_one, main_col_two = st.columns([2,2], gap="large")
152
+ #photo column
153
+ with main_col_one:
154
+ # Create a placeholder for the image
155
+ image_placeholder = st.empty()
156
+
157
+ with main_col_two:
158
+ with open("styles/detectiveMag.svg", "r") as file:
159
+ svg_content_detective_Mag = file.read()
160
+
161
+ #alignment between magnifying glass image and upload line
162
+ col1, col2, col3,col4 = st.columns([4,4,1,3], gap="small")
163
+ with col1:
164
+ st.write('')
165
+ with col2:
166
+ st.markdown(
167
+ """<p class = "upload_line"> Please upload the image </p>""",
168
+ unsafe_allow_html= True
169
+ )
170
+ with col3:
171
+ st.markdown(
172
+ f"<div class='detectiveMag1' style='bottom: 0%; right: 0%;'>{svg_content_detective_Mag}</div>",
173
+ unsafe_allow_html=True
174
+ )
175
+ with col4:
176
+ st.write('')
177
+
178
+
179
+
180
+ # introduce states
181
+ if "prev_image" not in st.session_state:
182
+ st.session_state.prev_image = None
183
+ if "reset_model" not in st.session_state:
184
+ st.session_state.reset_model = False
185
+ if "model_key" not in st.session_state:
186
+ st.session_state.model_key = "default_model_key"
187
+
188
+
189
+
190
+ # Upload image widget
191
+ user_image = st.file_uploader("png, jpg, or jpeg image", ['png', 'jpg', 'jpeg'], label_visibility='hidden')
192
+
193
+ if user_image:
194
+ # Convert the image to base64 encoding
195
+ image_bytes = user_image.read()
196
+ image_base64 = base64.b64encode(image_bytes).decode('utf-8')
197
+
198
+ # Display the image centered using HTML
199
+ image_placeholder.markdown(
200
+ f'<div style="display: flex; justify-content: center;">'
201
+ f'<img src="data:image/jpeg;base64,{image_base64}" max-width:"100%" height:"auto"/>'
202
+ f'</div>',
203
+ unsafe_allow_html=True
204
+ )
205
+
206
+ # model name select box widget reset condition. reset model name when a new image is uploaded
207
+ if user_image != st.session_state.prev_image:
208
+ if st.session_state.prev_image is not None:
209
+ st.session_state.model_key = "reset_model_key" if st.session_state.model_key == "default_model_key" else "default_model_key"
210
+ st.session_state.reset_model = True
211
+ st.session_state.prev_image = user_image # set prev image to current image
212
+
213
+ # model name select box widget
214
+ model_name = st.selectbox(
215
+ 'Choose a model',
216
+ ['CNN', 'Efficientnet', 'Efficientnet Art'],
217
+ index=None,
218
+ placeholder='choose an option',
219
+ key=st.session_state.model_key
220
+ )
221
+
222
+ # placeholder to display result
223
+ result_placeholder = st.empty()
224
 
225
  if user_image is not None and model_name is not None:
226
  predictions = []
227
+ # preprocess image and run the user selected model
228
  if model_name == 'CNN':
229
  print('CNN is running')
230
  predictions = pre_process_img(user_image)
 
236
  predictions = pre_process_img_effNetArt(user_image)
237
 
238
  if predictions[0] < 0.5:
239
+ result_word = "AI Generated"
240
  else:
241
  result_word = "REAL"
242
 
243
+ # display the result and the prediction
244
  if user_image is not None:
245
  if len(predictions) > 0:
246
+ result_placeholder.markdown(f"<div class='result'> <span class = 'prediction'>Prediction: {predictions[0][0]:.2%}</span> <br> It is a <span class = resultword> {result_word} </span> image. </div>", unsafe_allow_html=True)
 
247
 
248
  print(model_name)
249
  print(predictions[0])
styles/detectiveMag.svg CHANGED
styles/robot.png ADDED
styles/style.css CHANGED
@@ -1,8 +1,9 @@
1
  /* Reset and global styles */
2
  * {
3
- margin: 0;
4
  box-sizing: border-box;
5
- background-color: #001220 !important;
 
 
6
  }
7
 
8
  html,
@@ -16,12 +17,24 @@ body {
16
  /* Title Styling */
17
  .title {
18
  font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
19
- font-size: 2em; /* Scalable font size */
20
  text-align: center;
21
- background: linear-gradient(to right, #16f0da 0%, #ff93ed 100%);
22
  -webkit-background-clip: text;
23
  -webkit-text-fill-color: transparent;
24
- margin: 0 !important;
 
 
 
 
 
 
 
 
 
 
 
 
25
  }
26
 
27
  /* Upload Line */
@@ -32,6 +45,31 @@ body {
32
  color: white;
33
  margin: 1em 0;
34
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  @keyframes blink {
36
  25% {
37
  opacity: 0.5;
@@ -44,23 +82,13 @@ body {
44
  }
45
  }
46
  /* Base styles for magnifying glasses */
47
- .detectiveMag1,
48
- .detectiveMag2,
49
- .detectiveMag3 {
50
- position: fixed;
51
  width: 10vw;
52
  max-width: 100px;
53
  max-height: 100px;
54
  animation: blink 6s infinite linear;
55
  }
56
 
57
- .detectiveMag2 {
58
- animation-delay: 2s;
59
- }
60
-
61
- .detectiveMag3 {
62
- animation-delay: 4s;
63
- }
64
 
65
  /* Result Styling */
66
  .result {
@@ -76,10 +104,10 @@ body {
76
  text-transform: uppercase;
77
  background-image: linear-gradient(
78
  -225deg,
79
- #aae2e5 0%,
80
- #edaee8 29%,
81
- #421f7e 67%,
82
- #ff0099 100%
83
  );
84
  background-size: auto auto;
85
  background-clip: border-box;
@@ -115,43 +143,19 @@ body {
115
  .upload_line {
116
  font-size: 0.9rem;
117
  }
118
-
119
- .detectiveMag1,
120
- .detectiveMag2,
121
- .detectiveMag3 {
122
- width: 12vw; /* Slightly larger magnifying glasses */
123
- }
124
-
125
  .result {
126
  font-size: 1.2rem;
127
  }
128
  }
129
 
130
- @media (max-width: 924px) {
131
- .detectiveMag1,
132
- .detectiveMag2,
133
- .detectiveMag3 {
134
- width: 10vw; /* Larger magnifying glasses */
135
- background-color: transparent !important;
136
- }
137
- }
138
-
139
  @media (max-width: 768px) {
140
  .title {
141
  font-size: 1.5em;
142
  }
143
-
144
  .upload_line {
145
  font-size: 0.8rem;
146
  }
147
 
148
- .detectiveMag1,
149
- .detectiveMag2,
150
- .detectiveMag3 {
151
- width: 15vw; /* Larger magnifying glasses */
152
- background-color: transparent !important;
153
- }
154
-
155
  .result {
156
  font-size: 1rem;
157
  }
@@ -159,27 +163,23 @@ body {
159
 
160
  @media (max-width: 480px) {
161
  .title {
162
- font-size: 1.2em;
163
- }
164
- .upload_line {
165
- font-size: 0.7rem;
166
- }
167
-
168
- .detectiveMag1,
169
- .detectiveMag2,
170
- .detectiveMag3 {
171
- width: 20vw; /* Even larger magnifying glasses */
172
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
173
 
174
  .result {
175
  font-size: 0.9rem;
176
  }
177
  }
178
-
179
- @media (max-width: 390px) {
180
- .detectiveMag1,
181
- .detectiveMag2,
182
- .detectiveMag3 {
183
- visibility: hidden;
184
- }
185
- }
 
1
  /* Reset and global styles */
2
  * {
 
3
  box-sizing: border-box;
4
+ /* background-color: #001220 !important; */
5
+ background-color:#0f0f31 !important;
6
+
7
  }
8
 
9
  html,
 
17
  /* Title Styling */
18
  .title {
19
  font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
20
+ font-size: 32px; /* Scalable font size */
21
  text-align: center;
22
+ background: linear-gradient(to right, #5af9fb 0%, #1C6DF3 100%);
23
  -webkit-background-clip: text;
24
  -webkit-text-fill-color: transparent;
25
+ margin-top: 0 !important;
26
+
27
+ }
28
+ /* robot image */
29
+ .st-emotion-cache-1xf0csu {
30
+ max-width: 55px !important;
31
+ }
32
+
33
+ /* main container */
34
+ .stMainBlockContainer {
35
+ padding-left: 5rem;
36
+ padding-right: 5rem;
37
+ max-width: 100%;
38
  }
39
 
40
  /* Upload Line */
 
45
  color: white;
46
  margin: 1em 0;
47
  }
48
+
49
+ /* upload image widget customization */
50
+ .stFileUploader span, button {
51
+ color: white !important; /* Set the font color to white */
52
+ }
53
+
54
+ .stSelectbox label {
55
+ color: white;
56
+ }
57
+ .stSelectbox > div div {
58
+ color: white;
59
+ }
60
+
61
+ /* select box widget customization*/
62
+ .st-cf {
63
+ color: white !important;
64
+ }
65
+
66
+ .stSelectbox div[data-baseweb="select"] > div:first-child {
67
+ border-color: #2d408d;
68
+ }
69
+ .st-emotion-cache-sy3zga {
70
+ color: white !important;
71
+ }
72
+
73
  @keyframes blink {
74
  25% {
75
  opacity: 0.5;
 
82
  }
83
  }
84
  /* Base styles for magnifying glasses */
85
+ .detectiveMag1{
 
 
 
86
  width: 10vw;
87
  max-width: 100px;
88
  max-height: 100px;
89
  animation: blink 6s infinite linear;
90
  }
91
 
 
 
 
 
 
 
 
92
 
93
  /* Result Styling */
94
  .result {
 
104
  text-transform: uppercase;
105
  background-image: linear-gradient(
106
  -225deg,
107
+ #8ad8e6 0%,
108
+ #a438f2 29%,
109
+ #ff007f 67%,
110
+ #e8a7c8 100%
111
  );
112
  background-size: auto auto;
113
  background-clip: border-box;
 
143
  .upload_line {
144
  font-size: 0.9rem;
145
  }
 
 
 
 
 
 
 
146
  .result {
147
  font-size: 1.2rem;
148
  }
149
  }
150
 
 
 
 
 
 
 
 
 
 
151
  @media (max-width: 768px) {
152
  .title {
153
  font-size: 1.5em;
154
  }
 
155
  .upload_line {
156
  font-size: 0.8rem;
157
  }
158
 
 
 
 
 
 
 
 
159
  .result {
160
  font-size: 1rem;
161
  }
 
163
 
164
  @media (max-width: 480px) {
165
  .title {
166
+ font-size: 16px !important;
 
 
 
 
 
 
 
 
 
167
  }
168
+ /* robot image */
169
+ .st-emotion-cache-1xf0csu {
170
+ display: none;
171
+ }
172
+ .upload_line {
173
+ margin:0rem !important;
174
+ }
175
+ .detectiveMag1{
176
+ display: none;
177
+ }
178
+ .st-emotion-cache-ocqkz7 {
179
+ gap: 0 !important;
180
+ }
181
 
182
  .result {
183
  font-size: 0.9rem;
184
  }
185
  }