Spaces:
Sleeping
Sleeping
Update app.py (#5)
Browse files- Update app.py (398d0f3806ee4477cce3353e687cde80196d3def)
3 unmodified lines4567892 unmodified lines121314151617181913 unmodified lines333435363738394041423 unmodified linesfrom transformers import pipeline, Pipelineimport osfrom dotenv import load_dotenv# --- Configuration ---load_dotenv()2 unmodified lines# --- AI Model Setup ---print("Loading AI Image Detection model...")# FIX 1: Use the correct pipeline for this task.# 'zero-shot-image-classification' is designed to work with 'candidate_labels'.image_detector: Pipeline = pipeline("zero-shot-image-classification", model="openai/clip-vit-base-patch32")print("✅ Model loaded successfully.")13 unmodified linesprint(f"Analyzing image from URL: {image_url}")try:# FIX 2: Add a User-Agent header to mimic a real browser request.# This prevents websites (including Puch's image hosting) from blocking the download.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'}image = Image.open(requests.get(image_url, stream=True, timeout=10, headers=headers).raw)except Exception as e:raise gr.Error(f"Could not load image from URL. Error: {str(e)}")3 unmodified lines456789102 unmodified lines13141516171813 unmodified lines323334353637383940414243444546473 unmodified linesfrom transformers import pipeline, Pipelineimport osfrom dotenv import load_dotenvimport io # <-- NEW: Import the 'io' library# --- Configuration ---load_dotenv()2 unmodified lines# --- AI Model Setup ---print("Loading AI Image Detection model...")image_detector: Pipeline = pipeline("zero-shot-image-classification", model="openai/clip-vit-base-patch32")print("✅ Model loaded successfully.")13 unmodified linesprint(f"Analyzing image from URL: {image_url}")try:# --- MODIFIED IMAGE DOWNLOAD BLOCK ---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'}# 1. Let requests handle the full download and any redirectsresponse = requests.get(image_url, timeout=15, headers=headers)response.raise_for_status() # Raise an exception for bad status codes (like 404 or 500)# 2. Open the image from the downloaded content in memoryimage = Image.open(io.BytesIO(response.content))# --- END OF MODIFIED BLOCK ---except Exception as e:raise gr.Error(f"Could not load image from URL. Error: {str(e)}")
app.py+10-5