Spaces:
Sleeping
Sleeping
Update app.py (#3)
Browse files- Update app.py (fb631e2a9613bae61a9b55a4a3764fd0e61194c1)
5 unmodified lines6789101112131415161718192021222311 unmodified lines35363738394041421 unmodified line444546474849505152535455565758595 unmodified linesfrom dotenv import load_dotenv# --- Configuration ---# Load secrets from the Space's "Repository secrets" settingsload_dotenv()VALID_BEARER_TOKEN = os.getenv("VALID_BEARER_TOKEN")OWNER_PHONE_NUMBER = os.getenv("OWNER_PHONE_NUMBER")# --- AI Model Setup ---# This is loaded once when the Space startsprint("Loading AI Image Detection model...")image_detector: Pipeline = pipeline("image-classification", model="openai/clip-vit-base-patch32")print("✅ Model loaded successfully.")# --- Main Tool Function ---def analyze_image_authenticity(image_url: str) -> dict:"""Analyzes an image from a URL to determine if it is real or AI-generated.11 unmodified linestry:image = Image.open(requests.get(image_url, stream=True, timeout=10).raw)except Exception as e:# For Gradio, it's better to raise a gr.Error for user-facing issuesraise gr.Error(f"Could not load image from URL. It might be invalid or inaccessible. Error: {str(e)}")labels = ["a real photograph", "a computer-generated image", "an illustration or drawing"]results = image_detector(image, candidate_labels=labels)1 unmodified lineprint(f"Analysis successful. Results: {results}")return {"analysis_results": results}# --- Gradio Interface ---# This defines the UI and the MCP endpointdemo = gr.Interface(fn=analyze_image_authenticity,inputs=[gr.Textbox(label="Image URL")],outputs=[gr.JSON(label="Analysis Results")],title="AI Image Authenticity Detector",description="Provide an image URL to determine if it is a real photograph or AI-generated.")# --- Launch the App and MCP Server ---# mcp_server=True is the magic parameter that exposes your function as an MCP tooldemo.launch(mcp_server=True)5 unmodified lines67891011121314151617181920212211 unmodified lines343536373839401 unmodified line4243444546474849505152535455565758596061626364656667686970717273747576777879805 unmodified linesfrom dotenv import load_dotenv# --- Configuration ---load_dotenv()VALID_BEARER_TOKEN = os.getenv("VALID_BEARER_TOKEN")OWNER_PHONE_NUMBER = os.getenv("OWNER_PHONE_NUMBER")# --- 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:"""Analyzes an image from a URL to determine if it is real or AI-generated.11 unmodified linestry: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)}")labels = ["a real photograph", "a computer-generated image", "an illustration or drawing"]results = image_detector(image, candidate_labels=labels)1 unmodified lineprint(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.Returns:The owner's phone number as a string."""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"])# --- Launch the App and MCP Server ---demo.launch(mcp_server=True)
app.py+34-13