Update Image_text_generation.py
Browse files- Image_text_generation.py +7 -29
Image_text_generation.py
CHANGED
@@ -6,14 +6,12 @@ import PIL.Image
|
|
6 |
from google import genai
|
7 |
from google.genai import types
|
8 |
import httpx
|
9 |
-
from imgurpython import ImgurClient
|
10 |
|
11 |
genai_client = genai.Client(api_key=os.environ["GOOGLE_API_KEY"])
|
12 |
|
13 |
class Image_text_Generator:
|
14 |
-
def __init__(self
|
15 |
-
|
16 |
-
self.imgur_client = imgur
|
17 |
|
18 |
def generate_image_with_gemini(self, prompt):
|
19 |
"""
|
@@ -26,7 +24,7 @@ class Image_text_Generator:
|
|
26 |
bytes: 生成的圖片的二進位資料,如果生成失敗則返回 None。
|
27 |
"""
|
28 |
response = genai_client.models.generate_content(
|
29 |
-
model="gemini-2.0-flash
|
30 |
contents=prompt,
|
31 |
config=types.GenerateContentConfig(response_modalities=['Text', 'Image'])
|
32 |
)
|
@@ -38,16 +36,8 @@ class Image_text_Generator:
|
|
38 |
return part.inline_data.data
|
39 |
return None
|
40 |
|
41 |
-
def
|
42 |
"""
|
43 |
-
將圖片上傳到 Imgur。
|
44 |
-
|
45 |
-
參數:
|
46 |
-
image_binary (bytes): 要上傳的圖片的二進位資料。
|
47 |
-
album (str): 要上傳到的相簿 ID,預設為 None。
|
48 |
-
name (str): 圖片的名稱,預設為 "gemini-image"。
|
49 |
-
title (str): 圖片的標題,預設為 "Gemini Generated Image"。
|
50 |
-
|
51 |
返回:
|
52 |
str: 上傳後的圖片 URL,如果上傳失敗則返回 None。
|
53 |
"""
|
@@ -56,22 +46,10 @@ class Image_text_Generator:
|
|
56 |
image = PIL.Image.open(io.BytesIO(image_binary))
|
57 |
|
58 |
# 建立暫存檔案以便上傳
|
59 |
-
|
60 |
-
image.save(tmp.name, format='PNG')
|
61 |
-
|
62 |
-
# 準備上傳資訊
|
63 |
-
config = {
|
64 |
-
'album': album,
|
65 |
-
'name': name,
|
66 |
-
'title': title,
|
67 |
-
'description': f'Generated by Gemini - {datetime.now()}'
|
68 |
-
}
|
69 |
-
|
70 |
-
# 使用 Imgur 客戶端上傳圖片
|
71 |
-
uploaded_image = self.imgur_client.upload_from_path(tmp.name, config=config, anon=False)
|
72 |
|
73 |
# 返回圖片的連結
|
74 |
-
return
|
75 |
except Exception as e:
|
76 |
print(f"圖片上傳失敗: {e}")
|
77 |
return None
|
@@ -80,7 +58,7 @@ class Image_text_Generator:
|
|
80 |
doc_data = httpx.get(doc_url).content
|
81 |
|
82 |
response = client.models.generate_content(
|
83 |
-
model="gemini-
|
84 |
contents=[
|
85 |
types.Part.from_bytes(data=doc_data, mime_type='application/pdf',),prompt]
|
86 |
)
|
|
|
6 |
from google import genai
|
7 |
from google.genai import types
|
8 |
import httpx
|
|
|
9 |
|
10 |
genai_client = genai.Client(api_key=os.environ["GOOGLE_API_KEY"])
|
11 |
|
12 |
class Image_text_Generator:
|
13 |
+
def __init__(self):
|
14 |
+
pass
|
|
|
15 |
|
16 |
def generate_image_with_gemini(self, prompt):
|
17 |
"""
|
|
|
24 |
bytes: 生成的圖片的二進位資料,如果生成失敗則返回 None。
|
25 |
"""
|
26 |
response = genai_client.models.generate_content(
|
27 |
+
model="gemini-2.0-flash",
|
28 |
contents=prompt,
|
29 |
config=types.GenerateContentConfig(response_modalities=['Text', 'Image'])
|
30 |
)
|
|
|
36 |
return part.inline_data.data
|
37 |
return None
|
38 |
|
39 |
+
def upload_image_to_tmp(self, image_binary):
|
40 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
返回:
|
42 |
str: 上傳後的圖片 URL,如果上傳失敗則返回 None。
|
43 |
"""
|
|
|
46 |
image = PIL.Image.open(io.BytesIO(image_binary))
|
47 |
|
48 |
# 建立暫存檔案以便上傳
|
49 |
+
image.save('tmp/temp.png', format='PNG')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
# 返回圖片的連結
|
52 |
+
return 'tmp/temp.png'
|
53 |
except Exception as e:
|
54 |
print(f"圖片上傳失敗: {e}")
|
55 |
return None
|
|
|
58 |
doc_data = httpx.get(doc_url).content
|
59 |
|
60 |
response = client.models.generate_content(
|
61 |
+
model="gemini-2.0-flash",
|
62 |
contents=[
|
63 |
types.Part.from_bytes(data=doc_data, mime_type='application/pdf',),prompt]
|
64 |
)
|