-
Notifications
You must be signed in to change notification settings - Fork 1
/
mistral_files.py
54 lines (47 loc) · 1.52 KB
/
mistral_files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import base64
from io import BytesIO
from PIL import Image
import prompts
from mistralai import (
Mistral,
UserMessage,
SystemMessage,
)
PIXTRAL_TEMPERATURE = 0.3
def encode_image_base64(image_file):
img = Image.open(image_file).convert("RGB")
img = img.resize((512, int(512 * img.height / img.width)))
img_bytes = BytesIO()
img.save(img_bytes, format="JPEG")
img_bytes.seek(0)
return base64.b64encode(img_bytes.read()).decode("utf-8")
def handle_files(files: list, client: Mistral, model: str):
content = []
for file in files:
if file.type.startswith("image/jpeg"):
list_image = encode_image_base64(file)
content.append(
{
"type": "image_url",
"image_url": f"data:image/jpeg;base64,{list_image}",
}
)
elif file.type.startswith("image/png"):
list_image = encode_image_base64(file)
content.append(
{
"type": "image_url",
"image_url": f"data:image/png;base64,{list_image}",
}
)
system_messages = SystemMessage(
content=prompts.render_template_from_file("prompts/picture_information.md")
)
picture_messages = UserMessage(content=content)
chat_response = client.chat.complete(
model=model,
temperature=PIXTRAL_TEMPERATURE,
messages=[system_messages, picture_messages],
timeout_ms=60000,
)
return chat_response