Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
|
@@ -90,25 +90,7 @@ def upload_image_to_imgur_with_token(image_path, access_token):
|
|
| 90 |
except Exception as e:
|
| 91 |
print("εηδΈε³δΎε€οΌ", e)
|
| 92 |
return None
|
| 93 |
-
|
| 94 |
-
def generate_image_with_gemini(prompt, size=(512, 512)):
|
| 95 |
-
model_name = "gemini-2.0-flash-exp-image-generation"
|
| 96 |
-
image_model = genai.GenerativeModel(model_name)
|
| 97 |
-
prompt += f"οΌε°Ίε―ΈοΌ{size[0]}x{size[1]}"
|
| 98 |
-
response = image_model.generate_content(prompt)
|
| 99 |
-
if response.parts and hasattr(response.parts[0], "inline_data"):
|
| 100 |
-
image_data = response.parts[0].inline_data.data
|
| 101 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".png", dir="/tmp") as tmp_file:
|
| 102 |
-
# ζΈ
ηε
εηζηζ«εεζͺοΌιΈζζ§οΌε―ε εΌ·οΌ
|
| 103 |
-
for f in os.listdir("/tmp"):
|
| 104 |
-
if f.endswith(".png") and os.path.getmtime(f"/tmp/{f}") < os.path.getmtime(tmp_file.name):
|
| 105 |
-
try:
|
| 106 |
-
os.remove(f"/tmp/{f}")
|
| 107 |
-
except:
|
| 108 |
-
pass
|
| 109 |
-
tmp_file.write(image_data)
|
| 110 |
-
return tmp_file.name
|
| 111 |
-
return None
|
| 112 |
|
| 113 |
#==========================
|
| 114 |
# δ½Ώη¨θ
δΈε³εη
|
|
@@ -179,9 +161,15 @@ def handle_image_message(event):
|
|
| 179 |
if user_text and user_text.startswith("θ«εΉ«ζηζεη"):
|
| 180 |
prompt = user_text.replace("θ«εΉ«ζηζεη", "").strip()
|
| 181 |
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
if image_path:
|
| 184 |
-
image_url = f"https://your-domain.com/tmp/{os.path.basename(image_path)}"
|
| 185 |
line_bot_api.reply_message(
|
| 186 |
event.reply_token,
|
| 187 |
[
|
|
|
|
| 90 |
except Exception as e:
|
| 91 |
print("εηδΈε³δΎε€οΌ", e)
|
| 92 |
return None
|
| 93 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
#==========================
|
| 96 |
# δ½Ώη¨θ
δΈε³εη
|
|
|
|
| 161 |
if user_text and user_text.startswith("θ«εΉ«ζηζεη"):
|
| 162 |
prompt = user_text.replace("θ«εΉ«ζηζεη", "").strip()
|
| 163 |
|
| 164 |
+
image_model = genai.GenerativeModel("gemini-2.0-flash-exp-image-generation")
|
| 165 |
+
response = image_model.generate_content(prompt)
|
| 166 |
+
|
| 167 |
+
if response.parts and hasattr(response.parts[0], "inline_data"):
|
| 168 |
+
image_data = response.parts[0].inline_data.data
|
| 169 |
+
access_token = os.environ.get("IMGUR_ACCESS_TOKEN")
|
| 170 |
+
image_url = upload_image_to_imgur_with_token(image_data,access_token)
|
| 171 |
+
|
| 172 |
if image_path:
|
|
|
|
| 173 |
line_bot_api.reply_message(
|
| 174 |
event.reply_token,
|
| 175 |
[
|