fix: file not close

Signed-off-by: yihong0618 <zouzou0208@gmail.com>
This commit is contained in:
yihong0618 2024-03-05 17:04:24 +08:00
parent 66e3160f17
commit 6f6771e1e8
2 changed files with 27 additions and 25 deletions

View File

@ -201,31 +201,33 @@ def claude_photo_handler(message: Message, bot: TeleBot) -> None:
with open("claude_temp.jpg", "wb") as temp_file: with open("claude_temp.jpg", "wb") as temp_file:
temp_file.write(downloaded_file) temp_file.write(downloaded_file)
f = Path("claude_temp.jpg")
try: try:
r = client.messages.create( with f:
max_tokens=1024, r = client.messages.create(
messages=[ max_tokens=1024,
{ messages=[
"role": "user", {
"content": [ "role": "user",
{ "content": [
"type": "text", {
"text": prompt, "type": "text",
}, "text": prompt,
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": Path("claude_temp.jpg"),
}, },
}, {
], "type": "image",
}, "source": {
], "type": "base64",
model="claude-3-opus-20240229", "media_type": "image/jpeg",
) "data": f,
bot.reply_to(message, "Claude vision answer:\n" + r.content[0].text) },
},
],
},
],
model=ANTHROPIC_MODEL,
)
bot.reply_to(message, "Claude vision answer:\n" + r.content[0].text)
except Exception as e: except Exception as e:
print(e) print(e)
bot.reply_to( bot.reply_to(

View File

@ -210,8 +210,8 @@ def gemini_photo_handler(message: Message, bot: TeleBot) -> None:
temp_file.write(downloaded_file) temp_file.write(downloaded_file)
model = genai.GenerativeModel("gemini-pro-vision") model = genai.GenerativeModel("gemini-pro-vision")
image_path = Path("gemini_temp.jpg") with open("gemini_temp.jpg", "rb") as image_file:
image_data = image_path.read_bytes() image_data = image_file.read()
contents = { contents = {
"parts": [{"mime_type": "image/jpeg", "data": image_data}, {"text": prompt}] "parts": [{"mime_type": "image/jpeg", "data": image_data}, {"text": prompt}]
} }