File size: 960 Bytes
ae03257
 
 
2b9caa0
ae03257
 
 
8723d4e
ae03257
 
 
83e409c
f822b38
 
68e749a
ae03257
83e409c
 
ae03257
 
 
 
8794d26
ae03257
 
 
 
 
 
 
 
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
import os
from PIL import Image
import gradio as gr
from google import genai

# ๅˆๅง‹ๅŒ– Gemini API
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
client = genai.Client(api_key=GEMINI_API_KEY)

# ๅฎš็พฉใ€Œๅœ–่งฃ้‡‹ๆ–‡ใ€ๅŠŸ่ƒฝ
def explain_image(image: Image.Image):
    # ็›ดๆŽฅๆŠŠ PIL image ๅ‚ณ้€ฒๅŽป
    response = client.models.generate_content(
        model="gemini-2.0-flash",
        contents=[image, "ไฝฟ็”จ็น้ซ”ไธญๆ–‡ๆ่ฟฐ้€™ๅผตๅœ–็‰‡"],
    )
    # ๅ–ๅ‡บๅ›ž็ญ”
    explanation = response.text
    return explanation

# Gradio ไป‹้ข
with gr.Blocks() as demo:
    gr.Markdown("## ๐Ÿง ๆ–ฐ่ˆˆ้ซ˜ไธญ ็Ž‹ๆพ„้›… ๅœ–็‰‡ๆ™บๆ…งๅˆ†ๆž๏ผˆๅœ– โžœ ๆ–‡๏ผ‰")
    image_input = gr.Image(type="pil", label="ไธŠๅ‚ณๅœ–็‰‡")
    explain_button = gr.Button("่งฃ้‡‹ๅœ–็‰‡")
    output_text = gr.Textbox(label="ๅœ–็‰‡่ชชๆ˜Ž", lines=5)

    explain_button.click(fn=explain_image, inputs=image_input, outputs=output_text)

if __name__ == "__main__":
    demo.launch()