-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_handler.py
More file actions
42 lines (36 loc) · 1.35 KB
/
image_handler.py
File metadata and controls
42 lines (36 loc) · 1.35 KB
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
from llama_cpp import Llama
from llama_cpp.llama_chat_format import Llava15ChatHandler
import base64
from utils import load_config
#import streamlit as st
config = load_config()
def convert_bytes_to_base64(image_bytes):
encoded_string= base64.b64encode(image_bytes).decode("utf-8")
return "data:image/jpeg;base64," + encoded_string
#@st.cache_resource # can be cached if you use it often
def load_llava():
chat_handler = Llava15ChatHandler(clip_model_path=config["llava_model"]["clip_model_path"])
llm = Llama(
model_path=config["llava_model"]["llava_model_path"],
chat_handler=chat_handler,
logits_all=True,
n_ctx=1024 # n_ctx should be increased to accomodate the image embedding
)
return llm
def handle_image(image_bytes, user_message):
llava = load_llava()
image_base64 = convert_bytes_to_base64(image_bytes)
output = llava.create_chat_completion(
messages = [
{"role": "system", "content": "You are an assistant who perfectly describes images."},
{
"role": "user",
"content": [
{"type": "image_url", "image_url": {"url": image_base64}},
{"type" : "text", "text": user_message}
]
}
]
)
print(output)
return output["choices"][0]["message"]["content"]