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:
temp_file.write(downloaded_file)
f = Path("claude_temp.jpg")
try:
r = client.messages.create(
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt,
},
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": Path("claude_temp.jpg"),
with f:
r = client.messages.create(
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt,
},
},
],
},
],
model="claude-3-opus-20240229",
)
bot.reply_to(message, "Claude vision answer:\n" + r.content[0].text)
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": f,
},
},
],
},
],
model=ANTHROPIC_MODEL,
)
bot.reply_to(message, "Claude vision answer:\n" + r.content[0].text)
except Exception as e:
print(e)
bot.reply_to(

View File

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