Antoine Chaffin commited on
Commit
7988f40
·
1 Parent(s): 349b5c2

Adding Zero decorators

Browse files
Files changed (2) hide show
  1. app.py +10 -1
  2. model.py +4 -2
app.py CHANGED
@@ -1,13 +1,21 @@
 
1
  import uuid
2
 
3
  import gradio as gr
 
4
  import torch
5
  from qwen_vl_utils import process_vision_info
6
  from transformers import AutoProcessor, Qwen2VLForConditionalGeneration
7
  from voyager_index import Voyager
8
 
 
 
 
 
 
 
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
- device = "cpu"
11
 
12
  # Initialize the model and processor
13
  model = (
@@ -32,6 +40,7 @@ def add_to_index(files, index):
32
  return f"Added {len(files)} files to the index."
33
 
34
 
 
35
  def query_index(query, index):
36
  res = index(query, k=1)
37
  retrieved_image = res["documents"][0][0]["image"]
 
1
+ import subprocess
2
  import uuid
3
 
4
  import gradio as gr
5
+ import spaces
6
  import torch
7
  from qwen_vl_utils import process_vision_info
8
  from transformers import AutoProcessor, Qwen2VLForConditionalGeneration
9
  from voyager_index import Voyager
10
 
11
+ subprocess.run(
12
+ "pip install flash-attn --no-build-isolation",
13
+ env={"FLASH_ATTENTION_SKIP_CUDA_BUILD": "TRUE"},
14
+ shell=True,
15
+ )
16
+
17
  device = "cuda" if torch.cuda.is_available() else "cpu"
18
+
19
 
20
  # Initialize the model and processor
21
  model = (
 
40
  return f"Added {len(files)} files to the index."
41
 
42
 
43
+ @spaces.GPU
44
  def query_index(query, index):
45
  res = index(query, k=1)
46
  retrieved_image = res["documents"][0][0]["image"]
model.py CHANGED
@@ -1,13 +1,13 @@
 
1
  import torch
2
  from PIL import Image
3
  from qwen_vl_utils import process_vision_info
4
  from transformers import AutoProcessor, Qwen2VLForConditionalGeneration
5
 
6
  device = "cuda" if torch.cuda.is_available() else "cpu"
7
- # device = "cpu"
8
 
9
  min_pixels = 1 * 28 * 28
10
- max_pixels = 256 * 28 * 28 # 2560 * 28 * 28
11
 
12
 
13
  processor = AutoProcessor.from_pretrained(
@@ -35,6 +35,7 @@ def get_embedding(last_hidden_state: torch.Tensor, dimension: int):
35
  return reps.to(torch.float32).cpu().numpy()
36
 
37
 
 
38
  def encode_queries(queries: list):
39
  if isinstance(queries, str):
40
  queries = [queries]
@@ -77,6 +78,7 @@ def encode_queries(queries: list):
77
  return query_embeddings
78
 
79
 
 
80
  def encode_images(images: list):
81
  if isinstance(images, Image.Image):
82
  images = [images]
 
1
+ import spaces
2
  import torch
3
  from PIL import Image
4
  from qwen_vl_utils import process_vision_info
5
  from transformers import AutoProcessor, Qwen2VLForConditionalGeneration
6
 
7
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
8
 
9
  min_pixels = 1 * 28 * 28
10
+ max_pixels = 2560 * 28 * 28
11
 
12
 
13
  processor = AutoProcessor.from_pretrained(
 
35
  return reps.to(torch.float32).cpu().numpy()
36
 
37
 
38
+ @spaces.GPU
39
  def encode_queries(queries: list):
40
  if isinstance(queries, str):
41
  queries = [queries]
 
78
  return query_embeddings
79
 
80
 
81
+ @spaces.GPU
82
  def encode_images(images: list):
83
  if isinstance(images, Image.Image):
84
  images = [images]