File size: 1,860 Bytes
1f584ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75f5c93
1f584ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from functools import lru_cache

import gradio as gr
import numpy as np
from PIL import Image
from huggingface_hub import hf_hub_download

from cap import Predictor


@lru_cache()
def load_predictor(model):
    predictor = Predictor(hf_hub_download(
        f'7eu7d7/CAPTCHA_recognize',
        model,
    ))
    return predictor


def process_image(image):
    """
    Process the uploaded image - this is an example function
    You can modify this function to implement specific image processing logic
    """
    if image is None:
        return "Please upload an image first"

    # Example processing: convert image to grayscale
    if isinstance(image, np.ndarray):
        # If it's a numpy array, convert to PIL Image
        img = Image.fromarray(image.astype('uint8')).convert('RGB')
    else:
        img = image.convert('RGB')

    predictor = load_predictor('captcha-7400.safetensors')
    text = predictor.pred_img(img, show=False)
    return text


# Create Gradio interface
with gr.Blocks(title="CAPTCHA Recognize") as demo:

    with gr.Row():
        # Left column - Input area
        with gr.Column(scale=1):
            image_input = gr.Image(
                label="Upload CAPTCHA Image",
                type="pil",
                height=300
            )

            # Run button
            process_btn = gr.Button(
                "Run",
                variant="primary",
                size="lg"
            )

        # Right column - Output area
        with gr.Column(scale=1):
            text_output = gr.Textbox(
                label="Result",
                lines=4,
                interactive=False
            )

    # Bind events
    process_btn.click(
        fn=process_image,
        inputs=image_input,
        outputs=[text_output]
    )


# Launch the application
if __name__ == "__main__":
    demo.launch()