Spaces:
Sleeping
Sleeping
Update app.py
#4
by
Axe-08 - opened
app.py
CHANGED
|
@@ -12,10 +12,11 @@ OWNER_PHONE_NUMBER = os.getenv("OWNER_PHONE_NUMBER")
|
|
| 12 |
|
| 13 |
# --- AI Model Setup ---
|
| 14 |
print("Loading AI Image Detection model...")
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
print("✅ Model loaded successfully.")
|
| 17 |
|
| 18 |
-
|
| 19 |
# --- Tool 1: The Main Analysis Function ---
|
| 20 |
def analyze_image_authenticity(image_url: str) -> dict:
|
| 21 |
"""
|
|
@@ -32,7 +33,10 @@ def analyze_image_authenticity(image_url: str) -> dict:
|
|
| 32 |
|
| 33 |
print(f"Analyzing image from URL: {image_url}")
|
| 34 |
try:
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
except Exception as e:
|
| 37 |
raise gr.Error(f"Could not load image from URL. Error: {str(e)}")
|
| 38 |
|
|
@@ -42,8 +46,7 @@ def analyze_image_authenticity(image_url: str) -> dict:
|
|
| 42 |
print(f"Analysis successful. Results: {results}")
|
| 43 |
return {"analysis_results": results}
|
| 44 |
|
| 45 |
-
|
| 46 |
-
# --- Tool 2: The Validation Function for Sharing (NEW) ---
|
| 47 |
def validate() -> str:
|
| 48 |
"""
|
| 49 |
Validates the server ownership for sharing. Returns the owner's phone number.
|
|
@@ -53,24 +56,20 @@ def validate() -> str:
|
|
| 53 |
"""
|
| 54 |
return OWNER_PHONE_NUMBER
|
| 55 |
|
| 56 |
-
|
| 57 |
# --- Gradio Interface ---
|
| 58 |
-
# We now create a list of Interfaces to expose both tools
|
| 59 |
demo = gr.TabbedInterface(
|
| 60 |
[
|
| 61 |
gr.Interface(
|
| 62 |
fn=analyze_image_authenticity,
|
| 63 |
inputs=[gr.Textbox(label="Image URL")],
|
| 64 |
outputs=[gr.JSON(label="Analysis Results")],
|
| 65 |
-
title="AI Image Authenticity Detector"
|
| 66 |
-
description="Tool to analyze an image's authenticity."
|
| 67 |
),
|
| 68 |
gr.Interface(
|
| 69 |
fn=validate,
|
| 70 |
inputs=[],
|
| 71 |
outputs="text",
|
| 72 |
-
title="Validation Tool"
|
| 73 |
-
description="Used by the Puch AI platform to validate shared servers."
|
| 74 |
)
|
| 75 |
],
|
| 76 |
["Image Analyzer", "Validator"]
|
|
|
|
| 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 |
|
|
|
|
| 20 |
# --- Tool 1: The Main Analysis Function ---
|
| 21 |
def analyze_image_authenticity(image_url: str) -> dict:
|
| 22 |
"""
|
|
|
|
| 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 |
|
|
|
|
| 46 |
print(f"Analysis successful. Results: {results}")
|
| 47 |
return {"analysis_results": results}
|
| 48 |
|
| 49 |
+
# --- Tool 2: The Validation Function for Sharing ---
|
|
|
|
| 50 |
def validate() -> str:
|
| 51 |
"""
|
| 52 |
Validates the server ownership for sharing. Returns the owner's phone number.
|
|
|
|
| 56 |
"""
|
| 57 |
return OWNER_PHONE_NUMBER
|
| 58 |
|
|
|
|
| 59 |
# --- Gradio Interface ---
|
|
|
|
| 60 |
demo = gr.TabbedInterface(
|
| 61 |
[
|
| 62 |
gr.Interface(
|
| 63 |
fn=analyze_image_authenticity,
|
| 64 |
inputs=[gr.Textbox(label="Image URL")],
|
| 65 |
outputs=[gr.JSON(label="Analysis Results")],
|
| 66 |
+
title="AI Image Authenticity Detector"
|
|
|
|
| 67 |
),
|
| 68 |
gr.Interface(
|
| 69 |
fn=validate,
|
| 70 |
inputs=[],
|
| 71 |
outputs="text",
|
| 72 |
+
title="Validation Tool"
|
|
|
|
| 73 |
)
|
| 74 |
],
|
| 75 |
["Image Analyzer", "Validator"]
|