Evaaaaa06 commited on
Commit
c8dddb0
Β·
verified Β·
1 Parent(s): 268d397

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +13 -7
main.py CHANGED
@@ -108,15 +108,21 @@ def upload_image_to_imgur(client, image_binary, album=None, name="Hugging Face-i
108
 
109
  # 使用 Gemini η”Ÿζˆεœ–η‰‡
110
  def generate_image_with_gemini(prompt):
111
- model_name = "gemini-2.0-flash-exp-image-generation"
112
- image_model = genai.GenerativeModel(model_name)
113
 
114
- response = image_model.generate_content(prompt)
 
 
 
 
 
 
115
 
116
- if response.parts and hasattr(response.parts[0], "inline_data"):
117
- return response.parts[0].inline_data.data # η›΄ζŽ₯ε›žε‚³εœ–η‰‡binary資料
 
118
 
119
- return None
120
 
121
  # 使用 Hugging Face η”Ÿζˆεœ–η‰‡
122
  def generate_image_hf(prompt):
@@ -215,7 +221,7 @@ def handle_image_message(event):
215
 
216
  prompt_en = translate_zh_to_en(prompt)
217
  # η”Ÿζˆεœ–η‰‡
218
- image_binary = generate_image_hf(prompt_en)
219
 
220
  if image_binary:
221
  album = "nvsYwgq"
 
108
 
109
  # 使用 Gemini η”Ÿζˆεœ–η‰‡
110
  def generate_image_with_gemini(prompt):
111
+ client = genai.Client()
 
112
 
113
+ response = client.models.generate_content(
114
+ model="gemini-2.0-flash-exp-image-generation",
115
+ contents=prompt,
116
+ config=types.GenerateContentConfig(
117
+ response_modalities=['Text', 'Image']
118
+ )
119
+ )
120
 
121
+ for part in response.candidates[0].content.parts:
122
+ if part.inline_data is not None:
123
+ return part.inline_data.data # ε›žε‚³ binary εœ–η‰‡θ³‡ζ–™
124
 
125
+ return None # ε¦‚ζžœη„‘ζ³•η”Ÿζˆεœ–η‰‡οΌŒε›žε‚³None
126
 
127
  # 使用 Hugging Face η”Ÿζˆεœ–η‰‡
128
  def generate_image_hf(prompt):
 
221
 
222
  prompt_en = translate_zh_to_en(prompt)
223
  # η”Ÿζˆεœ–η‰‡
224
+ image_binary = generate_image_with_gemini(prompt_en)
225
 
226
  if image_binary:
227
  album = "nvsYwgq"