asad231 commited on
Commit
e61c7d1
Β·
verified Β·
1 Parent(s): 2bb11c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +144 -15
app.py CHANGED
@@ -84,6 +84,134 @@
84
 
85
  # st.markdown("πŸ”Ή **Developed for Fake News & Deepfake Detection Hackathon**")
86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  import streamlit as st
88
  import numpy as np
89
  import cv2
@@ -100,11 +228,11 @@ from tensorflow.keras.preprocessing.image import load_img, img_to_array
100
  # ---- Page Configuration ----
101
  st.set_page_config(page_title="Fake & Deepfake Detection", layout="wide")
102
 
103
- st.title("πŸ“° Fake News & Deepfake Detection Tool")
104
- st.write("πŸš€ Detect Fake News, Deepfake Images, and Videos using AI")
105
 
106
  # Load Models
107
- fake_news_detector = pipeline("text-classification", model="microsoft/deberta-v3-base")
108
 
109
  # Load Deepfake Detection Models
110
  base_model_image = Xception(weights="imagenet", include_top=False)
@@ -138,32 +266,33 @@ def detect_deepfake_image(image_path):
138
  return {"label": label, "score": confidence}
139
 
140
  # ---- Fake News Detection Section ----
141
- st.subheader("πŸ“ Fake News Detection")
142
  news_input = st.text_area("Enter News Text:", placeholder="Type here...")
143
 
144
  if st.button("Check News"):
145
- st.write("πŸ” Processing...")
146
- prediction = fake_news_detector(news_input)
147
- label = prediction[0]['label']
148
- confidence = prediction[0]['score']
 
149
 
150
- if label == "FAKE":
151
  st.error(f"⚠️ Result: This news is FAKE. (Confidence: {confidence:.2f})")
152
  else:
153
  st.success(f"βœ… Result: This news is REAL. (Confidence: {confidence:.2f})")
154
 
155
  # ---- Deepfake Image Detection Section ----
156
- st.subheader("πŸ“Έ Deepfake Image Detection")
157
  uploaded_image = st.file_uploader("Upload an Image", type=["jpg", "png", "jpeg"])
158
 
159
  if uploaded_image is not None:
160
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".jpg")
161
  img = Image.open(uploaded_image).convert("RGB")
162
  img.save(temp_file.name, "JPEG")
163
- st.image(temp_file.name, caption="πŸ–ΌοΈ Uploaded Image", use_column_width=True)
164
 
165
  if st.button("Analyze Image"):
166
- st.write("πŸ” Processing...")
167
  result = detect_deepfake_image(temp_file.name)
168
 
169
  if result["label"] == "FAKE":
@@ -172,7 +301,7 @@ if uploaded_image is not None:
172
  st.success(f"βœ… Result: This image is Real. (Confidence: {1 - result['score']:.2f})")
173
 
174
  # ---- Deepfake Video Detection Section ----
175
- st.subheader("πŸŽ₯ Deepfake Video Detection")
176
  uploaded_video = st.file_uploader("Upload a Video", type=["mp4", "avi", "mov"])
177
 
178
  def detect_deepfake_video(video_path):
@@ -202,7 +331,7 @@ if uploaded_video is not None:
202
  f.write(uploaded_video.read())
203
 
204
  if st.button("Analyze Video"):
205
- st.write("πŸ” Processing...")
206
  result = detect_deepfake_video(temp_file.name)
207
 
208
  if result["label"] == "FAKE":
@@ -210,4 +339,4 @@ if uploaded_video is not None:
210
  else:
211
  st.success(f"βœ… Result: This video is Real. (Confidence: {1 - result['score']:.2f})")
212
 
213
- st.markdown("πŸ”Ή **Developed for Fake News & Deepfake Detection Hackathon**")
 
84
 
85
  # st.markdown("πŸ”Ή **Developed for Fake News & Deepfake Detection Hackathon**")
86
 
