Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,18 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from ultralytics import YOLO
|
| 3 |
-
import
|
| 4 |
|
| 5 |
-
# Load pre-trained YOLOv8
|
| 6 |
-
|
| 7 |
-
# docseg_model2 = YOLO("path/to/your/second/model.pt") # Replace with your second model's path
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
# "Your Second Model": docseg_model2 # Uncomment and add more as needed
|
| 13 |
-
}
|
| 14 |
-
|
| 15 |
-
def process_image(image, model_name):
|
| 16 |
try:
|
| 17 |
-
# Select the model
|
| 18 |
-
model = MODELS[model_name]
|
| 19 |
-
|
| 20 |
# Process the image
|
| 21 |
results = model(source=image, save=False, show_labels=True, show_conf=True, show_boxes=True)
|
| 22 |
result = results[0]
|
| 23 |
-
|
| 24 |
# Extract the annotated image and the labels/confidence scores
|
| 25 |
annotated_image = result.plot()
|
| 26 |
detected_areas_labels = "\n".join(
|
|
@@ -30,17 +22,12 @@ def process_image(image, model_name):
|
|
| 30 |
return annotated_image, detected_areas_labels
|
| 31 |
except Exception as e:
|
| 32 |
return None, f"Error processing image: {e}"
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
| 36 |
# Create the Gradio Interface
|
| 37 |
with gr.Blocks() as demo:
|
| 38 |
-
gr.Markdown("# Document Segmentation Demo")
|
| 39 |
-
|
| 40 |
# Input Components
|
| 41 |
-
|
| 42 |
-
input_image = gr.Image(type="pil", label="Upload Image")
|
| 43 |
-
model_dropdown = gr.Dropdown(list(MODELS.keys()), label="Select Model", value=list(MODELS.keys())[0])
|
| 44 |
|
| 45 |
# Output Components
|
| 46 |
output_image = gr.Image(type="pil", label="Annotated Image")
|
|
@@ -48,7 +35,7 @@ with gr.Blocks() as demo:
|
|
| 48 |
|
| 49 |
# Button to trigger inference
|
| 50 |
btn = gr.Button("Run Document Segmentation")
|
| 51 |
-
btn.click(fn=process_image, inputs=
|
| 52 |
|
| 53 |
# Launch the demo
|
| 54 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from ultralytics import YOLO
|
| 3 |
+
import spaces # Import the `spaces` library
|
| 4 |
|
| 5 |
+
# Load pre-trained YOLOv8 model
|
| 6 |
+
model = YOLO("yolov8x-doclaynet-epoch64-imgsz640-initiallr1e-4-finallr1e-5.pt")
|
|
|
|
| 7 |
|
| 8 |
+
# Decorate the `process_image` function with `@spaces.GPU`
|
| 9 |
+
@spaces.GPU(duration=60) # Optional: Set the duration if needed
|
| 10 |
+
def process_image(image):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
try:
|
|
|
|
|
|
|
|
|
|
| 12 |
# Process the image
|
| 13 |
results = model(source=image, save=False, show_labels=True, show_conf=True, show_boxes=True)
|
| 14 |
result = results[0]
|
| 15 |
+
|
| 16 |
# Extract the annotated image and the labels/confidence scores
|
| 17 |
annotated_image = result.plot()
|
| 18 |
detected_areas_labels = "\n".join(
|
|
|
|
| 22 |
return annotated_image, detected_areas_labels
|
| 23 |
except Exception as e:
|
| 24 |
return None, f"Error processing image: {e}"
|
|
|
|
|
|
|
| 25 |
|
| 26 |
# Create the Gradio Interface
|
| 27 |
with gr.Blocks() as demo:
|
| 28 |
+
gr.Markdown("# Document Segmentation Demo (ZeroGPU)")
|
|
|
|
| 29 |
# Input Components
|
| 30 |
+
input_image = gr.Image(type="pil", label="Upload Image")
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# Output Components
|
| 33 |
output_image = gr.Image(type="pil", label="Annotated Image")
|
|
|
|
| 35 |
|
| 36 |
# Button to trigger inference
|
| 37 |
btn = gr.Button("Run Document Segmentation")
|
| 38 |
+
btn.click(fn=process_image, inputs=input_image, outputs=[output_image, output_text])
|
| 39 |
|
| 40 |
# Launch the demo
|
| 41 |
+
demo.queue(max_size=1).launch() # Queue to handle concurrent requests
|