Delete image2text.py
Browse files- image2text.py +0 -31
image2text.py
DELETED
|
@@ -1,31 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from PIL import Image
|
| 3 |
-
import gradio as gr
|
| 4 |
-
from google import genai
|
| 5 |
-
|
| 6 |
-
# 初始化 Gemini API
|
| 7 |
-
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 8 |
-
client = genai.Client(api_key=GEMINI_API_KEY)
|
| 9 |
-
|
| 10 |
-
# 定義「圖解釋文」功能
|
| 11 |
-
def explain_image(image: Image.Image):
|
| 12 |
-
# 直接把 PIL image 傳進去
|
| 13 |
-
response = client.models.generate_content(
|
| 14 |
-
model="gemini-2.0-flash",
|
| 15 |
-
contents=[image, "使用繁體中文描述這張圖片"],
|
| 16 |
-
)
|
| 17 |
-
# 取出回答
|
| 18 |
-
explanation = response.text
|
| 19 |
-
return explanation
|
| 20 |
-
|
| 21 |
-
# Gradio 介面
|
| 22 |
-
with gr.Blocks() as demo:
|
| 23 |
-
gr.Markdown("## 🧠B09000000 黃老師 Gemini 圖片解釋器(圖 ➜ 文)")
|
| 24 |
-
image_input = gr.Image(type="pil", label="上傳圖片")
|
| 25 |
-
explain_button = gr.Button("解釋圖片")
|
| 26 |
-
output_text = gr.Textbox(label="圖片說明", lines=5)
|
| 27 |
-
|
| 28 |
-
explain_button.click(fn=explain_image, inputs=image_input, outputs=output_text)
|
| 29 |
-
|
| 30 |
-
if __name__ == "__main__":
|
| 31 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|