Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
|
@@ -86,7 +86,7 @@ async def webhook(
|
|
| 86 |
#==========================
|
| 87 |
# δ½Ώη¨θ
θ«ζ±ηζεη
|
| 88 |
#==========================
|
| 89 |
-
def upload_image_to_imgur(client, image_binary, album=None, name="
|
| 90 |
# ε° binary θ³ζθ½ηΊ PIL Image
|
| 91 |
image = PIL.Image.open(io.BytesIO(image_binary))
|
| 92 |
|
|
@@ -99,7 +99,7 @@ def upload_image_to_imgur(client, image_binary, album=None, name="Hugging Face-i
|
|
| 99 |
'album': album,
|
| 100 |
'name': name,
|
| 101 |
'title': title,
|
| 102 |
-
'description': f'Generated by
|
| 103 |
}
|
| 104 |
|
| 105 |
# δ½Ώη¨ client ι²θ‘εηδΈε³
|
|
@@ -108,10 +108,8 @@ def upload_image_to_imgur(client, image_binary, album=None, name="Hugging Face-i
|
|
| 108 |
# εε³εηηΆ²ε
|
| 109 |
return uploaded_image['link']
|
| 110 |
|
| 111 |
-
|
| 112 |
# δ½Ώη¨ Gemini ηζεη
|
| 113 |
def generate_image_with_gemini(prompt):
|
| 114 |
-
|
| 115 |
client = genai.Client(api_key=os.environ["GOOGLE_API_KEY"])
|
| 116 |
response = client.models.generate_content(
|
| 117 |
model="gemini-2.0-flash-exp-image-generation",
|
|
@@ -138,16 +136,6 @@ def generate_image_hf(prompt):
|
|
| 138 |
else:
|
| 139 |
print("εηηζε€±ζ:", response.text)
|
| 140 |
return None
|
| 141 |
-
|
| 142 |
-
def translate_zh_to_en(text):
|
| 143 |
-
model_name = "Helsinki-NLP/opus-mt-zh-en"
|
| 144 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 145 |
-
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 146 |
-
|
| 147 |
-
inputs = tokenizer(text, return_tensors="pt")
|
| 148 |
-
outputs = model.generate(**inputs)
|
| 149 |
-
translated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 150 |
-
return translated_text
|
| 151 |
|
| 152 |
#==========================
|
| 153 |
# δ½Ώη¨θ
δΈε³εη
|
|
@@ -221,7 +209,6 @@ def handle_image_message(event):
|
|
| 221 |
line_bot_api.reply_message(event.reply_token, TextSendMessage(text="εηηζδΈο½ θ«η¨ε.....β¨"))
|
| 222 |
client = ImgurClient(client_id, client_secret, access_token, refresh_token)
|
| 223 |
|
| 224 |
-
#prompt_en = translate_zh_to_en(prompt)
|
| 225 |
# ηζεη
|
| 226 |
image_binary = generate_image_with_gemini(prompt)
|
| 227 |
|
|
|
|
| 86 |
#==========================
|
| 87 |
# δ½Ώη¨θ
θ«ζ±ηζεη
|
| 88 |
#==========================
|
| 89 |
+
def upload_image_to_imgur(client, image_binary, album=None, name="gemini-image", title="gemini Generated Image"):
|
| 90 |
# ε° binary θ³ζθ½ηΊ PIL Image
|
| 91 |
image = PIL.Image.open(io.BytesIO(image_binary))
|
| 92 |
|
|
|
|
| 99 |
'album': album,
|
| 100 |
'name': name,
|
| 101 |
'title': title,
|
| 102 |
+
'description': f'Generated by gemini - {datetime.now()}'
|
| 103 |
}
|
| 104 |
|
| 105 |
# δ½Ώη¨ client ι²θ‘εηδΈε³
|
|
|
|
| 108 |
# εε³εηηΆ²ε
|
| 109 |
return uploaded_image['link']
|
| 110 |
|
|
|
|
| 111 |
# δ½Ώη¨ Gemini ηζεη
|
| 112 |
def generate_image_with_gemini(prompt):
|
|
|
|
| 113 |
client = genai.Client(api_key=os.environ["GOOGLE_API_KEY"])
|
| 114 |
response = client.models.generate_content(
|
| 115 |
model="gemini-2.0-flash-exp-image-generation",
|
|
|
|
| 136 |
else:
|
| 137 |
print("εηηζε€±ζ:", response.text)
|
| 138 |
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
#==========================
|
| 141 |
# δ½Ώη¨θ
δΈε³εη
|
|
|
|
| 209 |
line_bot_api.reply_message(event.reply_token, TextSendMessage(text="εηηζδΈο½ θ«η¨ε.....β¨"))
|
| 210 |
client = ImgurClient(client_id, client_secret, access_token, refresh_token)
|
| 211 |
|
|
|
|
| 212 |
# ηζεη
|
| 213 |
image_binary = generate_image_with_gemini(prompt)
|
| 214 |
|