Spaces:
Runtime error
Runtime error
Download image2text.py from shhuangmust/ImageExplain: direct link, hf CLI and curl.
- Browser
- Download file 954 Bytes
-
https://huggingface.co/spaces/shhuangmust/ImageExplain/resolve/refs%2Fpr%2F2/image2text.py
- Command line
-
hf download hf://spaces/shhuangmust/ImageExplain@refs/pr/2/image2text.py
-
curl -L -o image2text.py https://huggingface.co/spaces/shhuangmust/ImageExplain/resolve/refs%2Fpr%2F2/image2text.py
954 Bytes
| 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("## 🧠張家匯 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() | |