import google.generativeai as genai import fitz # PyMuPDF for PDF text extraction import streamlit as st import spacy from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline from docx import Document import re import dateparser from datetime import datetime import os # Load SpaCy model for dependency parsing nlp_spacy = spacy.load('en_core_web_sm') # Load the NER model tokenizer = AutoTokenizer.from_pretrained("Babelscape/wikineural-multilingual-ner") model = AutoModelForTokenClassification.from_pretrained("Babelscape/wikineural-multilingual-ner") nlp_ner = pipeline('ner', model=model, tokenizer=tokenizer, aggregation_strategy="simple") # Function to authenticate with Gemini API def authenticate_gemini(): api_key = os.environ.get("GOOGLE_GEMINI_API_KEY") if not api_key: st.error("Google Gemini API key not found. Please set it in the Hugging Face Spaces secrets.") return None try: genai.configure(api_key=api_key) model = genai.GenerativeModel(model_name="gemini-pro") st.success("Gemini API successfully configured.") return model except Exception as e: st.error(f"Error configuring Gemini API: {e}") return None # Function to filter and refine extracted ORG entities def refine_org_entities(entities): refined_entities = set() company_suffixes = ['Inc', 'LLC', 'Corporation', 'Corp', 'Ltd', 'Co', 'GmbH', 'S.A.'] for entity in entities: if any(entity.endswith(suffix) for suffix in company_suffixes): refined_entities.add(entity) elif re.match(r'([A-Z][a-z]+)\s([A-Z][a-z]+)', entity): refined_entities.add(entity) return list(refined_entities) # Function to extract ORG entities using NER def extract_orgs(text): ner_results = nlp_ner(text) orgs = set() for entity in ner_results: if entity['entity_group'] == 'ORG': orgs.add(entity['word']) return refine_org_entities(orgs) # Extract text from PDF def extract_text_from_pdf(pdf_file): doc = fitz.open(stream=pdf_file.read(), filetype="pdf") text = "" for page_num in range(doc.page_count): page = doc.load_page(page_num) text += page.get_text() return text # Extract text from DOCX def extract_text_from_doc(doc_file): doc = Document(doc_file) text = '\n'.join([para.text for para in doc.paragraphs]) return text # Summary generation function def generate_summary(text, model): prompt = f"Can you summarize the following document in 100 words?\n\n{text}" try: response = model.generate_content(prompt) return response.text except Exception as e: return f"Error generating summary: {str(e)}" # Additional resume parsing functions def extract_experience(doc): experience = 0 for ent in doc.ents: if ent.label_ == "DATE": date = dateparser.parse(ent.text) if date: experience = max(experience, datetime.now().year - date.year) return experience def extract_phone(text): phone_patterns = [ r'\b(?:\+?1[-.\s]?)?(?:\(\d{3}\)|\d{3})[-.\s]?\d{3}[-.\s]?\d{4}\b', r'\b\d{3}[-.\s]?\d{3}[-.\s]?\d{4}\b' ] for pattern in phone_patterns: match = re.search(pattern, text) if match: return match.group() return "Not found" def extract_email(text): email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b' match = re.search(email_pattern, text) return match.group() if match else "Not found" def extract_colleges(doc): colleges = set() edu_keywords = ["university", "college", "institute", "school"] for ent in doc.ents: if ent.label_ == "ORG" and any(keyword in ent.text.lower() for keyword in edu_keywords): colleges.add(ent.text) return list(colleges) def extract_linkedin(text): linkedin_pattern = r'(?:https?:)?\/\/(?:[\w]+\.)?linkedin\.com\/in\/[A-z0-9_-]+\/?' match = re.search(linkedin_pattern, text) return match.group() if match else "Not found" # Main function to process the resume and return the analysis def main(): st.title("Comprehensive Resume Analyzer") st.write("Upload a resume to extract information, generate a summary, and analyze details.") # Authenticate with Gemini API model = authenticate_gemini() if model is None: return # File uploader for resume input uploaded_file = st.file_uploader("Choose a PDF or DOCX file", type=["pdf", "docx", "doc"]) if uploaded_file is not None: try: # Extract text from the uploaded resume file_ext = uploaded_file.name.split('.')[-1].lower() if file_ext == 'pdf': resume_text = extract_text_from_pdf(uploaded_file) elif file_ext in ['docx', 'doc']: resume_text = extract_text_from_doc(uploaded_file) else: st.error("Unsupported file format.") return if not resume_text.strip(): st.error("The resume appears to be empty.") return # Process the resume doc = nlp_spacy(resume_text) # Extract information companies = extract_orgs(resume_text) summary = generate_summary(resume_text, model) experience = extract_experience(doc) phone = extract_phone(resume_text) email = extract_email(resume_text) colleges = extract_colleges(doc) linkedin = extract_linkedin(resume_text) # Display results st.subheader("Extracted Information") st.write(f"*Years of Experience:* {experience}") st.write("*Companies Worked For:*") st.write(", ".join(companies)) st.write(f"*Phone Number:* {phone}") st.write(f"*Email ID:* {email}") st.write("*Colleges Attended:*") st.write(", ".join(colleges)) st.write(f"*LinkedIn ID:* {linkedin}") st.subheader("Generated Summary") st.write(summary) except Exception as e: st.error(f"Error during processing: {e}") if __name__ == "__main__": main()