File size: 2,598 Bytes
1fe9b6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
103
104
105
106
107
108
import numpy as np
import gradio as gr
import qrcode
from PIL import Image
import io  # Added for parity with user-provided snippet (currently unused)


def reverse_text(text):
    """
    ใƒ†ใ‚ญใ‚นใƒˆใ‚’ๅ่ปขใ™ใ‚‹ใ€‚

    Args:
        text (str): ๅ่ปขใ—ใŸใ„ใƒ†ใ‚ญใ‚นใƒˆใ€‚

    Returns:
        str: ๅ่ปขใ•ใ‚ŒใŸใƒ†ใ‚ญใ‚นใƒˆใ€‚
    """
    return text[::-1]


def generate_qr_code(text):
    """
    ใƒ†ใ‚ญใ‚นใƒˆใ‹ใ‚‰QRใ‚ณใƒผใƒ‰ใ‚’็”Ÿๆˆใ™ใ‚‹ใ€‚

    Args:
        text (str): QRใ‚ณใƒผใƒ‰ใซๅŸ‹ใ‚่พผใ‚€ใƒ†ใ‚ญใ‚นใƒˆใ€‚

    Returns:
        numpy.ndarray: ็”Ÿๆˆใ•ใ‚ŒใŸQRใ‚ณใƒผใƒ‰็”ปๅƒ (RGB)ใ€‚
    """
    qr = qrcode.QRCode(version=5, box_size=10, border=5)
    qr.add_data(text)
    qr.make(fit=True)
    img = qr.make_image(fill_color="black", back_color="white")

    # PILใ‚คใƒกใƒผใ‚ธใ‚’NumPy้…ๅˆ—ใซๅค‰ๆ›
    img_array = np.array(img.convert("RGB"))
    return img_array


def count_words(text):
    """
    ใƒ†ใ‚ญใ‚นใƒˆใฎๅ˜่ชžๆ•ฐใ‚’ใ‚ซใ‚ฆใƒณใƒˆใ™ใ‚‹ใ€‚

    Args:
        text (str): ใ‚ซใ‚ฆใƒณใƒˆใ—ใŸใ„ใƒ†ใ‚ญใ‚นใƒˆใ€‚

    Returns:
        int: ๅ˜่ชžๆ•ฐใ€‚
    """
    if not text.strip():
        return 0
    return len(text.split())


def resize_image(image, width, height):
    """
    ็”ปๅƒใ‚’ใƒชใ‚ตใ‚คใ‚บใ™ใ‚‹ใ€‚

    Args:
        image (numpy.ndarray): ใƒชใ‚ตใ‚คใ‚บใ—ใŸใ„็”ปๅƒใ€‚
        width (int): ๆ–ฐใ—ใ„ๅน…ใ€‚
        height (int): ๆ–ฐใ—ใ„้ซ˜ใ•ใ€‚

    Returns:
        numpy.ndarray: ใƒชใ‚ตใ‚คใ‚บใ•ใ‚ŒใŸ็”ปๅƒ (RGB)ใ€‚
    """
    # NumPy้…ๅˆ—ใ‹ใ‚‰PILใ‚คใƒกใƒผใ‚ธใซๅค‰ๆ›
    pil_image = Image.fromarray(image)

    resized_image = pil_image.resize((int(width), int(height)))
    return np.array(resized_image)


# --- Interface -----------------------------------------------------------

resize_interface = gr.Interface(
    fn=resize_image,
    inputs=[
        gr.Image(),
        gr.Number(label="ๅน…", value=300),
        gr.Number(label="้ซ˜ใ•", value=300),
    ],
    outputs=gr.Image(),
    api_name="resize_image",
)


demo = gr.TabbedInterface(
    [
        gr.Interface(reverse_text, gr.Textbox(), gr.Textbox(), api_name="reverse_text"),
        gr.Interface(generate_qr_code, gr.Textbox(), gr.Image(), api_name="generate_qr_code"),
        gr.Interface(count_words, gr.Textbox(), gr.Number(), api_name="count_words"),
        resize_interface,
    ],
    [
        "ใƒ†ใ‚ญใ‚นใƒˆๅ่ปข",
        "QRใ‚ณใƒผใƒ‰็”Ÿๆˆ",
        "ๅ˜่ชžๆ•ฐใ‚ซใ‚ฆใƒณใƒˆ",
        "็”ปๅƒใƒชใ‚ตใ‚คใ‚บ",
    ],
)


if __name__ == "__main__":
    # mcp_server=True starts the SSE endpoint at /gradio_api/mcp/sse
    demo.launch(mcp_server=True)