saakshigupta commited on
Commit
023ba3f
Β·
verified Β·
1 Parent(s): aba09b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +152 -10
app.py CHANGED
@@ -20,7 +20,7 @@ warnings.filterwarnings("ignore", category=UserWarning)
20
 
21
  # App title and description
22
  st.set_page_config(
23
- page_title="Deepfake Image Analyser",
24
  layout="wide",
25
  page_icon="πŸ”"
26
  )
@@ -42,9 +42,9 @@ def check_gpu():
42
  # Sidebar components
43
  st.sidebar.title("Options")
44
 
45
- # Fixed values instead of sliders
46
- temperature = 0.7 # Fixed temperature value
47
- max_tokens = 500 # Fixed max tokens value
48
 
49
  # Custom instruction text area in sidebar
50
  custom_instruction = st.sidebar.text_area(
@@ -542,11 +542,7 @@ def load_llm_model():
542
  return None, None
543
 
544
  # Analyze image function
545
- def analyze_image_with_llm(image, gradcam_overlay, face_box, pred_label, confidence, question, model, tokenizer, custom_instruction=""):
546
- # Use fixed values for temperature and max_tokens
547
- temperature = 0.7 # Fixed temperature value
548
- max_tokens = 500 # Fixed max tokens value
549
-
550
  # Create a prompt that includes GradCAM information
551
  if custom_instruction.strip():
552
  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}"
@@ -761,4 +757,150 @@ def main():
761
  st.session_state.current_pred_label = pred_label
762
  st.session_state.current_confidence = confidence
763
 
