File size: 6,169 Bytes
61c1a82
 
d340145
61c1a82
 
 
 
 
 
 
 
d340145
61c1a82
 
d340145
61c1a82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d340145
 
 
 
61c1a82
d340145
 
 
 
 
 
 
 
 
61c1a82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d340145
61c1a82
 
d340145
61c1a82
 
 
 
 
d340145
 
 
61c1a82
 
 
 
 
 
 
 
d340145
61c1a82
d340145
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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
from nltk.corpus import words
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")

english_words = set(words.words())

# Your hardcoded API key
api_key ="AIzaSyCG-qpFRqJc0QOJT-AcAaO5XIEdE-nk3Tc"

# Function to authenticate with Gemini API
def authenticate_gemini(api_key):
    try:
        genai.configure(api_key=api_key)
        model = genai.GenerativeModel(model_name="gemini-1.5-flash-latest")
        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("Resume Analyzer")
    st.write("Upload a resume to extract information")

    # 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:
            # Authenticate with Google Gemini API
            model = authenticate_gemini(api_key)
            if model is None:
                return

            # 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.write("Generated Summary")
            st.write(summary)

        except Exception as e:
            st.error(f"Error during processing: {e}")

if __name__ == "__main__":
    main()