Evaaaaa06 commited on
Commit
38bd070
·
verified ·
1 Parent(s): a83f899

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +28 -29
main.py CHANGED
@@ -77,6 +77,26 @@ async def webhook(
77
  #==========================
78
  # 使用者請求生成圖片
79
  #==========================
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  def upload_image_to_imgur(client, image_binary, album=None, name="gemini-image", title="Gemini Generated Image"):
81
  config = {
82
  'album': album,
@@ -96,35 +116,14 @@ def upload_image_to_imgur(client, image_binary, album=None, name="gemini-image",
96
  # 使用 Gemini 生成圖片
97
  def generate_image_with_gemini(prompt):
98
  model_name = "gemini-2.0-flash-exp-image-generation"
 
99
 
100
- client = genai.GenerativeModel(model_name)
101
-
102
- contents = [genai.types.Content(
103
- role="user",
104
- parts=[genai.types.Part.from_text(prompt)]
105
- )]
106
-
107
- generate_content_config = genai.types.GenerateContentConfig(
108
- temperature=1,
109
- top_p=0.95,
110
- top_k=40,
111
- max_output_tokens=1000,
112
- response_modalities=["image", "text"],
113
- response_mime_type="text/plain",
114
- )
115
-
116
- for chunk in client.generate_content_stream(
117
- contents=contents,
118
- generation_config=generate_content_config,
119
- ):
120
- if not chunk.candidates or not chunk.candidates[0].content.parts:
121
- continue
122
- if chunk.candidates[0].content.parts[0].inline_data:
123
- inline_data = chunk.candidates[0].content.parts[0].inline_data
124
- file_extension = mimetypes.guess_extension(inline_data.mime_type) or ".png"
125
- return inline_data.data, file_extension
126
-
127
- return None, None
128
 
129
  #==========================
130
  # 使用者上傳圖片
@@ -199,7 +198,7 @@ def handle_image_message(event):
199
 
200
  if image_binary:
201
  album = "nvsYwgq" # 你的相簿ID
202
- image_url = upload_image_to_imgur(client, image_binary, album)
203
  line_bot_api.reply_message(
204
  event.reply_token,
205
  [
 
77
  #==========================
78
  # 使用者請求生成圖片
79
  #==========================
80
+ def upload_image_to_imgur_direct(image_binary, client_id, album=None, name="gemini-image", title="Gemini Generated Image"):
81
+ headers = {
82
+ "Authorization": f"Client-ID {client_id}",
83
+ }
84
+ data = {
85
+ 'image': base64.b64encode(image_binary).decode('utf-8'),
86
+ 'album': album,
87
+ 'type': 'base64',
88
+ 'name': name,
89
+ 'title': title,
90
+ 'description': f'Generated by Gemini - {datetime.now()}'
91
+ }
92
+
93
+ response = httpx.post("https://api.imgur.com/3/image", headers=headers, data=data)
94
+ if response.status_code == 200:
95
+ return response.json()["data"]["link"]
96
+ else:
97
+ print("Imgur 上傳失敗:", response.text)
98
+ return None
99
+
100
  def upload_image_to_imgur(client, image_binary, album=None, name="gemini-image", title="Gemini Generated Image"):
101
  config = {
102
  'album': album,
 
116
  # 使用 Gemini 生成圖片
117
  def generate_image_with_gemini(prompt):
118
  model_name = "gemini-2.0-flash-exp-image-generation"
119
+ image_model = genai.GenerativeModel(model_name)
120
 
121
+ response = image_model.generate_content(prompt)
122
+
123
+ if response.parts and hasattr(response.parts[0], "inline_data"):
124
+ return response.parts[0].inline_data.data # 直接回傳圖片binary資料
125
+
126
+ return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
 
128
  #==========================
129
  # 使用者上傳圖片
 
198
 
199
  if image_binary:
200
  album = "nvsYwgq" # 你的相簿ID
201
+ image_url = upload_image_to_imgur_direct(client, image_binary, album)
202
  line_bot_api.reply_message(
203
  event.reply_token,
204
  [