Spaces:
Runtime error
Runtime error
Update app.py
Browse files3 unmodified lines4567891011123 unmodified lines1617181920212223242526272829303132333435363738394041424344453 unmodified linesfrom PIL import Imagefrom byaldi import RAGMultiModalModelfrom qwen_vl_utils import process_vision_info# Load ColPali modelRAG = RAGMultiModalModel.from_pretrained("vidore/colpali")processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct")3 unmodified linesvlm = load_model()def ocr_image(image, keyword=""):# Convert PIL Image to file-like objectimport ioimg_byte_arr = io.BytesIO()image.save(img_byte_arr, format='PNG')img_byte_arr = img_byte_arr.getvalue()# Index the imageRAG.index(input_data=img_byte_arr, index_name="temp_index", overwrite=True)# Retrieve text from the imageresults = RAG.search("Extract all text from this image", k=1)# Extract text from resultsoutput_text = results[0].get('text', '')if keyword:keyword_lower = keyword.lower()if keyword_lower in output_text.lower():highlighted_text = output_text.replace(keyword, f"**{keyword}**")return f"Keyword '{keyword}' found in the text:\n\n{highlighted_text}"else:return f"Keyword '{keyword}' not found in the text:\n\n{output_text}"else:return output_textdef process_image(image, keyword=""):max_size = 10243 unmodified lines45678910111213143 unmodified lines1819202122232425262728293031323334353637383940414243444546474849503 unmodified linesfrom PIL import Imagefrom byaldi import RAGMultiModalModelfrom qwen_vl_utils import process_vision_infoimport osimport tempfile# Load ColPali modelRAG = RAGMultiModalModel.from_pretrained("vidore/colpali", device_map="cpu", torch_dtype=torch.float32)processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct")3 unmodified linesvlm = load_model()def ocr_image(image, keyword=""):# Save the image to a temporary filewith tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:image.save(temp_file, format='PNG')temp_file_path = temp_file.nametry:# Index the imageRAG.index(input_path=temp_file_path, index_name="temp_index", overwrite=True)# Retrieve text from the imageresults = RAG.search("Extract all text from this image", k=1)# Extract text from resultsoutput_text = results[0].get('text', '')if keyword:keyword_lower = keyword.lower()if keyword_lower in output_text.lower():highlighted_text = output_text.replace(keyword, f"**{keyword}**")return f"Keyword '{keyword}' found in the text:\n\n{highlighted_text}"else:return f"Keyword '{keyword}' not found in the text:\n\n{output_text}"else:return output_textfinally:# Clean up the temporary fileos.unlink(temp_file_path)def process_image(image, keyword=""):max_size = 1024
app.py+29-24