Spaces:
Sleeping
Sleeping
Update app.py
Browse files11 unmodified lines1213141516171819202110 unmodified lines323334353637383 unmodified lines42434445464748493 unmodified lines53545556575859606162636465666768697071727374757611 unmodified lines# --- AI Model Setup ---print("Loading AI Image Detection model...")image_detector: Pipeline = pipeline("image-classification", model="openai/clip-vit-base-patch32")print("✅ Model loaded successfully.")# --- Tool 1: The Main Analysis Function ---def analyze_image_authenticity(image_url: str) -> dict:"""10 unmodified linesprint(f"Analyzing image from URL: {image_url}")try:image = Image.open(requests.get(image_url, stream=True, timeout=10).raw)except Exception as e:raise gr.Error(f"Could not load image from URL. Error: {str(e)}")3 unmodified linesprint(f"Analysis successful. Results: {results}")return {"analysis_results": results}# --- Tool 2: The Validation Function for Sharing (NEW) ---def validate() -> str:"""Validates the server ownership for sharing. Returns the owner's phone number.3 unmodified lines"""return OWNER_PHONE_NUMBER# --- Gradio Interface ---# We now create a list of Interfaces to expose both toolsdemo = gr.TabbedInterface([gr.Interface(fn=analyze_image_authenticity,inputs=[gr.Textbox(label="Image URL")],outputs=[gr.JSON(label="Analysis Results")],title="AI Image Authenticity Detector",description="Tool to analyze an image's authenticity."),gr.Interface(fn=validate,inputs=[],outputs="text",title="Validation Tool",description="Used by the Puch AI platform to validate shared servers.")],["Image Analyzer", "Validator"]11 unmodified lines121314151617181920212210 unmodified lines333435363738394041423 unmodified lines464748495051523 unmodified lines565758596061626364656667686970717273747511 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.")# --- Tool 1: The Main Analysis Function ---def analyze_image_authenticity(image_url: str) -> dict:"""10 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 linesprint(f"Analysis successful. Results: {results}")return {"analysis_results": results}# --- Tool 2: The Validation Function for Sharing ---def validate() -> str:"""Validates the server ownership for sharing. Returns the owner's phone number.3 unmodified lines"""return OWNER_PHONE_NUMBER# --- Gradio Interface ---demo = gr.TabbedInterface([gr.Interface(fn=analyze_image_authenticity,inputs=[gr.Textbox(label="Image URL")],outputs=[gr.JSON(label="Analysis Results")],title="AI Image Authenticity Detector"),gr.Interface(fn=validate,inputs=[],outputs="text",title="Validation Tool")],["Image Analyzer", "Validator"]
app.py+10-11