Files changed (1) hide show
  1. app.py +10 -5
app.py CHANGED
@@ -4,6 +4,7 @@ from PIL import Image
4
  from transformers import pipeline, Pipeline
5
  import os
6
  from dotenv import load_dotenv
 
7
 
8
  # --- Configuration ---
9
  load_dotenv()
@@ -12,8 +13,6 @@ OWNER_PHONE_NUMBER = os.getenv("OWNER_PHONE_NUMBER")
12
 
13
  # --- AI Model Setup ---
14
  print("Loading AI Image Detection model...")
15
- # FIX 1: Use the correct pipeline for this task.
16
- # 'zero-shot-image-classification' is designed to work with 'candidate_labels'.
17
  image_detector: Pipeline = pipeline("zero-shot-image-classification", model="openai/clip-vit-base-patch32")
18
  print("✅ Model loaded successfully.")
19
 
@@ -33,10 +32,16 @@ def analyze_image_authenticity(image_url: str) -> dict:
33
 
34
  print(f"Analyzing image from URL: {image_url}")
35
  try:
36
- # FIX 2: Add a User-Agent header to mimic a real browser request.
37
- # This prevents websites (including Puch's image hosting) from blocking the download.
38
  headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
39
- image = Image.open(requests.get(image_url, stream=True, timeout=10, headers=headers).raw)
 
 
 
 
 
 
 
40
  except Exception as e:
41
  raise gr.Error(f"Could not load image from URL. Error: {str(e)}")
42
 
 
4
  from transformers import pipeline, Pipeline
5
  import os
6
  from dotenv import load_dotenv
7
+ import io # <-- NEW: Import the 'io' library
8
 
9
  # --- Configuration ---
10
  load_dotenv()
 
13
 
14
  # --- AI Model Setup ---
15
  print("Loading AI Image Detection model...")
 
 
16
  image_detector: Pipeline = pipeline("zero-shot-image-classification", model="openai/clip-vit-base-patch32")
17
  print("✅ Model loaded successfully.")
18
 
 
32
 
33
  print(f"Analyzing image from URL: {image_url}")
34
  try:
35
+ # --- MODIFIED IMAGE DOWNLOAD BLOCK ---
 
36
  headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
37
+
38
+ # 1. Let requests handle the full download and any redirects
39
+ response = requests.get(image_url, timeout=15, headers=headers)
40
+ response.raise_for_status() # Raise an exception for bad status codes (like 404 or 500)
41
+
42
+ # 2. Open the image from the downloaded content in memory
43
+ image = Image.open(io.BytesIO(response.content))
44
+ # --- END OF MODIFIED BLOCK ---
45
  except Exception as e:
46
  raise gr.Error(f"Could not load image from URL. Error: {str(e)}")
47