Spaces:
Sleeping
Sleeping
Upload 2 files
#1
by Axe-08 - opened
Files changed (2)
- app.pyAdded+59
- requirements.txtAdded+7
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859import gradio as grimport requestsfrom PIL import Imagefrom transformers import pipeline, Pipelineimport osfrom 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. Args: image_url: The URL of the image to analyze. Returns: A dictionary with the analysis results and probability scores. """ if not image_url: raise gr.Error("Image URL parameter is missing.") print(f"Analyzing image from URL: {image_url}") try: 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 issues raise 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) print(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)1234567gradio[mcp]requestsPillowtorchtorchvisiontransformerspython-dotenvapp.pyAdded+59
requirements.txtAdded+7