Views
No views yet
liuhaotian/llava-v1.5-7b on ocr and object detection data using LoRA (adapter config at bottom) to improve OCR captioning abilities and bounding box generation.1from llama_cpp import Llama
2from llama_cpp.llama_chat_format import Llava15ChatHandler
3import io
4from PIL import Image
5import base64
6import os
7import json
8import argparse
9import time
10from pathlib import Path
11
12chat_handler = Llava15ChatHandler(clip_model_path="your-path-to-mmproj-model-f16.gguf")
13
14llm = Llama(
15 model_path="your-path-to-llava-v1.5-7b-ocr-pretrain.Q4_K_M.gguf",
16 chat_handler=chat_handler,
17 n_ctx=2048, # n_ctx should be increased to accomodate the image embedding
18 logits_all=True,# needed to make llava work
19 n_gpu_layers=-1,
20)
21
22def inference(url, question):
23 start = time.perf_counter()
24 output = llm.create_chat_completion(
25 messages = [
26 {"role": "system", "content": "You are an assistant who answers user questions"},
27 {
28 "role": "user",
29 "content": [
30 {"type": "image_url",
31 "image_url": {
32 "url": url
33 }
34 },
35 {"type" : "text", "text": question}
36 ]
37 }
38 ],
39 temperature=1.0,
40 )
41 stop = time.perf_counter()
42
43 return {
44 **output,
45 "completion_time": stop-start,
46 "tokens-per-second": output["usage"]["completion_tokens"]/(stop-start),
47 }
48
49
50def img_base64(path):
51 ext = path.suffix
52 path = str(path)
53 with open(str(path), 'rb') as f:
54 data = f.read()
55 return f'data:image/{ext};base64,' + base64.b64encode(data).decode('utf8')
56
57if __name__ == "__main__":
58 parser = argparse.ArgumentParser()
59 parser.add_argument('--url', help="url of an image for inference", type=str, default = "https://adquick-public.imgix.net/landing+images/media_formats/billboard-carvana.png?auto=format")
60 parser.add_argument('--question', '-q', type=str, default="generate a descriptive caption for this image.")
61
62 args = parser.parse_args()
63 url = args.url
64
65 # hope this works for local images
66 if url.startswith('/') or url.startswith('./') or url.startswith("../"):
67 url = img_base64(Path(url))
68
69 # print(url)
70
71 outputs = inference(url, args.question)
72 print(json.dumps(outputs, indent=4))The image depicts an advertisement billboard against a blue sky backdrop, displaying an orange car being transported on a flatbed truck emblazoned with the word \"CARVANA.\" Below the central message reading \"BUY YOUR NEXT CAR FROM YOUR COUCH. CARVANA,\" the text \"Carvana\" is prominently displayed in both yellow and white fonts, while a small \"e\" logo is also noticeable at the bottom right corner of the billboard.