Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -38,11 +38,9 @@ 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
|
| 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,31 +61,23 @@ if uploaded_file is not None:
|
|
| 63 |
else:
|
| 64 |
image_path = uploaded_file
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 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,7 +115,7 @@ if uploaded_file is not None:
|
|
| 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,7 +128,7 @@ if uploaded_file is not None:
|
|
| 138 |
st.write(output_text[0])
|
| 139 |
|
| 140 |
# Clean up temporary file
|
| 141 |
-
if image_source == "Upload an image"
|
| 142 |
os.remove("temp_image.png")
|
| 143 |
else:
|
| 144 |
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 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 |
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 |
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 |
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.")
|