File size: 3,651 Bytes
6f3b453
10e69ed
 
 
 
6f3b453
10e69ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f3b453
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import gradio as gr
import requests
from image_utils import print_text_on_image_centered, create_background_image
from hf_utils import hf_validate_api_token
from segmentation_utils import segment_and_overlay_results

def segment_gradio_image(api_token, model, image):
    
    # Validacion del token y la imagen
    
    is_token_valid, api_token_message = hf_validate_api_token(api_token)
    if not is_token_valid:
        text_image = print_text_on_image_centered(
            create_background_image(500, 500, "white"),
            'HuggingFace API Token invalid. Please enter a valid token.',
            'red'
        )
        segments_list = "No segments available."
    else:
        if image is None:
            text_image = print_text_on_image_centered(
                create_background_image(500, 500, "white"),
                'No image detected',
                'orange'
            )
            segments_list = "No segments available."
        else:
            text_image = print_text_on_image_centered(
                create_background_image(500, 500, "white"),
                'PROCESANDO',
                'blue'
            )
            segments_list = "No segments available."
            # Assuming segment_image is a placeholder for actual segmentation function
            # Uncomment and modify this part according to your segmentation implementation
            # response = segment_image(api_token, model, image)
            # text_image = response["segmented_image"]
            
            text_image, segments = segment_and_overlay_results(image,model,api_token)
            print("app.py segment_gradio_image")
            segments_list = "Segments:\n"
            for segment in segments:
                print(segment['label'] + " " + str(segment['score']))
                segments_list += f"\n{segment['label']}: {segment['score']}"
           
    
    return api_token_message, text_image, segments_list



with gr.Blocks() as demo:
    gr.Markdown("# Segment Image")
    gr.Markdown("Upload an image and let the model segment it.")
    
    with gr.Row():
        api_token = gr.Textbox(
            label="API Token", 
            placeholder="Enter your Hugging Face API token here"
        )
        model_name = gr.Textbox(
            label="AI Segmentation Model", 
            placeholder="Enter your Segmentation model here",
            value="facebook/mask2former-swin-tiny-coco-panoptic"
        )
    
    image_input = gr.Image(label="Upload Image")
    
    with gr.Row():
        api_token_validation = gr.Textbox(label="API Token Validation")
        segmented_image = gr.Image(label="Segmented Image")
        
    # New block for segments output
    
    with gr.Row():
        segments_output = gr.Textbox(label="Segments")
        
    examples = gr.Examples(
        examples=[
            ["Your HF API Token", "facebook/mask2former-swin-tiny-coco-panoptic", "https://upload.wikimedia.org/wikipedia/commons/7/74/A-Cat.jpg"]
        ],
        inputs=[api_token, model_name, image_input]
    )
    
    api_token.change(
        fn=segment_gradio_image, 
        inputs=[api_token, model_name, image_input], 
        outputs=[api_token_validation, segmented_image, segments_output]
    )

    model_name.change(
        fn=segment_gradio_image, 
        inputs=[api_token, model_name, image_input], 
        outputs=[api_token_validation, segmented_image, segments_output]
    )

    image_input.change(
        fn=segment_gradio_image, 
        inputs=[api_token, model_name, image_input], 
        outputs=[api_token_validation, segmented_image, segments_output]
    )

demo.launch()