Spaces:
Running
Running
content type
Browse files- text2image.py +17 -6
text2image.py
CHANGED
@@ -22,19 +22,30 @@ def explain_image(image: Image.Image):
|
|
22 |
# 將 PIL Image 轉成 Gemini 所需的格式
|
23 |
buffered = BytesIO()
|
24 |
image.save(buffered, format="PNG")
|
25 |
-
|
26 |
|
27 |
-
#
|
28 |
contents = [
|
29 |
-
types.
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
]
|
32 |
|
33 |
# 呼叫 Gemini 模型
|
34 |
-
response = client.
|
35 |
model="gemini-1.5-flash",
|
36 |
contents=contents,
|
37 |
-
|
38 |
)
|
39 |
|
40 |
# 回傳第一個回答
|
|
|
22 |
# 將 PIL Image 轉成 Gemini 所需的格式
|
23 |
buffered = BytesIO()
|
24 |
image.save(buffered, format="PNG")
|
25 |
+
image_bytes = buffered.getvalue()
|
26 |
|
27 |
+
# 直接建立 types.Content
|
28 |
contents = [
|
29 |
+
types.Content(
|
30 |
+
parts=[
|
31 |
+
types.Part(
|
32 |
+
inline_data=types.Blob(
|
33 |
+
mime_type="image/png",
|
34 |
+
data=image_bytes
|
35 |
+
)
|
36 |
+
),
|
37 |
+
types.Part(
|
38 |
+
text="請用繁體中文詳細說明這張圖片的內容。"
|
39 |
+
)
|
40 |
+
]
|
41 |
+
)
|
42 |
]
|
43 |
|
44 |
# 呼叫 Gemini 模型
|
45 |
+
response = client.generate_content(
|
46 |
model="gemini-1.5-flash",
|
47 |
contents=contents,
|
48 |
+
generation_config=types.GenerationConfig(response_mime_type="text/plain")
|
49 |
)
|
50 |
|
51 |
# 回傳第一個回答
|