764
- st.success("βœ… Initial detection and GradCAM visualization complete!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  # App title and description
22
  st.set_page_config(
23
+ page_title="Deepfake Analyzer",
24
  layout="wide",
25
  page_icon="πŸ”"
26
  )
 
42
  # Sidebar components
43
  st.sidebar.title("Options")
44
 
45
+ # Fixed values for temperature and max tokens
46
+ temperature = 0.7
47
+ max_tokens = 500
48
 
49
  # Custom instruction text area in sidebar
50
  custom_instruction = st.sidebar.text_area(
 
542
  return None, None
543
 
544
  # Analyze image function
545
+ def analyze_image_with_llm(image, gradcam_overlay, face_box, pred_label, confidence, question, model, tokenizer, temperature=0.7, max_tokens=500, custom_instruction=""):
 
 
 
 
546
  # Create a prompt that includes GradCAM information
547
  if custom_instruction.strip():
548
  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}"
 
757
  st.session_state.current_pred_label = pred_label
758
  st.session_state.current_confidence = confidence
759
 
760
+ st.success("βœ… Initial detection and GradCAM visualization complete!")
761
+ else:
762
+ st.warning("⚠️ Please load the CLIP model first to perform initial detection.")
763
+ except Exception as e:
764
+ st.error(f"Error processing image: {str(e)}")
765
+ import traceback
766
+ st.error(traceback.format_exc()) # This will show the full error traceback
767
+
768
+ # Image Analysis Summary section - AFTER Stage 2
769
+ if hasattr(st.session_state, 'current_image') and (hasattr(st.session_state, 'image_caption') or hasattr(st.session_state, 'gradcam_caption')):
770
+ with st.expander("Image Analysis Summary", expanded=True):
771
+ st.subheader("Generated Descriptions and Analysis")
772
+
773
+ # Display image, captions, and results in organized layout with proper formatting
774
+ col1, col2 = st.columns([1, 2])
775
+
776
+ with col1:
777
+ # Display original image and overlay side by side with controlled size
778
+ st.image(st.session_state.current_image, caption="Original Image", width=300)
779
+ if hasattr(st.session_state, 'current_overlay'):
780
+ st.image(st.session_state.current_overlay, caption="GradCAM Overlay", width=300)
781
+
782
+ with col2:
783
+ # Detection result
784
+ if hasattr(st.session_state, 'current_pred_label'):
785
+ st.markdown("### Detection Result")
786
+ st.markdown(f"**Classification:** {st.session_state.current_pred_label} (Confidence: {st.session_state.current_confidence:.2%})")
787
+ st.markdown("---")
788
+
789
+ # Image description
790
+ if hasattr(st.session_state, 'image_caption'):
791
+ st.markdown("### Image Description")
792
+ st.markdown(st.session_state.image_caption)
793
+ st.markdown("---")
794
+
795
+ # GradCAM analysis
796
+ if hasattr(st.session_state, 'gradcam_caption'):
797
+ st.markdown("### GradCAM Analysis")
798
+ st.markdown(st.session_state.gradcam_caption)
799
+
800
+ # LLM Analysis section - AFTER Image Analysis Summary
801
+ with st.expander("Stage 3: Detailed Analysis with Vision LLM", expanded=False):
802
+ if hasattr(st.session_state, 'current_image') and st.session_state.llm_model_loaded:
803
+ st.subheader("Detailed Deepfake Analysis")
804
+
805
+ # Display chat history
806
+ for i, (question, answer) in enumerate(st.session_state.chat_history):
807
+ st.markdown(f"**Question {i+1}:** {question}")
808
+ st.markdown(f"**Answer:** {answer}")
809
+ st.markdown("---")
810
+
811
+ # Include both captions in the prompt if available
812
+ caption_text = ""
813
+ if hasattr(st.session_state, 'image_caption'):
814
+ caption_text += f"\n\nImage Description:\n{st.session_state.image_caption}"
815
+
816
+ if hasattr(st.session_state, 'gradcam_caption'):
817
+ caption_text += f"\n\nGradCAM Analysis:\n{st.session_state.gradcam_caption}"
818
+
819
+ # Default question with option to customize
820
+ 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."
821
+
822
+ # User input for new question
823
+ new_question = st.text_area("Ask a question about the image:", value=default_question if not st.session_state.chat_history else "", height=100)
824
+
825
+ # Analyze button and Clear Chat button in the same row
826
+ col1, col2 = st.columns([3, 1])
827
+ with col1:
828
+ analyze_button = st.button("πŸ” Send Question", type="primary")
829
+ with col2:
830
+ clear_button = st.button("πŸ—‘οΈ Clear Chat History")
831
+
832
+ if clear_button:
833
+ st.session_state.chat_history = []
834
+ st.experimental_rerun()
835
+
836
+ if analyze_button and new_question:
837
+ try:
838
+ # Add caption info if it's the first question
839
+ if not st.session_state.chat_history:
840
+ full_question = new_question + caption_text
841
+ else:
842
+ full_question = new_question
843
+
844
+ result = analyze_image_with_llm(
845
+ st.session_state.current_image,
846
+ st.session_state.current_overlay,
847
+ st.session_state.current_face_box,
848
+ st.session_state.current_pred_label,
849
+ st.session_state.current_confidence,
850
+ full_question,
851
+ st.session_state.llm_model,
852
+ st.session_state.tokenizer,
853
+ temperature=temperature,
854
+ max_tokens=max_tokens,
855
+ custom_instruction=custom_instruction
856
+ )
857
+
858
+ # Add to chat history
859
+ st.session_state.chat_history.append((new_question, result))
860
+
861
+ # Display the latest result too
862
+ st.success("βœ… Analysis complete!")
863
+
864
+ # Check if the result contains both technical and non-technical explanations
865
+ if "Technical" in result and "Non-Technical" in result:
866
+ try:
867
+ # Split the result into technical and non-technical sections
868
+ parts = result.split("Non-Technical")
869
+ technical = parts[0]
870
+ non_technical = "Non-Technical" + parts[1]
871
+
872
+ # Display in two columns
873
+ tech_col, simple_col = st.columns(2)
874
+ with tech_col:
875
+ st.subheader("Technical Analysis")
876
+ st.markdown(technical)
877
+
878
+ with simple_col:
879
+ st.subheader("Simple Explanation")
880
+ st.markdown(non_technical)
881
+ except Exception as e:
882
+ # Fallback if splitting fails
883
+ st.subheader("Analysis Result")
884
+ st.markdown(result)
885
+ else:
886
+ # Just display the whole result
887
+ st.subheader("Analysis Result")
888
+ st.markdown(result)
889
+
890
+ # Rerun to update the chat history display
891
+ st.experimental_rerun()
892
+
893
+ except Exception as e:
894
+ st.error(f"Error during LLM analysis: {str(e)}")
895
+
896
+ elif not hasattr(st.session_state, 'current_image'):
897
+ st.warning("⚠️ Please upload an image and complete the initial detection first.")
898
+ else:
899
+ st.warning("⚠️ Please load the Vision LLM to perform detailed analysis.")
900
+
901
+ # Footer
902
+ st.markdown("---")
903
+ st.caption("Advanced Deepfake Image Analyzer with Structured BLIP Captioning")
904
+
905
+ if __name__ == "__main__":
906
+ main()