clayton07 commited on
Commit
96e35ac
·
verified ·
1 Parent(s): 080848a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -20
app.py CHANGED
@@ -38,9 +38,11 @@ st.warning("⚠️ Disclaimer: This app is currently running on CPU, which may r
38
  # Add download link
39
  st.markdown("[📥 Download the app code](https://huggingface.co/spaces/clayton07/colpali-qwen2-ocr/blob/main/app.py)")
40
 
41
- # Initialize session state for tracking if index is created
42
  if 'index_created' not in st.session_state:
43
  st.session_state.index_created = False
 
 
44
 
45
  # File uploader
46
  image_source = st.radio("Choose image source:", ("Upload an image", "Use example image"))
@@ -61,23 +63,31 @@ if uploaded_file is not None:
61
  else:
62
  image_path = uploaded_file
63
 
64
- if not st.session_state.index_created:
65
- # Initialize the index for the first image
66
- RAG.index(
67
- input_path=image_path,
68
- index_name="temp_index",
69
- store_collection_with_index=False,
70
- overwrite=True
71
- )
72
- st.session_state.index_created = True
73
- else:
74
- # Add to the existing index for subsequent images
75
- RAG.add_to_index(
76
- input_item=image_path,
77
- store_collection_with_index=False
78
- )
79
-
80
- st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
 
 
 
 
 
 
 
 
81
 
82
  # Text query input
83
  text_query = st.text_input("Enter your query about the image:")
@@ -115,7 +125,7 @@ if uploaded_file is not None:
115
  return_tensors="pt",
116
  )
117
  inputs = inputs.to(device)
118
- generated_ids = model.generate(**inputs, max_new_tokens=max_new_tokens) # Using the slider value here
119
  generated_ids_trimmed = [
120
  out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
121
  ]
@@ -128,7 +138,7 @@ if uploaded_file is not None:
128
  st.write(output_text[0])
129
 
130
  # Clean up temporary file
131
- if image_source == "Upload an image":
132
  os.remove("temp_image.png")
133
  else:
134
  st.write("Please upload an image to get started.")
 
38
  # Add download link
39
  st.markdown("[📥 Download the app code](https://huggingface.co/spaces/clayton07/colpali-qwen2-ocr/blob/main/app.py)")
40
 
41
+ # Initialize session state
42
  if 'index_created' not in st.session_state:
43
  st.session_state.index_created = False
44
+ if 'processed_images' not in st.session_state:
45
+ st.session_state.processed_images = set()
46
 
47
  # File uploader
48
  image_source = st.radio("Choose image source:", ("Upload an image", "Use example image"))
 
63
  else:
64
  image_path = uploaded_file
65
 
66
+ # Check if this image has been processed before
67
+ if image_path not in st.session_state.processed_images:
68
+ with st.spinner('Processing image...'):
69
+ if not st.session_state.index_created:
70
+ # Initialize the index for the first image
71
+ RAG.index(
72
+ input_path=image_path,
73
+ index_name="temp_index",
74
+ store_collection_with_index=False,
75
+ overwrite=True
76
+ )
77
+ st.session_state.index_created = True
78
+ st.success('Index created successfully!')
79
+ else:
80
+ # Add to the existing index for subsequent images
81
+ RAG.add_to_index(
82
+ input_item=image_path,
83
+ store_collection_with_index=False
84
+ )
85
+ st.success('Image added to index successfully!')
86
+
87
+ # Mark this image as processed
88
+ st.session_state.processed_images.add(image_path)
89
+
90
+ st.image(image_path, caption="Uploaded Image", use_column_width=True)
91
 
92
  # Text query input
93
  text_query = st.text_input("Enter your query about the image:")
 
125
  return_tensors="pt",
126
  )
127
  inputs = inputs.to(device)
128
+ generated_ids = model.generate(**inputs, max_new_tokens=max_new_tokens)
129
  generated_ids_trimmed = [
130
  out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
131
  ]
 
138
  st.write(output_text[0])
139
 
140
  # Clean up temporary file
141
+ if image_source == "Upload an image" and os.path.exists("temp_image.png"):
142
  os.remove("temp_image.png")
143
  else:
144
  st.write("Please upload an image to get started.")