import gradio as gr from PIL import Image import numpy as np import requests # For API calls # Define your API endpoint and key API_ENDPOINT = "https://your-api-endpoint.com/predict" # Replace with your actual API endpoint API_KEY = "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" # Replace with your API key def detect_weapons(image): """ Sends the uploaded image to an API for weapon detection. """ try: # Convert the PIL Image to bytes for API upload image_bytes = np.array(image).tobytes() # Prepare headers and payload headers = {"Authorization": f"Bearer {API_KEY}"} files = {"image": image_bytes} # Send the image to the API response = requests.post(API_ENDPOINT, headers=headers, files=files) if response.status_code == 200: # Parse the API response results = response.json() # Assuming the API returns JSON return f"Detected Weapons: {results}" else: return f"API Error: {response.status_code} - {response.text}" except Exception as e: return f"Error during detection: {e}" def process_image(image): """ Function to process the uploaded image for weapon detection. """ results = detect_weapons(image) return results # Create the Gradio Interface interface = gr.Interface( fn=process_image, inputs=gr.Image(type="pil", label="Upload an Image"), outputs=gr.Textbox(label="Detection Results"), title="Weapon Detection App", description="Upload an image to detect weapons like guns or bombs." ) # Launch the Gradio app interface.launch(server_name="0.0.0.0", server_port=7860)