mcp-101 / app.py
Yoon-gu Hwang
์—…๋ฐ์ดํŠธ
0db572d
import numpy as np
import gradio as gr
from pathlib import Path
import os
from PIL import Image
def prime_factors(n: str):
"""
Compute the prime factorization of a positive integer.
Args:
n (str): The integer to factorize. Must be greater than 1.
"""
n_int = int(n)
if n_int <= 1:
raise ValueError("Input must be an integer greater than 1.")
factors = []
while n_int % 2 == 0:
factors.append(2)
n_int //= 2
divisor = 3
while divisor * divisor <= n_int:
while n_int % divisor == 0:
factors.append(divisor)
n_int //= divisor
divisor += 2
if n_int > 1:
factors.append(n_int)
return factors
def image_orientation(image: Image.Image) -> str:
"""
Returns whether image is portrait or landscape.
Args:
image (Image.Image): The image to check.
Returns:
str: "Portrait" if image is portrait, "Landscape" if image is landscape.
"""
return "Portrait" if image.height > image.width else "Landscape"
def sepia(input_img):
"""
Apply a sepia filter to the input image.
Args:
input_img (np.array): The input image to apply the sepia filter to.
Returns:
The sepia filtered image.
"""
sepia_filter = np.array([
[0.393, 0.769, 0.189],
[0.349, 0.686, 0.168],
[0.272, 0.534, 0.131]
])
sepia_img = input_img.dot(sepia_filter.T)
sepia_img /= sepia_img.max()
return sepia_img
def echo(message, history):
return message
def vote(data: gr.LikeData):
print(data.liked)
def add(a: int, b: int) -> int:
"""Add two numbers"""
return a + b
def multiply(a: int, b: int) -> int:
"""Multiply two numbers"""
return a * b
with gr.Blocks() as chat_ui:
chatbot = gr.Chatbot(placeholder="<strong>Your Personal Echo-Man</strong><br>Ask Me Anything")
chatbot.like(vote, None, None)
gr.ChatInterface(fn=echo, type="messages", chatbot=chatbot, examples=["hello", "hola", "merhaba"], title="Echo Bot")
from app_tool import demo as ui
demo = gr.TabbedInterface(
[
ui,
chat_ui,
gr.Interface(prime_factors, gr.Textbox("1001"), gr.Textbox()),
gr.Interface(image_orientation, gr.Image(type="pil"), gr.Textbox()),
gr.Interface(sepia, gr.Image(), gr.Image()),
gr.Interface(add, [gr.Number(), gr.Number()], gr.Number()),
gr.Interface(multiply, [gr.Number(), gr.Number()], gr.Number())
],
[
"Langgraph Chatbot",
"Chat UI",
"Prime Factors",
"Image Orientation Checker",
"Sepia Filter",
"Add",
"multiply"
]
)
if __name__ == "__main__":
demo.launch(mcp_server=True)