87
+ # import streamlit as st
88
+ # import numpy as np
89
+ # import cv2
90
+ # import tempfile
91
+ # import os
92
+ # from PIL import Image
93
+ # import tensorflow as tf
94
+ # from transformers import pipeline
95
+ # from tensorflow.keras.applications import Xception, EfficientNetB7
96
+ # from tensorflow.keras.models import Model
97
+ # from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
98
+ # from tensorflow.keras.preprocessing.image import load_img, img_to_array
99
+
100
+ # # ---- Page Configuration ----
101
+ # st.set_page_config(page_title="Fake & Deepfake Detection", layout="wide")
102
+
103
+ # st.title("πŸ“° Fake News & Deepfake Detection Tool")
104
+ # st.write("πŸš€ Detect Fake News, Deepfake Images, and Videos using AI")
105
+
106
+ # # Load Models
107
+ # fake_news_detector = pipeline("text-classification", model="microsoft/deberta-v3-base")
108
+
109
+ # # Load Deepfake Detection Models
110
+ # base_model_image = Xception(weights="imagenet", include_top=False)
111
+ # base_model_image.trainable = False # Freeze base layers
112
+ # x = GlobalAveragePooling2D()(base_model_image.output)
113
+ # x = Dense(1024, activation="relu")(x)
114
+ # x = Dense(1, activation="sigmoid")(x) # Sigmoid for probability output
115
+ # deepfake_image_model = Model(inputs=base_model_image.input, outputs=x)
116
+
117
+ # base_model_video = EfficientNetB7(weights="imagenet", include_top=False)
118
+ # base_model_video.trainable = False
119
+ # x = GlobalAveragePooling2D()(base_model_video.output)
120
+ # x = Dense(1024, activation="relu")(x)
121
+ # x = Dense(1, activation="sigmoid")(x)
122
+ # deepfake_video_model = Model(inputs=base_model_video.input, outputs=x)
123
+
124
+ # # Function to Preprocess Image
125
+ # def preprocess_image(image_path):
126
+ # img = load_img(image_path, target_size=(299, 299)) # Xception expects 299x299
127
+ # img = img_to_array(img)
128
+ # img = np.expand_dims(img, axis=0)
129
+ # img /= 255.0 # Normalize pixel values
130
+ # return img
131
+
132
+ # # Function to Detect Deepfake Image
133
+ # def detect_deepfake_image(image_path):
134
+ # image = preprocess_image(image_path)
135
+ # prediction = deepfake_image_model.predict(image)[0][0]
136
+ # confidence = round(float(prediction), 2)
137
+ # label = "FAKE" if confidence > 0.5 else "REAL"
138
+ # return {"label": label, "score": confidence}
139
+
140
+ # # ---- Fake News Detection Section ----
141
+ # st.subheader("πŸ“ Fake News Detection")
142
+ # news_input = st.text_area("Enter News Text:", placeholder="Type here...")
143
+
144
+ # if st.button("Check News"):
145
+ # st.write("πŸ” Processing...")
146
+ # prediction = fake_news_detector(news_input)
147
+ # label = prediction[0]['label']
148
+ # confidence = prediction[0]['score']
149
+
150
+ # if label == "FAKE":
151
+ # st.error(f"⚠️ Result: This news is FAKE. (Confidence: {confidence:.2f})")
152
+ # else:
153
+ # st.success(f"βœ… Result: This news is REAL. (Confidence: {confidence:.2f})")
154
+
155
+ # # ---- Deepfake Image Detection Section ----
156
+ # st.subheader("πŸ“Έ Deepfake Image Detection")
157
+ # uploaded_image = st.file_uploader("Upload an Image", type=["jpg", "png", "jpeg"])
158
+
159
+ # if uploaded_image is not None:
160
+ # temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".jpg")
161
+ # img = Image.open(uploaded_image).convert("RGB")
162
+ # img.save(temp_file.name, "JPEG")
163
+ # st.image(temp_file.name, caption="πŸ–ΌοΈ Uploaded Image", use_column_width=True)
164
+
165
+ # if st.button("Analyze Image"):
166
+ # st.write("πŸ” Processing...")
167
+ # result = detect_deepfake_image(temp_file.name)
168
+
169
+ # if result["label"] == "FAKE":
170
+ # st.error(f"⚠️ Result: This image is a Deepfake. (Confidence: {result['score']:.2f})")
171
+ # else:
172
+ # st.success(f"βœ… Result: This image is Real. (Confidence: {1 - result['score']:.2f})")
173
+
174
+ # # ---- Deepfake Video Detection Section ----
175
+ # st.subheader("πŸŽ₯ Deepfake Video Detection")
176
+ # uploaded_video = st.file_uploader("Upload a Video", type=["mp4", "avi", "mov"])
177
+
178
+ # def detect_deepfake_video(video_path):
179
+ # cap = cv2.VideoCapture(video_path)
180
+ # frame_scores = []
181
+
182
+ # while cap.isOpened():
183
+ # ret, frame = cap.read()
184
+ # if not ret:
185
+ # break
186
+
187
+ # frame_path = "temp_frame.jpg"
188
+ # cv2.imwrite(frame_path, frame)
189
+ # result = detect_deepfake_image(frame_path)
190
+ # frame_scores.append(result["score"])
191
+ # os.remove(frame_path)
192
+
193
+ # cap.release()
194
+ # avg_score = np.mean(frame_scores)
195
+ # final_label = "FAKE" if avg_score > 0.5 else "REAL"
196
+ # return {"label": final_label, "score": round(float(avg_score), 2)}
197
+
198
+ # if uploaded_video is not None:
199
+ # st.video(uploaded_video)
200
+ # temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
201
+ # with open(temp_file.name, "wb") as f:
202
+ # f.write(uploaded_video.read())
203
+
204
+ # if st.button("Analyze Video"):
205
+ # st.write("πŸ” Processing...")
206
+ # result = detect_deepfake_video(temp_file.name)
207
+
208
+ # if result["label"] == "FAKE":
209
+ # st.warning(f"⚠️ Result: This video contains Deepfake elements. (Confidence: {result['score']:.2f})")
210
+ # else:
211
+ # st.success(f"βœ… Result: This video is Real. (Confidence: {1 - result['score']:.2f})")
212
+
213
+ # st.markdown("πŸ”Ή **Developed for Fake News & Deepfake Detection Hackathon**")
214
+
215
  import streamlit as st
