asad231 commited on
Commit
c7fed8b
·
verified ·
1 Parent(s): 494b03f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -41
app.py CHANGED
@@ -275,7 +275,7 @@ def detect_deepfake_video(video_path):
275
 
276
  return {"label": final_label, "score": confidence}
277
 
278
- # Download video from direct MP4 URL
279
  def download_video(url):
280
  try:
281
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
@@ -290,58 +290,34 @@ def download_video(url):
290
  except Exception as e:
291
  return None
292
 
293
- # Download YouTube video
294
- def download_youtube_video(youtube_url):
295
- try:
296
- yt = YouTube(youtube_url)
297
- video_stream = yt.streams.filter(file_extension="mp4", res="360p").first()
298
- if not video_stream:
299
- video_stream = yt.streams.get_lowest_resolution()
300
-
301
- temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
302
- video_stream.download(filename=temp_file.name)
303
-
304
- return temp_file.name # Return local file path
305
- except Exception as e:
306
- return None
307
-
308
- # Select Video Source
309
  video_path = None
310
- video_display_url = None
311
 
312
- # Process Uploaded Video
313
  if uploaded_video is not None:
314
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
315
  with open(temp_file.name, "wb") as f:
316
  f.write(uploaded_video.read())
317
  video_path = temp_file.name # Set video path for detection
318
- video_display_url = temp_file.name
319
 
320
- # Process Video from URL
321
  elif video_url:
322
  if "youtube.com" in video_url or "youtu.be" in video_url:
323
- st.write("⏳ Downloading YouTube Video...")
324
- video_path = download_youtube_video(video_url) # Download YouTube video
 
 
 
 
325
  if video_path:
326
- st.success("✅ YouTube video downloaded successfully!")
327
- video_display_url = video_path
328
  else:
329
- st.error(" Failed to download YouTube video. Try another link.")
330
- else:
331
- video_path = download_video(video_url) # Download direct MP4 video
332
- video_display_url = video_path
333
 
334
- # ✅ Display Video Properly
335
- if video_display_url:
336
- st.markdown("### 🎬 Video Preview")
337
- st.video(video_display_url) # Play Video
338
 
339
- # "Analyze Video" بٹن ہمیشہ شو ہوگا
340
- analyze_button = st.button("Analyze Video")
341
-
342
- # 🚀 اگر ویڈیو موجود ہے تو تجزیہ کریں
343
- if analyze_button:
344
- if video_path and os.path.exists(video_path):
345
  st.write("🔍 Processing... Please wait.")
346
  result = detect_deepfake_video(video_path)
347
 
@@ -351,7 +327,5 @@ if analyze_button:
351
  st.success(f"✅ This video appears to be REAL. (Confidence: {1 - result['score']:.2f})")
352
  else:
353
  st.warning("⚠️ Unable to analyze the video. Please try a different file.")
354
- else:
355
- st.warning("⚠️ Invalid video. Please check the file or URL.")
356
 
357
  st.markdown("🔹 **Developed for Fake News & Deepfake Detection**")
 
275
 
276
  return {"label": final_label, "score": confidence}
277
 
278
+ # Download direct MP4 video
279
  def download_video(url):
280
  try:
281
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
 
290
  except Exception as e:
291
  return None
292
 
293
+ # Display Video (YouTube Embed or Local)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
294
  video_path = None
 
295
 
 
296
  if uploaded_video is not None:
297
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
298
  with open(temp_file.name, "wb") as f:
299
  f.write(uploaded_video.read())
300
  video_path = temp_file.name # Set video path for detection
301
+ st.video(video_path) # Show uploaded video
302
 
 
303
  elif video_url:
304
  if "youtube.com" in video_url or "youtu.be" in video_url:
305
+ st.markdown(
306
+ f'<iframe width="560" height="315" src="{video_url.replace("watch?v=", "embed/")}" frameborder="0" allowfullscreen></iframe>',
307
+ unsafe_allow_html=True,
308
+ )
309
+ else:
310
+ video_path = download_video(video_url) # Download MP4
311
  if video_path:
312
+ st.video(video_path) # Show downloaded MP4
 
313
  else:
314
+ st.warning("⚠️ Invalid MP4 URL.")
 
 
 
315
 
316
+ # ✅ "Analyze Video" Button (Only for Local/MP4)
317
+ if uploaded_video or (video_url and not "youtube.com" in video_url):
318
+ analyze_button = st.button("Analyze Video")
 
319
 
320
+ if analyze_button and video_path:
 
 
 
 
 
321
  st.write("🔍 Processing... Please wait.")
322
  result = detect_deepfake_video(video_path)
323
 
 
327
  st.success(f"✅ This video appears to be REAL. (Confidence: {1 - result['score']:.2f})")
328
  else:
329
  st.warning("⚠️ Unable to analyze the video. Please try a different file.")
 
 
330
 
331
  st.markdown("🔹 **Developed for Fake News & Deepfake Detection**")