from transformers import pipeline from transformers.utils import logging from helper import load_image_from_url, render_results_in_image from PIL import Image import gradio as gr import os # Reduce the verbosity of transformers and suppress warnings logging.set_verbosity_error() # Load the object detection pipeline od_pipe = pipeline("object-detection", "facebook/detr-resnet-50") def get_pipeline_prediction(pil_image): # Perform object detection pipeline_output = od_pipe(pil_image) # Render results on the image processed_image = render_results_in_image(pil_image, pipeline_output) return processed_image # Custom CSS to improve the interface css = """ body { font-family: Arial, sans-serif; } button { background-color: #4CAF50; color: white; border: none; padding: 10px 20px; } """ # Set up the Gradio Blocks interface with gr.Blocks(css=css) as demo: gr.Markdown("## Object Detection Service") gr.Markdown("Upload an image and see the object detection results rendered on the image.") with gr.Row(): image_input = gr.Image(label="Upload Image", type="pil", tool="editor") submit_button = gr.Button("Detect Objects") output_image = gr.Image(label="Detected Objects") submit_button.click( get_pipeline_prediction, inputs=[image_input], outputs=[output_image] ) # Launch the Gradio app demo.launch(share=True, server_port=int(os.environ.get('PORT1', 7860))) # Default port 7860 if PORT1 is not set