Spaces:
Running
Running
Upload 2 files
Browse files- app.py +78 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
BASE_URL = "https://api.jigsawstack.com/v1"
|
| 8 |
+
headers = {
|
| 9 |
+
"x-api-key": os.getenv("JIGSAWSTACK_API_KEY")
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
def validate_nsfw(url):
|
| 13 |
+
if not url or not url.strip():
|
| 14 |
+
return None, "Error: Image URL is required.", None, None, None
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
response = requests.post(
|
| 18 |
+
f"{BASE_URL}/validate/nsfw",
|
| 19 |
+
headers=headers,
|
| 20 |
+
json={"url": url.strip()}
|
| 21 |
+
)
|
| 22 |
+
response.raise_for_status()
|
| 23 |
+
result = response.json()
|
| 24 |
+
|
| 25 |
+
if not result.get("success"):
|
| 26 |
+
error_msg = f"Error: API call failed - {result.get('message', 'Unknown error')}"
|
| 27 |
+
return url, error_msg, None, None, None
|
| 28 |
+
|
| 29 |
+
# Extract detailed information
|
| 30 |
+
nsfw = result.get("nsfw", False)
|
| 31 |
+
nudity = result.get("nudity", False)
|
| 32 |
+
gore = result.get("gore", False)
|
| 33 |
+
nsfw_score = result.get("nsfw_score", 0)
|
| 34 |
+
nudity_score = result.get("nudity_score", 0)
|
| 35 |
+
gore_score = result.get("gore_score", 0)
|
| 36 |
+
|
| 37 |
+
# Format the output for better readability
|
| 38 |
+
status_message = f"Overall NSFW: {'Yes' if nsfw else 'No'}"
|
| 39 |
+
nudity_details = f"Nudity: {'Detected' if nudity else 'Not Detected'} (Score: {nudity_score:.4f})"
|
| 40 |
+
gore_details = f"Gore: {'Detected' if gore else 'Not Detected'} (Score: {gore_score:.4f})"
|
| 41 |
+
|
| 42 |
+
return url, status_message, nudity_details, gore_details
|
| 43 |
+
|
| 44 |
+
except requests.exceptions.RequestException as e:
|
| 45 |
+
return url, f"Request failed: {str(e)}", None, None, None
|
| 46 |
+
except Exception as e:
|
| 47 |
+
return url, f"An unexpected error occurred: {str(e)}", None, None, None
|
| 48 |
+
|
| 49 |
+
with gr.Blocks() as demo:
|
| 50 |
+
gr.Markdown("""
|
| 51 |
+
<div style='text-align: center; margin-bottom: 24px;'>
|
| 52 |
+
<h1 style='font-size:2.2em; margin-bottom: 0.2em;'>🧩 NSFW Detection</h1>
|
| 53 |
+
<p style='font-size:1.2em; margin-top: 0;'>Quickly detect nudity, violence, and other NSFW content in images.</p>
|
| 54 |
+
<p style='font-size:1em; margin-top: 0.5em;'>For more details and API usage, see the <a href='https://jigsawstack.com/docs/api-reference/validate/nsfw' target='_blank'>documentation</a>.</p>
|
| 55 |
+
</div>
|
| 56 |
+
""")
|
| 57 |
+
with gr.Row():
|
| 58 |
+
with gr.Column():
|
| 59 |
+
gr.Markdown("#### Image to Analyze")
|
| 60 |
+
nsfw_url = gr.Textbox(
|
| 61 |
+
label="Image URL",
|
| 62 |
+
placeholder="Enter the URL of the image you want to validate..."
|
| 63 |
+
)
|
| 64 |
+
nsfw_btn = gr.Button("Validate Image", variant="primary")
|
| 65 |
+
|
| 66 |
+
with gr.Column():
|
| 67 |
+
gr.Markdown("#### Validation Result")
|
| 68 |
+
nsfw_image_display = gr.Image(label="Image Preview")
|
| 69 |
+
nsfw_status_message = gr.Textbox(label="Status", interactive=False)
|
| 70 |
+
nsfw_nudity_details = gr.Textbox(label="Nudity Details", interactive=False)
|
| 71 |
+
nsfw_gore_details = gr.Textbox(label="Gore Details", interactive=False)
|
| 72 |
+
|
| 73 |
+
nsfw_btn.click(
|
| 74 |
+
validate_nsfw,
|
| 75 |
+
inputs=[nsfw_url],
|
| 76 |
+
outputs=[nsfw_image_display, nsfw_status_message, nsfw_nudity_details, nsfw_gore_details]
|
| 77 |
+
)
|
| 78 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
requests
|
| 3 |
+
Pillow
|