ImageGen / text2image.py
3v324v23's picture
正確寫法
83e409c
raw
history blame
1.19 kB
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()