Skip to content

refactor: move inline OCR imports to module level with a global availability flag (#591)#647

Open
suhaniiz wants to merge 3 commits into
param20h:devfrom
suhaniiz:refactor-ocr-imports-591
Open

refactor: move inline OCR imports to module level with a global availability flag (#591)#647
suhaniiz wants to merge 3 commits into
param20h:devfrom
suhaniiz:refactor-ocr-imports-591

Conversation

@suhaniiz

Copy link
Copy Markdown
Contributor

📋 PR Checklist

Thank you for contributing to PDF-Assistant-RAG! 🎉
Please fill out this template before submitting. PRs without it filled in will be closed.


🔗 Related Issue

Closes #591


📝 What does this PR do?

Optimizes performance and readability within the image captioning module by refactoring how OCR dependencies are managed.

  • Moved the expensive inline PIL and pytesseract imports out of the _ocr_caption function loop and up to the module initialization level.
  • Implemented a top-level global boolean flag HAS_OCR to handle conditional dependency loading safely at runtime.
  • Updated _ocr_caption to immediately evaluate the state of HAS_OCR rather than spinning up repetitive try/except execution paths on every function invocation. This removes unnecessary interpreter overhead during long document batch processing routines.

🗂️ Type of Change

  • 🐛 Bug fix
  • ✨ New feature
  • 🔧 Refactor / code cleanup
  • 📝 Documentation update
  • 🎨 UI / styling change
  • ⚙️ CI / tooling / config change
  • 🧪 Tests

🧪 How was this tested?

  • Verified error safety handling locally when target libraries are absent vs. present.
  • Confirmed function behavior maintains standard string fallbacks cleanly without altering functional outputs.

📸 Screenshots (if UI change)

N/A


⚠️ Anything to flag for reviewers?

None. This change does not alter the external API surface or functionality of caption_image.


✅ Self-Review Checklist

  • My branch is based on dev, not main
  • I have not added any secrets / API keys
  • I have not modified main branch or any HuggingFace deployment config
  • My code follows the existing style (no unnecessary formatting changes)
  • I have updated relevant docs / comments if needed

@suhaniiz suhaniiz requested a review from param20h as a code owner June 19, 2026 19:44
@suhaniiz

Copy link
Copy Markdown
Contributor Author

Hi @param20h,

Since I am the author who originally opened this issue (#591), I have a deep understanding of the performance overhead we wanted to solve here.

I've successfully isolated the heavy inline imports up to the module level and tied them to a clean HAS_OCR global flag initialization. This completely eliminates the repetitive try/except overhead during batch chunking while keeping the fallback safe.

Please take this PR into consideration—it perfectly bridges the issue I highlighted. Ready for review! 🚀

@suhaniiz

Copy link
Copy Markdown
Contributor Author

@param20h

param20h
param20h previously approved these changes Jun 21, 2026
@param20h

Copy link
Copy Markdown
Owner

merge conflicts'

@suhaniiz

Copy link
Copy Markdown
Contributor Author
image

i cannot see the conflicts to be resolved!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] refactor: move inline OCR imports to module level with a global availability flag

2 participants