vilarin commited on
Commit
7d8bfa5
Β·
verified Β·
1 Parent(s): 2402abf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -8
app.py CHANGED
@@ -5,7 +5,7 @@ import os
5
  from huggingface_hub import hf_hub_download
6
  import base64
7
  from llama_cpp import Llama
8
- from llama_cpp.llama_chat_format import Llava16ChatHandler
9
 
10
 
11
  os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
@@ -27,18 +27,23 @@ CSS = """
27
  }
28
  """
29
 
30
- # chat_handler = Llava16ChatHandler.from_pretrained(
31
- # repo_id="openbmb/MiniCPM-Llama3-V-2_5-gguf",
32
- # filename="*mmproj*",
33
- # )
34
 
35
  llm = Llama.from_pretrained(
36
  repo_id="openbmb/MiniCPM-Llama3-V-2_5-gguf",
37
  filename="ggml-model-Q5_K_M.gguf",
38
- # chat_handler=chat_handler,
39
  n_ctx=2048, # n_ctx should be increased to accommodate the image embedding
40
  )
41
 
 
 
 
 
 
42
  @spaces.GPU(queue=False)
43
  def stream_chat(message, history: list, temperature: float, max_new_tokens: int):
44
  print(f'message is - {message}')
@@ -46,7 +51,7 @@ def stream_chat(message, history: list, temperature: float, max_new_tokens: int)
46
  messages = []
47
 
48
  if message["files"]:
49
- image = Image.open(message["files"][-1]).convert('RGB')
50
  messages.append({
51
  "role": "user",
52
  "content": [
@@ -59,7 +64,7 @@ def stream_chat(message, history: list, temperature: float, max_new_tokens: int)
59
  raise gr.Error("Please upload an image first.")
60
  image = None
61
  else:
62
- image = Image.open(history[0][0][0])
63
  for prompt, answer in history:
64
  if answer is None:
65
  messages.extend([{
 
5
  from huggingface_hub import hf_hub_download
6
  import base64
7
  from llama_cpp import Llama
8
+ from llama_cpp.llama_chat_format import NanoLlavaChatHandler
9
 
10
 
11
  os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
 
27
  }
28
  """
29
 
30
+ chat_handler = NanoLlavaChatHandler.from_pretrained(
31
+ repo_id="openbmb/MiniCPM-Llama3-V-2_5-gguf",
32
+ filename="*mmproj*",
33
+ )
34
 
35
  llm = Llama.from_pretrained(
36
  repo_id="openbmb/MiniCPM-Llama3-V-2_5-gguf",
37
  filename="ggml-model-Q5_K_M.gguf",
38
+ chat_handler=chat_handler,
39
  n_ctx=2048, # n_ctx should be increased to accommodate the image embedding
40
  )
41
 
42
+ def image_to_base64_data_uri(file_path):
43
+ with open(file_path, "rb") as img_file:
44
+ base64_data = base64.b64encode(img_file.read()).decode('utf-8')
45
+ return f"data:image/png;base64,{base64_data}"
46
+
47
  @spaces.GPU(queue=False)
48
  def stream_chat(message, history: list, temperature: float, max_new_tokens: int):
49
  print(f'message is - {message}')
 
51
  messages = []
52
 
53
  if message["files"]:
54
+ image = image_to_base64_data_uri(message["files"][-1])
55
  messages.append({
56
  "role": "user",
57
  "content": [
 
64
  raise gr.Error("Please upload an image first.")
65
  image = None
66
  else:
67
+ image = image_to_base64_data_uri(history[0][0][0])
68
  for prompt, answer in history:
69
  if answer is None:
70
  messages.extend([{