GradioSample / app.py
medical-kiban's picture
no message
1a45176
import gradio as gr
from transformers import pipeline
from PIL import Image
classifier = pipeline("image-classification")
def classify_image(img: Image.Image):
results = classifier(img)
formatted = "\n".join([f"{r['label']} ({r['score']:.2%})" for r in results])
return formatted
def greet(name, age, mood):
return f"{name}さん、こんにちは!年齢は{age}歳で、気分は「{mood}」ですね! これはサンプルなので特に機能はないですよ!😊"
def process_file(file):
with open(file.name, "r", encoding="utf-8") as f:
content = f.read()
return content[:300] + "\n...(省略)" if len(content) > 300 else content
def image_shape(img):
return f"画像サイズ:{img.size[0]} x {img.size[1]}"
with gr.Blocks() as demo:
gr.Markdown("## 🖼️ HuggingFaceLLMとの連携")
gr.Markdown("#### 犬か猫の写真を入れるとAIが種類を判別してくれます。")
with gr.Row():
with gr.Column():
image_input = gr.Image(type="pil", label="イメージを載せてください")
btn = gr.Button("どの種類だろう・・・")
with gr.Column():
result_output = gr.Textbox(label="結果", lines=10)
btn.click(fn=classify_image, inputs=image_input, outputs=result_output)
gr.Markdown("## 🖼️各種widgetのサンプル")
with gr.Tab("👋 基本入力"):
with gr.Row():
name = gr.Textbox(label="名前 (Textbox機能)")
age = gr.Slider(0, 100, value=20, label="年齢 (Slider機能)")
mood = gr.Radio(["嬉しい", "悲しい", "疲れた", "怒っている"], label="今の気分 (Radio機能)")
greet_btn = gr.Button("確認する (Button機能)")
greet_output = gr.Textbox(label="結果")
greet_btn.click(fn=greet, inputs=[name, age, mood], outputs=greet_output)
with gr.Tab("🖼️ 画像処理"):
gr.Markdown("#### 画像オブジェクトとして処理してくれます")
img_input = gr.Image(type="pil", label="画像をアップロード")
img_btn = gr.Button("画像情報を表示")
img_output = gr.Textbox(label="出力")
img_btn.click(fn=image_shape, inputs=img_input, outputs=img_output)
with gr.Tab("📄 ファイル処理"):
file_input = gr.File(file_types=[".txt"], label="テキストファイルをアップロード")
file_output = gr.Textbox(label="ファイル内容プレビュー")
file_input.change(fn=process_file, inputs=file_input, outputs=file_output)
my_list = ["Option 1", "Option 2", "Option 3", "Option 4 ", "Medical"]
with gr.Tab("🧩 その他のウィジェット"):
with gr.Accordion("🔽 Accordion機能", open=False):
chk = gr.Checkbox(label="Checkbox機能")
drop1 = gr.Dropdown(choices=my_list, label="Dropdown機能", value=my_list[0], interactive=True)
gr.Textbox(value="テキスト", label="Textbox機能")
demo.launch()