gauri-sharan commited on
Commit
d7c725d
·
verified ·
1 Parent(s): b384e01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -11
app.py CHANGED
@@ -7,14 +7,13 @@ from PIL import Image
7
  import os
8
  import traceback
9
  import spaces
10
- import time
11
 
12
  # Check if CUDA is available
13
  device = "cuda" if torch.cuda.is_available() else "cpu"
14
  print(f"Using device: {device}")
15
 
16
  # Load the Byaldi and Qwen2-VL models
17
- rag_model = RAGMultiModalModel.from_pretrained("vidore/colpali") # Do not move Byaldi to GPU
18
  qwen_model = Qwen2VLForConditionalGeneration.from_pretrained(
19
  "Qwen/Qwen2-VL-7B-Instruct", trust_remote_code=True, torch_dtype=torch.bfloat16
20
  ).to(device) # Move Qwen2-VL to GPU
@@ -29,15 +28,12 @@ def ocr_and_extract(image):
29
  temp_image_path = "temp_image.jpg"
30
  image.save(temp_image_path)
31
 
32
- # Generate a unique index name using the current timestamp
33
- unique_index_name = f"image_index_{int(time.time())}"
34
-
35
- # Index the image with Byaldi
36
  rag_model.index(
37
  input_path=temp_image_path,
38
- index_name=unique_index_name, # Use the unique index name
39
  store_collection_with_index=False,
40
- overwrite=True # Ensure the index is overwritten if it already exists
41
  )
42
 
43
  # Perform the search query on the indexed image
@@ -79,9 +75,6 @@ def ocr_and_extract(image):
79
  # Clean up the temporary file
80
  os.remove(temp_image_path)
81
 
82
- # Clear the index after processing each image to avoid conflicts on future uploads
83
- rag_model.clear_index(unique_index_name)
84
-
85
  return "\n".join(filtered_output).strip()
86
 
87
  except Exception as e:
 
7
  import os
8
  import traceback
9
  import spaces
 
10
 
11
  # Check if CUDA is available
12
  device = "cuda" if torch.cuda.is_available() else "cpu"
13
  print(f"Using device: {device}")
14
 
15
  # Load the Byaldi and Qwen2-VL models
16
+ rag_model = RAGMultiModalModel.from_pretrained("vidore/colpali") # Byaldi model
17
  qwen_model = Qwen2VLForConditionalGeneration.from_pretrained(
18
  "Qwen/Qwen2-VL-7B-Instruct", trust_remote_code=True, torch_dtype=torch.bfloat16
19
  ).to(device) # Move Qwen2-VL to GPU
 
28
  temp_image_path = "temp_image.jpg"
29
  image.save(temp_image_path)
30
 
31
+ # Index the image with Byaldi, and force overwrite of the existing index
 
 
 
32
  rag_model.index(
33
  input_path=temp_image_path,
34
+ index_name="image_index", # Reuse the same index
35
  store_collection_with_index=False,
36
+ overwrite=True # Overwrite the index for every new image
37
  )
38
 
39
  # Perform the search query on the indexed image
 
75
  # Clean up the temporary file
76
  os.remove(temp_image_path)
77
 
 
 
 
78
  return "\n".join(filtered_output).strip()
79
 
80
  except Exception as e: