Axe-08 commited on
Commit
fb631e2
·
verified ·
1 Parent(s): abc3d93

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -13
app.py CHANGED
@@ -6,18 +6,17 @@ 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.
@@ -35,8 +34,7 @@ def analyze_image_authenticity(image_url: str) -> dict:
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)
@@ -44,16 +42,39 @@ def analyze_image_authenticity(image_url: str) -> dict:
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)
 
6
  from dotenv import load_dotenv
7
 
8
  # --- Configuration ---
 
9
  load_dotenv()
10
  VALID_BEARER_TOKEN = os.getenv("VALID_BEARER_TOKEN")
11
  OWNER_PHONE_NUMBER = os.getenv("OWNER_PHONE_NUMBER")
12
 
13
  # --- AI Model Setup ---
 
14
  print("Loading AI Image Detection model...")
15
  image_detector: Pipeline = pipeline("image-classification", model="openai/clip-vit-base-patch32")
16
  print("✅ Model loaded successfully.")
17
 
18
+
19
+ # --- Tool 1: The Main Analysis Function ---
20
  def analyze_image_authenticity(image_url: str) -> dict:
21
  """
22
  Analyzes an image from a URL to determine if it is real or AI-generated.
 
34
  try:
35
  image = Image.open(requests.get(image_url, stream=True, timeout=10).raw)
36
  except Exception as e:
37
+ raise gr.Error(f"Could not load image from URL. Error: {str(e)}")
 
38
 
39
  labels = ["a real photograph", "a computer-generated image", "an illustration or drawing"]
40
  results = image_detector(image, candidate_labels=labels)
 
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.
50
+
51
+ Returns:
52
+ The owner's phone number as a string.
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"]
77
  )
78
 
79
  # --- Launch the App and MCP Server ---
 
80
  demo.launch(mcp_server=True)