Spaces:
Sleeping
Sleeping
Codex CLI
commited on
Commit
·
b867af2
1
Parent(s):
5bf673e
🔧 fix: #9 アプリの更新!
Browse files🔍 問題: 下記の内容のアプリにapp.pyを更新して\nーーー\n\n\n
✅ 対応: 必要なファイルを追加・修正しました
Issue: #9
- app.py +93 -35
- requirements.txt +5 -1
app.py
CHANGED
@@ -1,49 +1,107 @@
|
|
1 |
-
import
|
2 |
import gradio as gr
|
|
|
|
|
|
|
3 |
|
4 |
-
|
|
|
5 |
"""
|
6 |
-
|
7 |
|
8 |
Args:
|
9 |
-
text (str):
|
10 |
-
letter (str): Letter to count.
|
11 |
|
12 |
Returns:
|
13 |
-
|
14 |
"""
|
15 |
-
return text
|
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 |
if __name__ == "__main__":
|
42 |
# mcp_server=True starts the SSE endpoint at /gradio_api/mcp/sse
|
43 |
-
|
44 |
-
# PORT 環境変数があればそれを使い、無ければデフォルト 7860✨
|
45 |
-
demo.launch(
|
46 |
-
mcp_server=True,
|
47 |
-
server_name="0.0.0.0",
|
48 |
-
server_port=int(os.getenv("PORT", 7860)),
|
49 |
-
)
|
|
|
1 |
+
import numpy as np
|
2 |
import gradio as gr
|
3 |
+
import qrcode
|
4 |
+
from PIL import Image
|
5 |
+
import io # Added for parity with user-provided snippet (currently unused)
|
6 |
|
7 |
+
|
8 |
+
def reverse_text(text):
|
9 |
"""
|
10 |
+
テキストを反転する。
|
11 |
|
12 |
Args:
|
13 |
+
text (str): 反転したいテキスト。
|
|
|
14 |
|
15 |
Returns:
|
16 |
+
str: 反転されたテキスト。
|
17 |
"""
|
18 |
+
return text[::-1]
|
19 |
|
20 |
+
|
21 |
+
def generate_qr_code(text):
|
22 |
+
"""
|
23 |
+
テキストからQRコードを生成する。
|
24 |
+
|
25 |
+
Args:
|
26 |
+
text (str): QRコードに埋め込むテキスト。
|
27 |
+
|
28 |
+
Returns:
|
29 |
+
numpy.ndarray: 生成されたQRコード画像 (RGB)。
|
30 |
+
"""
|
31 |
+
qr = qrcode.QRCode(version=5, box_size=10, border=5)
|
32 |
+
qr.add_data(text)
|
33 |
+
qr.make(fit=True)
|
34 |
+
img = qr.make_image(fill_color="black", back_color="white")
|
35 |
+
|
36 |
+
# PILイメージをNumPy配列に変換
|
37 |
+
img_array = np.array(img.convert("RGB"))
|
38 |
+
return img_array
|
39 |
+
|
40 |
+
|
41 |
+
def count_words(text):
|
42 |
+
"""
|
43 |
+
テキストの単語数をカウントする。
|
44 |
+
|
45 |
+
Args:
|
46 |
+
text (str): カウントしたいテキスト。
|
47 |
+
|
48 |
+
Returns:
|
49 |
+
int: 単語数。
|
50 |
+
"""
|
51 |
+
if not text.strip():
|
52 |
+
return 0
|
53 |
+
return len(text.split())
|
54 |
+
|
55 |
+
|
56 |
+
def resize_image(image, width, height):
|
57 |
+
"""
|
58 |
+
画像をリサイズする。
|
59 |
+
|
60 |
+
Args:
|
61 |
+
image (numpy.ndarray): リサイズしたい画像。
|
62 |
+
width (int): 新しい幅。
|
63 |
+
height (int): 新しい高さ。
|
64 |
+
|
65 |
+
Returns:
|
66 |
+
numpy.ndarray: リサイズされた画像 (RGB)。
|
67 |
+
"""
|
68 |
+
# NumPy配列からPILイメージに変換
|
69 |
+
pil_image = Image.fromarray(image)
|
70 |
+
|
71 |
+
resized_image = pil_image.resize((int(width), int(height)))
|
72 |
+
return np.array(resized_image)
|
73 |
+
|
74 |
+
|
75 |
+
# --- Interface -----------------------------------------------------------
|
76 |
+
|
77 |
+
resize_interface = gr.Interface(
|
78 |
+
fn=resize_image,
|
79 |
+
inputs=[
|
80 |
+
gr.Image(),
|
81 |
+
gr.Number(label="幅", value=300),
|
82 |
+
gr.Number(label="高さ", value=300),
|
83 |
+
],
|
84 |
+
outputs=gr.Image(),
|
85 |
+
api_name="resize_image",
|
86 |
)
|
87 |
+
|
88 |
+
|
89 |
+
demo = gr.TabbedInterface(
|
90 |
+
[
|
91 |
+
gr.Interface(reverse_text, gr.Textbox(), gr.Textbox(), api_name="reverse_text"),
|
92 |
+
gr.Interface(generate_qr_code, gr.Textbox(), gr.Image(), api_name="generate_qr_code"),
|
93 |
+
gr.Interface(count_words, gr.Textbox(), gr.Number(), api_name="count_words"),
|
94 |
+
resize_interface,
|
95 |
+
],
|
96 |
+
[
|
97 |
+
"テキスト反転",
|
98 |
+
"QRコード生成",
|
99 |
+
"単語数カウント",
|
100 |
+
"画像リサイズ",
|
101 |
+
],
|
102 |
)
|
103 |
|
104 |
+
|
105 |
if __name__ == "__main__":
|
106 |
# mcp_server=True starts the SSE endpoint at /gradio_api/mcp/sse
|
107 |
+
demo.launch(mcp_server=True)
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -1,2 +1,6 @@
|
|
|
|
|
|
|
|
|
|
1 |
mcp
|
2 |
-
https://gradio-pypi-previews.s3.amazonaws.com/3b5cace94781b90993b596a83fb39fd1584d68ee/gradio-5.26.0-py3-none-any.whl
|
|
|
1 |
+
gradio
|
2 |
+
numpy
|
3 |
+
qrcode[pil]
|
4 |
+
pillow
|
5 |
mcp
|
6 |
+
https://gradio-pypi-previews.s3.amazonaws.com/3b5cace94781b90993b596a83fb39fd1584d68ee/gradio-5.26.0-py3-none-any.whl
|