Spaces:
Runtime error
Runtime error
Update Image_text_generation.py
Browse files- Image_text_generation.py +7 -6
Image_text_generation.py
CHANGED
|
@@ -10,9 +10,10 @@ import httpx
|
|
| 10 |
genai_client = genai.Client(api_key=os.environ["GOOGLE_API_KEY"])
|
| 11 |
|
| 12 |
class Image_text_Generator:
|
| 13 |
-
def __init__(self):
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
def generate_image_with_gemini(self, prompt):
|
| 17 |
"""
|
| 18 |
使用 Gemini 模型生成圖片。
|
|
@@ -36,7 +37,7 @@ class Image_text_Generator:
|
|
| 36 |
return part.inline_data.data
|
| 37 |
return None
|
| 38 |
|
| 39 |
-
def upload_image_to_tmp(self
|
| 40 |
"""
|
| 41 |
返回:
|
| 42 |
str: 上傳後的圖片 URL,如果上傳失敗則返回 None。
|
|
@@ -46,10 +47,10 @@ class Image_text_Generator:
|
|
| 46 |
image = PIL.Image.open(io.BytesIO(image_binary))
|
| 47 |
|
| 48 |
# 建立暫存檔案以便上傳
|
| 49 |
-
image.save('tmp/'+user_id+'.png', format='PNG')
|
| 50 |
|
| 51 |
# 返回圖片的連結
|
| 52 |
-
return 'tmp/'+user_id+'.png'
|
| 53 |
except Exception as e:
|
| 54 |
print(f"圖片上傳失敗: {e}")
|
| 55 |
return None
|
|
|
|
| 10 |
genai_client = genai.Client(api_key=os.environ["GOOGLE_API_KEY"])
|
| 11 |
|
| 12 |
class Image_text_Generator:
|
| 13 |
+
def __init__(self, user_id):
|
| 14 |
+
|
| 15 |
+
sellf.user_id = user_id
|
| 16 |
+
|
| 17 |
def generate_image_with_gemini(self, prompt):
|
| 18 |
"""
|
| 19 |
使用 Gemini 模型生成圖片。
|
|
|
|
| 37 |
return part.inline_data.data
|
| 38 |
return None
|
| 39 |
|
| 40 |
+
def upload_image_to_tmp(self):
|
| 41 |
"""
|
| 42 |
返回:
|
| 43 |
str: 上傳後的圖片 URL,如果上傳失敗則返回 None。
|
|
|
|
| 47 |
image = PIL.Image.open(io.BytesIO(image_binary))
|
| 48 |
|
| 49 |
# 建立暫存檔案以便上傳
|
| 50 |
+
image.save('tmp/'+self.user_id+'.png', format='PNG')
|
| 51 |
|
| 52 |
# 返回圖片的連結
|
| 53 |
+
return 'tmp/'+self.user_id+'.png'
|
| 54 |
except Exception as e:
|
| 55 |
print(f"圖片上傳失敗: {e}")
|
| 56 |
return None
|