216
  import numpy as np
217
  import cv2
 
228
  # ---- Page Configuration ----
229
  st.set_page_config(page_title="Fake & Deepfake Detection", layout="wide")
230
 
231
+ st.title("\U0001F4F0 Fake News & Deepfake Detection Tool")
232
+ st.write("\U0001F680 Detect Fake News, Deepfake Images, and Videos using AI")
233
 
234
  # Load Models
235
+ fake_news_detector = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
236
 
237
  # Load Deepfake Detection Models
238
  base_model_image = Xception(weights="imagenet", include_top=False)
 
266
  return {"label": label, "score": confidence}
267
 
268
  # ---- Fake News Detection Section ----
269
+ st.subheader("\U0001F4DD Fake News Detection")
270
  news_input = st.text_area("Enter News Text:", placeholder="Type here...")
271
 
272
  if st.button("Check News"):
273
+ st.write("\U0001F50D Processing...")
274
+ labels = ["fake news", "real news"]
275
+ prediction = fake_news_detector(news_input, labels)
276
+ label = prediction['labels'][0]
277
+ confidence = prediction['scores'][0]
278
 
279
+ if label == "fake news":
280
  st.error(f"⚠️ Result: This news is FAKE. (Confidence: {confidence:.2f})")
281
  else:
282
  st.success(f"βœ… Result: This news is REAL. (Confidence: {confidence:.2f})")
283
 
284
  # ---- Deepfake Image Detection Section ----
285
+ st.subheader("\U0001F4F8 Deepfake Image Detection")
286
  uploaded_image = st.file_uploader("Upload an Image", type=["jpg", "png", "jpeg"])
287
 
288
  if uploaded_image is not None:
289
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".jpg")
290
  img = Image.open(uploaded_image).convert("RGB")
291
  img.save(temp_file.name, "JPEG")
292
+ st.image(temp_file.name, caption="\U0001F5BC️ Uploaded Image", use_column_width=True)
293
 
294
  if st.button("Analyze Image"):
295
+ st.write("\U0001F50D Processing...")
296
  result = detect_deepfake_image(temp_file.name)
297
 
298
  if result["label"] == "FAKE":
 
301
  st.success(f"βœ… Result: This image is Real. (Confidence: {1 - result['score']:.2f})")
302
 
303
  # ---- Deepfake Video Detection Section ----
304
+ st.subheader("\U0001F3A5 Deepfake Video Detection")
305
  uploaded_video = st.file_uploader("Upload a Video", type=["mp4", "avi", "mov"])
306
 
307
  def detect_deepfake_video(video_path):
 
331
  f.write(uploaded_video.read())
332
 
333
  if st.button("Analyze Video"):
334
+ st.write("\U0001F50D Processing...")
335
  result = detect_deepfake_video(temp_file.name)
336
 
337
  if result["label"] == "FAKE":
 
339
  else:
340
  st.success(f"βœ… Result: This video is Real. (Confidence: {1 - result['score']:.2f})")
341
 
342
+ st.markdown("πŸ”Ή **Developed for Fake News & Deepfake Detection Hackathon**")