Middle
Design a service on FastAPI: it receives a trigger on a schedule, processes PDF documents from Kafka (OCR, entity extraction), and saves the text + entities in Postgres. How would you design the structure?
sobes.tech AI
Answer from AI
For such a service on FastAPI, I would design the architecture as follows:
- Scheduled Trigger
- Use
APSchedulerorCelery Beatto run periodic tasks that initiate processing.
- Processing messages from Kafka
- Create a separate consumer that listens to the topic with PDF documents.
- Upon receiving a message, download or obtain the PDF (if the message contains a link or binary data).
- OCR and entity extraction
- Use Tesseract or cloud services (Google Vision, AWS Textract) for OCR.
- For entity extraction, use NLP libraries (spaCy, DeepPavlov) or custom models.
- Saving to Postgres
- Table
documentswith fields: id, filename, upload date, original text. - Table
entitieswith fields: id, document_id (FK), entity type, value, position in text.
- Project structure
/app
/api.py # FastAPI endpoints
/kafka_consumer.py # Message consumption logic
/ocr.py # OCR functions
/ner.py # Entity extraction
/db.py # Working with Postgres via SQLAlchemy
/scheduler.py # Task scheduler
- Example Kafka consumer code (simplified):
from kafka import KafkaConsumer
import json
consumer = KafkaConsumer('pdf_topic', bootstrap_servers='localhost:9092')
for msg in consumer:
data = json.loads(msg.value)
pdf_path = data['pdf_path']
text = ocr_process(pdf_path)
entities = extract_entities(text)
save_to_db(pdf_path, text, entities)
- FastAPI can be used for monitoring processing status or manually triggering tasks.
Thus, the service will be modular, scalable, and easy to maintain.