saakshigupta commited on
Commit
42b053e
·
verified ·
1 Parent(s): c90870b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +196 -72
app.py CHANGED
@@ -26,7 +26,7 @@ st.set_page_config(
26
  )
27
 
28
  # Main title and description
29
- st.title("Deepfake Image Analyzer")
30
  st.markdown("Analyze images for deepfake manipulation with multi-stage analysis")
31
 
32
  # Check for GPU availability
@@ -39,10 +39,54 @@ def check_gpu():
39
  st.sidebar.warning("⚠️ No GPU detected. Analysis will be slower.")
40
  return False
41
 
42
- # Set constant values for generation parameters
43
- TEMPERATURE = 0.7
44
- MAX_TOKENS = 500
45
- CUSTOM_INSTRUCTION = "Focus on analyzing the highlighted regions from the GradCAM visualization. Examine facial inconsistencies, lighting irregularities, and other artifacts visible in the heat map."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
  # ----- GradCAM Implementation -----
48
 
@@ -514,7 +558,7 @@ def load_llm_model():
514
  return None, None
515
 
516
  # Analyze image function
517
- def analyze_image_with_llm(image, gradcam_overlay, face_box, pred_label, confidence, question, model, tokenizer, custom_instruction=""):
518
  # Create a prompt that includes GradCAM information
519
  if custom_instruction.strip():
520
  full_prompt = f"{question}\n\nThe image has been processed with GradCAM and classified as {pred_label} with confidence {confidence:.2f}. Focus on the highlighted regions in red/yellow which show the areas the detection model found suspicious.\n\n{custom_instruction}"
