3v324v23 commited on
Commit
67cd09d
·
1 Parent(s): 3969f62

content type

Browse files
Files changed (1) hide show
  1. 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
- image_data = buffered.getvalue()
26
 
27
- # 準備輸入給 Gemini 的內容
28
  contents = [
29
- types.ContentPart.from_data(data=image_data, mime_type="image/png"),
30
- types.ContentPart.text("請用繁體中文說明這張圖片的內容。")
 
 
 
 
 
 
 
 
 
 
 
31
  ]
32
 
33
  # 呼叫 Gemini 模型
34
- response = client.models.generate_content(
35
  model="gemini-1.5-flash",
36
  contents=contents,
37
- config=types.GenerateContentConfig(response_modalities=["TEXT"])
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
  # 回傳第一個回答