vineet124jig commited on
Commit
b8e6ffe
·
verified ·
1 Parent(s): 6bf89db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -13
app.py CHANGED
@@ -3,7 +3,6 @@ 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")
@@ -11,7 +10,7 @@ headers = {
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(
@@ -24,7 +23,7 @@ def validate_nsfw(url):
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)
@@ -34,17 +33,17 @@ def validate_nsfw(url):
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("""
@@ -53,13 +52,14 @@ with gr.Blocks() as demo:
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
 
@@ -71,8 +71,14 @@ with gr.Blocks() as demo:
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()
 
3
  import json
4
  import os
5
 
 
6
  BASE_URL = "https://api.jigsawstack.com/v1"
7
  headers = {
8
  "x-api-key": os.getenv("JIGSAWSTACK_API_KEY")
 
10
 
11
  def validate_nsfw(url):
12
  if not url or not url.strip():
13
+ return None, "Error: Image URL is required.", None, None
14
 
15
  try:
16
  response = requests.post(
 
23
 
24
  if not result.get("success"):
25
  error_msg = f"Error: API call failed - {result.get('message', 'Unknown error')}"
26
+ return url, error_msg, None, None
27
 
28
  # Extract detailed information
29
  nsfw = result.get("nsfw", False)
 
33
  nudity_score = result.get("nudity_score", 0)
34
  gore_score = result.get("gore_score", 0)
35
 
36
+ # Format the output
37
  status_message = f"Overall NSFW: {'Yes' if nsfw else 'No'}"
38
  nudity_details = f"Nudity: {'Detected' if nudity else 'Not Detected'} (Score: {nudity_score:.4f})"
39
  gore_details = f"Gore: {'Detected' if gore else 'Not Detected'} (Score: {gore_score:.4f})"
40
+
41
  return url, status_message, nudity_details, gore_details
42
 
43
  except requests.exceptions.RequestException as e:
44
+ return url, f"Request failed: {str(e)}", None, None
45
  except Exception as e:
46
+ return url, f"An unexpected error occurred: {str(e)}", None, None
47
 
48
  with gr.Blocks() as demo:
49
  gr.Markdown("""
 
52
  <p style='font-size:1.2em; margin-top: 0;'>Quickly detect nudity, violence, and other NSFW content in images.</p>
53
  <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>
54
  </div>
55
+ """)
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
 
 
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=[
77
+ nsfw_image_display,
78
+ nsfw_status_message,
79
+ nsfw_nudity_details,
80
+ nsfw_gore_details
81
+ ]
82
+ )
83
+
84
  demo.launch()