|
import numpy as np |
|
import gradio as gr |
|
import qrcode |
|
from PIL import Image |
|
import io |
|
|
|
|
|
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") |
|
|
|
|
|
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)ใ |
|
""" |
|
|
|
pil_image = Image.fromarray(image) |
|
|
|
resized_image = pil_image.resize((int(width), int(height))) |
|
return np.array(resized_image) |
|
|
|
|
|
|
|
|
|
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__": |
|
|
|
demo.launch(mcp_server=True) |
|
|