Files changed (2) hide show
  1. app.py +59 -0
  2. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ from PIL import Image
4
+ from transformers import pipeline, Pipeline
5
+ import os
6
+ from dotenv import load_dotenv
7
+
8
+ # --- Configuration ---
9
+ # Load secrets from the Space's "Repository secrets" settings
10
+ load_dotenv()
11
+ VALID_BEARER_TOKEN = os.getenv("VALID_BEARER_TOKEN")
12
+ OWNER_PHONE_NUMBER = os.getenv("OWNER_PHONE_NUMBER")
13
+
14
+ # --- AI Model Setup ---
15
+ # This is loaded once when the Space starts
16
+ print("Loading AI Image Detection model...")
17
+ image_detector: Pipeline = pipeline("image-classification", model="openai/clip-vit-base-patch32")
18
+ print("✅ Model loaded successfully.")
19
+
20
+ # --- Main Tool Function ---
21
+ def analyze_image_authenticity(image_url: str) -> dict:
22
+ """
23
+ Analyzes an image from a URL to determine if it is real or AI-generated.
24
+
25
+ Args:
26
+ image_url: The URL of the image to analyze.
27
+
28
+ Returns:
29
+ A dictionary with the analysis results and probability scores.
30
+ """
31
+ if not image_url:
32
+ raise gr.Error("Image URL parameter is missing.")
33
+
34
+ print(f"Analyzing image from URL: {image_url}")
35
+ try:
36
+ image = Image.open(requests.get(image_url, stream=True, timeout=10).raw)
37
+ except Exception as e:
38
+ # For Gradio, it's better to raise a gr.Error for user-facing issues
39
+ raise gr.Error(f"Could not load image from URL. It might be invalid or inaccessible. Error: {str(e)}")
40
+
41
+ labels = ["a real photograph", "a computer-generated image", "an illustration or drawing"]
42
+ results = image_detector(image, candidate_labels=labels)
43
+
44
+ print(f"Analysis successful. Results: {results}")
45
+ return {"analysis_results": results}
46
+
47
+ # --- Gradio Interface ---
48
+ # This defines the UI and the MCP endpoint
49
+ demo = gr.Interface(
50
+ fn=analyze_image_authenticity,
51
+ inputs=[gr.Textbox(label="Image URL")],
52
+ outputs=[gr.JSON(label="Analysis Results")],
53
+ title="AI Image Authenticity Detector",
54
+ description="Provide an image URL to determine if it is a real photograph or AI-generated."
55
+ )
56
+
57
+ # --- Launch the App and MCP Server ---
58
+ # mcp_server=True is the magic parameter that exposes your function as an MCP tool
59
+ demo.launch(mcp_server=True)
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ gradio[mcp]
2
+ requests
3
+ Pillow
4
+ torch
5
+ torchvision
6
+ transformers
7
+ python-dotenv