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)
|