@@ -549,9 +593,9 @@ def analyze_image_with_llm(image, gradcam_overlay, face_box, pred_label, confide
549
  with torch.no_grad():
550
  output_ids = model.generate(
551
  **inputs,
552
- max_new_tokens=MAX_TOKENS,
553
  use_cache=True,
554
- temperature=TEMPERATURE,
555
  top_p=0.9
556
  )
557
 
@@ -566,67 +610,6 @@ def analyze_image_with_llm(image, gradcam_overlay, face_box, pred_label, confide
566
 
567
  return result
568
 
569
- # Sidebar chat interface
570
- def chat_interface():
571
- st.sidebar.title("Deepfake Analysis Chat")
572
-
573
- # Display chat history
574
- if 'chat_history' not in st.session_state:
575
- st.session_state.chat_history = []
576
-
577
- # Display chat messages
578
- for i, (question, answer) in enumerate(st.session_state.chat_history):
579
- st.sidebar.markdown(f"**You:** {question}")
580
- st.sidebar.markdown(f"**AI:** {answer}")
581
- st.sidebar.markdown("---")
582
-
583
- # Only show the chat interface if image has been analyzed
584
- if hasattr(st.session_state, 'current_image'):
585
- # New question input
586
- new_question = st.sidebar.text_area("Ask about the image:", height=100)
587
-
588
- # Send button
589
- if st.sidebar.button("Send Question", type="primary"):
590
- if new_question:
591
- try:
592
- # Add caption info if it's the first question
593
- caption_text = ""
594
- if not st.session_state.chat_history:
595
- if hasattr(st.session_state, 'image_caption'):
596
- caption_text += f"\n\nImage Description:\n{st.session_state.image_caption}"
597
- if hasattr(st.session_state, 'gradcam_caption'):
598
- caption_text += f"\n\nGradCAM Analysis:\n{st.session_state.gradcam_caption}"
599
-
600
- full_question = new_question + caption_text
601
- else:
602
- full_question = new_question
603
-
604
- result = analyze_image_with_llm(
605
- st.session_state.current_image,
606
- st.session_state.current_overlay,
607
- st.session_state.current_face_box,
608
- st.session_state.current_pred_label,
609
- st.session_state.current_confidence,
610
- full_question,
611
- st.session_state.llm_model,
612
- st.session_state.tokenizer,
613
- custom_instruction=CUSTOM_INSTRUCTION
614
- )
615
-
616
- # Add to chat history
617
- st.session_state.chat_history.append((new_question, result))
618
- st.experimental_rerun()
619
-
620
- except Exception as e:
621
- st.sidebar.error(f"Error during LLM analysis: {str(e)}")
622
- else:
623
- st.sidebar.info("Upload and analyze an image to start chatting")
624
-
625
- # Clear chat button
626
- if st.session_state.chat_history and st.sidebar.button("Clear Chat History"):
627
- st.session_state.chat_history = []
628
- st.experimental_rerun()
629
-
630
  # Main app
631
  def main():
632
  # Initialize session state variables
@@ -644,8 +627,9 @@ def main():
644
  st.session_state.blip_processor = None
645
  st.session_state.blip_model = None
646
 
647
- # Set up chat interface in sidebar
648
- chat_interface()
 
649
 
650
  # Create expanders for each stage
651
  with st.expander("Stage 1: Model Loading", expanded=True):
@@ -795,4 +779,144 @@ def main():
795
  except Exception as e:
796
  st.error(f"Error processing image: {str(e)}")
797
  import traceback
798
- st.error(traceback.format_exc()) # This will show the full error traceback
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  )
27
 
28
  # Main title and description
29
+ st.title("Advanced Deepfake Image Analyzer")
30
  st.markdown("Analyze images for deepfake manipulation with multi-stage analysis")
31
 
32
  # Check for GPU availability
 
39
  st.sidebar.warning("⚠️ No GPU detected. Analysis will be slower.")
40
  return False
41
 
42
+ # Sidebar components
43
+ st.sidebar.title("Options")
44
+
45
+ # Temperature slider
46
+ temperature = st.sidebar.slider(
47
+ "Temperature",
48
+ min_value=0.1,
49
+ max_value=1.0,
50
+ value=0.7,
51
+ step=0.1,
52
+ help="Higher values make output more random, lower values more deterministic"
53
+ )
54
+
55
+ # Max response length slider
56
+ max_tokens = st.sidebar.slider(
57
+ "Maximum Response Length",
58
+ min_value=100,
59
+ max_value=1000,
60
+ value=500,
61
+ step=50,
62
+ help="The maximum number of tokens in the response"
63
+ )
64
+
65
+ # Custom instruction text area in sidebar
66
+ custom_instruction = st.sidebar.text_area(
67
+ "Custom Instructions (Advanced)",
68
+ value="Focus on analyzing the highlighted regions from the GradCAM visualization. Examine facial inconsistencies, lighting irregularities, and other artifacts visible in the heat map.",
69
+ help="Add specific instructions for the LLM analysis"
70
+ )
71
+
72
+ # About section in sidebar
73
+ st.sidebar.markdown("---")
74
+ st.sidebar.subheader("About")
75
+ st.sidebar.markdown("""
76
+ This analyzer performs multi-stage detection:
77
+ 1. **Initial Detection**: CLIP-based classifier
78
+ 2. **GradCAM Visualization**: Highlights suspicious regions
79
+ 3. **Image Captioning**: BLIP model describes the image content
80
+ 4. **LLM Analysis**: Fine-tuned Llama 3.2 Vision provides detailed explanations
81
+
82
+ The system looks for:
83
+ - Facial inconsistencies
84
+ - Unnatural movements
85
+ - Lighting issues
86
+ - Texture anomalies
87
+ - Edge artifacts
88
+ - Blending problems
89
+ """)
90
 
91
  # ----- GradCAM Implementation -----
92
 
 
558
  return None, None
559
 
560
  # Analyze image function
561
+ def analyze_image_with_llm(image, gradcam_overlay, face_box, pred_label, confidence, question, model, tokenizer, temperature=0.7, max_tokens=500, custom_instruction=""):
562
  # Create a prompt that includes GradCAM information
563
  if custom_instruction.strip():
564
  full_prompt = f"{question}\n\nThe image has been processed with GradCAM and classified as {pred_label} with confidence {confidence:.2f}. Focus on the highlighted regions in red/yellow which show the areas the detection model found suspicious.\n\n{custom_instruction}"
 
593
  with torch.no_grad():
594
  output_ids = model.generate(
595
  **inputs,
596
+ max_new_tokens=max_tokens,
597
  use_cache=True,
598
+ temperature=temperature,
599
  top_p=0.9
600
  )
601
 
 
610
 
611
  return result
612
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
613
  # Main app
614
  def main():
615
  # Initialize session state variables
 
627
  st.session_state.blip_processor = None
628
  st.session_state.blip_model = None
629
 
630
+ # Initialize chat history
631
+ if 'chat_history' not in st.session_state:
632
+ st.session_state.chat_history = []
633
 
634
  # Create expanders for each stage
635
  with st.expander("Stage 1: Model Loading", expanded=True):
 
779
  except Exception as e:
780
  st.error(f"Error processing image: {str(e)}")
781
  import traceback
782
+ st.error(traceback.format_exc()) # This will show the full error traceback
783
+
784
+ # Image Analysis Summary section - AFTER Stage 2
785
+ if hasattr(st.session_state, 'current_image') and (hasattr(st.session_state, 'image_caption') or hasattr(st.session_state, 'gradcam_caption')):
786
+ with st.expander("Image Analysis Summary", expanded=True):
787
+ st.subheader("Generated Descriptions and Analysis")
788
+
789
+ # Display image, captions, and results in organized layout with proper formatting
790
+ col1, col2 = st.columns([1, 2])
791
+
792
+ with col1:
793
+ # Display original image and overlay side by side with controlled size
794
+ st.image(st.session_state.current_image, caption="Original Image", width=300)
795
+ if hasattr(st.session_state, 'current_overlay'):
796
+ st.image(st.session_state.current_overlay, caption="GradCAM Overlay", width=300)
797
+
798
+ with col2:
799
+ # Detection result
800
+ if hasattr(st.session_state, 'current_pred_label'):
801
+ st.markdown("### Detection Result")
802
+ st.markdown(f"**Classification:** {st.session_state.current_pred_label} (Confidence: {st.session_state.current_confidence:.2%})")
803
+ st.markdown("---")
804
+
805
+ # Image description
806
+ if hasattr(st.session_state, 'image_caption'):
807
+ st.markdown("### Image Description")
808
+ st.markdown(st.session_state.image_caption)
809
+ st.markdown("---")
810
+
811
+ # GradCAM analysis
812
+ if hasattr(st.session_state, 'gradcam_caption'):
813
+ st.markdown("### GradCAM Analysis")
814
+ st.markdown(st.session_state.gradcam_caption)
815
+
816
+ # LLM Analysis section - AFTER Image Analysis Summary
817
+ with st.expander("Stage 3: Detailed Analysis with Vision LLM", expanded=False):
818
+ if hasattr(st.session_state, 'current_image') and st.session_state.llm_model_loaded:
819
+ st.subheader("Detailed Deepfake Analysis")
820
+
821
+ # Display chat history
822
+ for i, (question, answer) in enumerate(st.session_state.chat_history):
823
+ st.markdown(f"**Question {i+1}:** {question}")
824
+ st.markdown(f"**Answer:** {answer}")
825
+ st.markdown("---")
826
+
827
+ # Include both captions in the prompt if available
828
+ caption_text = ""
829
+ if hasattr(st.session_state, 'image_caption'):
830
+ caption_text += f"\n\nImage Description:\n{st.session_state.image_caption}"
831
+
832
+ if hasattr(st.session_state, 'gradcam_caption'):
833
+ caption_text += f"\n\nGradCAM Analysis:\n{st.session_state.gradcam_caption}"
834
+
835
+ # Default question with option to customize
836
+ default_question = f"This image has been classified as {st.session_state.current_pred_label}. Analyze the key features that led to this classification, focusing on the highlighted areas in the GradCAM visualization. Provide both a technical explanation for experts and a simple explanation for non-technical users."
837
+
838
+ # User input for new question
839
+ new_question = st.text_area("Ask a question about the image:", value=default_question if not st.session_state.chat_history else "", height=100)
840
+
841
+ # Analyze button and Clear Chat button in the same row
842
+ col1, col2 = st.columns([3, 1])
843
+ with col1:
844
+ analyze_button = st.button("🔍 Send Question", type="primary")
845
+ with col2:
846
+ clear_button = st.button("🗑️ Clear Chat History")
847
+
848
+ if clear_button:
849
+ st.session_state.chat_history = []
850
+ st.experimental_rerun()
851
+
852
+ if analyze_button and new_question:
853
+ try:
854
+ # Add caption info if it's the first question
855
+ if not st.session_state.chat_history:
856
+ full_question = new_question + caption_text
857
+ else:
858
+ full_question = new_question
859
+
860
+ result = analyze_image_with_llm(
861
+ st.session_state.current_image,
862
+ st.session_state.current_overlay,
863
+ st.session_state.current_face_box,
864
+ st.session_state.current_pred_label,
865
+ st.session_state.current_confidence,
866
+ full_question,
867
+ st.session_state.llm_model,
868
+ st.session_state.tokenizer,
869
+ temperature=temperature,
870
+ max_tokens=max_tokens,
871
+ custom_instruction=custom_instruction
872
+ )
873
+
874
+ # Add to chat history
875
+ st.session_state.chat_history.append((new_question, result))
876
+
877
+ # Display the latest result too
878
+ st.success("✅ Analysis complete!")
879
+
880
+ # Check if the result contains both technical and non-technical explanations
881
+ if "Technical" in result and "Non-Technical" in result:
882
+ try:
883
+ # Split the result into technical and non-technical sections
884
+ parts = result.split("Non-Technical")
885
+ technical = parts[0]
886
+ non_technical = "Non-Technical" + parts[1]
887
+
888
+ # Display in two columns
889
+ tech_col, simple_col = st.columns(2)
890
+ with tech_col:
891
+ st.subheader("Technical Analysis")
892
+ st.markdown(technical)
893
+
894
+ with simple_col:
895
+ st.subheader("Simple Explanation")
896
+ st.markdown(non_technical)
897
+ except Exception as e:
898
+ # Fallback if splitting fails
899
+ st.subheader("Analysis Result")
900
+ st.markdown(result)
901
+ else:
902
+ # Just display the whole result
903
+ st.subheader("Analysis Result")
904
+ st.markdown(result)
905
+
906
+ # Rerun to update the chat history display
907
+ st.experimental_rerun()
908
+
909
+ except Exception as e:
910
+ st.error(f"Error during LLM analysis: {str(e)}")
911
+
912
+ elif not hasattr(st.session_state, 'current_image'):
913
+ st.warning("⚠️ Please upload an image and complete the initial detection first.")
914
+ else:
915
+ st.warning("⚠️ Please load the Vision LLM to perform detailed analysis.")
916
+
917
+ # Footer
918
+ st.markdown("---")
919
+ st.caption("Advanced Deepfake Image Analyzer with Structured BLIP Captioning")
920
+
921
+ if __name__ == "__main__":
922
+ main()