File size: 1,188 Bytes
ae03257
 
 
 
83e409c
ae03257
 
 
 
 
 
 
 
 
 
83e409c
 
ae03257
 
 
83e409c
 
 
ae03257
83e409c
 
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
33
34
35
36
37
38
39
40
41
import os
import logging
from PIL import Image
import gradio as gr
import google.generativeai as genai

# ่จญๅฎš logging
logging.basicConfig(
    filename='app.log',
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s'
)

# ๅˆๅง‹ๅŒ– Gemini API
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
genai.configure(api_key=GEMINI_API_KEY)
google_client = genai.GenerativeModel(model_name="gemini-2.0-flash")

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

# Gradio ไป‹้ข
with gr.Blocks() as demo:
    gr.Markdown("## ๐Ÿง  Gemini ๅœ–็‰‡่งฃ้‡‹ๅ™จ๏ผˆๅœ– โžœ ๆ–‡๏ผ‰")
    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()