import re import os import json import logging import requests from PyPDF2 import PdfReader from datetime import datetime from django.db import transaction from .utils import create_zoom_meeting from recruitment.models import Application from . linkedin_service import LinkedInService from django.shortcuts import get_object_or_404 from . models import JobPosting from django.utils import timezone from django.template.loader import render_to_string from . models import BulkInterviewTemplate,Interview,Message,ScheduledInterview from django.contrib.auth import get_user_model from .utils import get_setting User = get_user_model() # Add python-docx import for Word document processing try: from docx import Document DOCX_AVAILABLE = True except ImportError: DOCX_AVAILABLE = False logger = logging.getLogger(__name__) logger.warning("python-docx not available. Word document processing will be disabled.") logger = logging.getLogger(__name__) OPENROUTER_API_URL = get_setting('OPENROUTER_API_URL') OPENROUTER_API_KEY = get_setting('OPENROUTER_API_KEY') OPENROUTER_MODEL = get_setting('OPENROUTER_MODEL') # OPENROUTER_API_KEY ='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' # OPENROUTER_MODEL = 'qwen/qwen-2.5-72b-instruct' # OPENROUTER_MODEL = 'qwen/qwen-2.5-7b-instruct' # OPENROUTER_MODEL = 'openai/gpt-oss-20b' # OPENROUTER_MODEL = 'mistralai/mistral-small-3.2-24b-instruct:free' # https://openrouter.ai/api/v1/chat/completions # from google import genai # client = genai.Client(api_key="AIzaSyDkwYmvRe5ieTjQi1ClSzD5z5roTwaFsmY") # def google_ai(text): # response = client.models.generate_content( # model="gemini-2.5-flash", contents=text # ) # return response if not OPENROUTER_API_KEY: logger.warning("OPENROUTER_API_KEY not set. Resume scoring will be skipped.") def extract_text_from_pdf(file_path): """Extract text from PDF files""" print("PDF text extraction") text = "" try: with open(file_path, "rb") as f: reader = PdfReader(f) for page in reader.pages: text += (page.extract_text() or "") except Exception as e: logger.error(f"PDF extraction failed: {e}") raise return text.strip() def extract_text_from_word(file_path): """Extract text from Word documents (.docx)""" if not DOCX_AVAILABLE: raise ImportError("python-docx is not installed. Please install it with: pip install python-docx") print("Word text extraction") text = "" try: doc = Document(file_path) # Extract text from paragraphs for paragraph in doc.paragraphs: text += paragraph.text + "\n" # Extract text from tables for table in doc.tables: for row in table.rows: for cell in row.cells: text += cell.text + "\t" text += "\n" # Extract text from headers and footers for section in doc.sections: # Header if section.header: for paragraph in section.header.paragraphs: text += "[HEADER] " + paragraph.text + "\n" # Footer if section.footer: for paragraph in section.footer.paragraphs: text += "[FOOTER] " + paragraph.text + "\n" except Exception as e: logger.error(f"Word extraction failed: {e}") raise return text.strip() def extract_text_from_document(file_path): """Extract text from documents based on file type""" if not os.path.exists(file_path): raise FileNotFoundError(f"File not found: {file_path}") file_ext = os.path.splitext(file_path)[1].lower() if file_ext == '.pdf': return extract_text_from_pdf(file_path) elif file_ext == '.docx': return extract_text_from_word(file_path) else: raise ValueError(f"Unsupported file type: {file_ext}. Only .pdf and .docx files are supported.") def format_job_description(pk): job_posting = JobPosting.objects.get(pk=pk) print(job_posting) prompt = f""" You are a dual-purpose AI assistant specializing in content formatting and social media copywriting for job announcements. **JOB POSTING DATA (Raw Input):** --- **JOB DESCRIPTION:** {job_posting.description} **QUALIFICATIONS:** {job_posting.qualifications} **BENEFITS:** {job_posting.benefits} **APPLICATION INSTRUCTIONS:** {job_posting.application_instructions} **APPLICATION DEADLINE:** {job_posting.application_deadline} **HASHTAGS: for search and reach:** {job_posting.hash_tags} **APPLICATION URL: for career page only if it is provided** {job_posting.application_url} --- **TASK 1: HTML Formatting (Two Blocks)** 1. **Format the Job Description:** Organize and format the raw JOB DESCRIPTION and BENEFITS data into clear, readable sections using `

` headings and `