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()