Make me a script that can query this data and finds questions which aren't associated…
- User
Make me a script that can query this data and finds questions which aren't associated with any answers. Then lists them exam wise and asks user if they want to fill it. :::::# main.py
import os import re import logging import json from datetime import datetime from dotenv import load_dotenv import psycopg2 from psycopg2 import sql import fitz # PyMuPDF from pdfminer.high_level import extract_text as pdfminer_extract_text from pdfminer.pdfpage import PDFPage from pdf2image import convert_from_path import pytesseract from PIL import Image import fasttext # Optional: Install via
pip install fasttextimport pdfplumber import pandas as pd from pathlib import PathLoad environment variables from .env file
load_dotenv()
Configure logging
logging.basicConfig( filename='exam_data_import.log', filemode='a', level=logging.DEBUG, # Set to DEBUG for detailed logs format='%(asctime)s:%(levelname)s:%(message)s' )
Database connection parameters from environment variables
DB_PARAMS = { 'dbname': os.getenv('DB_NAME'), 'user': os.getenv('DB_USER'), 'password': os.getenv('DB_PASSWORD'), 'host': os.getenv('DB_HOST'), 'port': os.getenv('DB_PORT') }
Paths from environment variables
DATA_DIR = Path(os.getenv('DATA_DIR', './data')) POPPLER_PATH = os.getenv('POPPLER_PATH') TESSERACT_CMD = os.getenv('TESSERACT_CMD') OUTPUT_DIR = Path(os.getenv('OUTPUT_DIR', './output')) FASTTEXT_MODEL_PATH = os.getenv('FASTTEXT_MODEL_PATH') # e.g., 'models/lid.176.bin' PAGES_CONFIG_PATH = Path('pages_config.json') # Path to pages configuration
Ensure output directory exists
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
Configure Tesseract OCR
pytesseract.pytesseract.tesseract_cmd = TESSERACT_CMD
Optional: Initialize FastText model for language detection if improved accuracy is needed
if FASTTEXT_MODEL_PATH and Path(FASTTEXT_MODEL_PATH).exists(): try: fasttext_model = fasttext.load_model(FASTTEXT_MODEL_PATH) logging.info("FastText language detection model loaded successfully.") except Exception as e: fasttext_model = None logging.error(f"Failed to load FastText model: {e}") else: fasttext_model = None logging.warning("FastText model not found. Proceeding without FastText language detection.")
def verify_tesseract_languages(): """ Verify that Tesseract has the required languages installed. If Gujarati ('guj') is not installed, provide instructions. """ try: langs = pytesseract.get_languages(config='') if 'guj' not in langs: logging.error("Gujarati language data ('guj') not found in Tesseract.") print("Error: Gujarati language data ('guj') not found in Tesseract.") print("Please install it by following the instructions below:") print("For Ubuntu/Debian:") print(" sudo apt-get install tesseract-ocr-guj") print("For Windows:") print(" Download 'guj.traineddata' from https://github.com/tesseract-ocr/tessdata and place it in your tessdata directory.") return False else: logging.info("Gujarati language data ('guj') is installed in Tesseract.") return True except Exception as e: logging.error(f"Failed to verify Tesseract languages: {e}") print(f"Failed to verify Tesseract languages: {e}") return False
- Assistant