import os import sys from pathlib import Path import gradio as gr from utils.data_processing import detect_nsfw # YOLO-related module path setup FILE = Path(__file__).resolve() ROOT = FILE.parents[0] if str(ROOT) not in sys.path: sys.path.append(str(ROOT)) ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # Define the Gradio interface with gr.Blocks() as demo: gr.Markdown("# NSFW Content Detection - Detailed Analysis") # Advanced parameters for Detailed Analysis with gr.Row(): conf_threshold = gr.Slider(0, 1, value=0.2, label="Confidence Threshold") iou_threshold = gr.Slider(0, 1, value=0.45, label="Overlap Threshold") label_mode = gr.Dropdown( ["Draw box", "Draw Label", "Draw Confidence", "Censor Predictions"], label="Label Display Mode", value="Draw box", ) # Input and output components with gr.Row(): input_image = gr.Image(type="numpy", label="Upload an image or enter a URL") output_text = gr.Textbox(label="Detection Result") with gr.Row(): output_image = gr.Image(type="numpy", label="Processed Image (for detailed analysis)") # Detection button detect_button = gr.Button("Detect") # Connect detection button to the detect_nsfw function def safe_detect_nsfw(image, conf, iou, label): try: return detect_nsfw(image, conf, iou, label) except Exception as e: return f"Error during detection: {e}", None detect_button.click( safe_detect_nsfw, inputs=[input_image, conf_threshold, iou_threshold, label_mode], outputs=[output_text, output_image], ) # Launch the Gradio app if __name__ == "__main__": demo.launch(server_name="0.0.0.0")