File size: 5,470 Bytes
07de611
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import sqlite3
import hashlib
import spacy
from nltk.corpus import stopwords
import re
import docx2txt
import PyPDF2
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

DB_PATH = 'resume_assistant.db'

# Initialize NLP and stop words
nlp = spacy.load("en_core_web_sm")
stop_words = set(stopwords.words("english"))

def initialize_db():
    with sqlite3.connect(DB_PATH) as conn:
        cur = conn.cursor()
        cur.execute('''
            CREATE TABLE IF NOT EXISTS users (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                username TEXT UNIQUE,
                email TEXT UNIQUE,
                password_hash TEXT
            )
        ''')
        conn.commit()

def hash_password(password):
    return hashlib.sha256(password.encode('utf-8')).hexdigest()

def signup_user(username, email, password):
    pwd_hash = hash_password(password)
    try:
        with sqlite3.connect(DB_PATH) as conn:
            cur = conn.cursor()
            cur.execute(
                "INSERT INTO users (username, email, password_hash) VALUES (?, ?, ?)",
                (username, email, pwd_hash)
            )
            conn.commit()
        return True, "Signup successful!"
    except sqlite3.IntegrityError:
        return False, "Username or email already exists."

def verify_login(username, password):
    pwd_hash = hash_password(password)
    with sqlite3.connect(DB_PATH) as conn:
        cur = conn.cursor()
        cur.execute("SELECT id FROM users WHERE username=? AND password_hash=?", (username, pwd_hash))
        data = cur.fetchone()
        return (True, data[0]) if data else (False, None)

def extract_text(file):
    if not file:
        return ""
    if file.type == "application/pdf":
        reader = PyPDF2.PdfReader(file)
        text = ""
        for page in reader.pages:
            text += page.extract_text() or ""
        return text
    elif file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
        return docx2txt.process(file)
    elif file.type == "text/plain":
        return file.getvalue().decode("utf-8")
    return ""

def clean_text(text):
    text = re.sub(r'[^a-zA-Z\s]', '', text.lower())
    doc = nlp(text)
    return set([token.lemma_ for token in doc if token.is_alpha and token.text not in stop_words and len(token.text)>2])

def ats_score(resume_text, jd_text):
    vectorizer = TfidfVectorizer(stop_words='english')
    tfidf = vectorizer.fit_transform([resume_text, jd_text])
    return cosine_similarity(tfidf[0:1], tfidf[1:2])[0]*100

def ml_score_dummy(resume_text, jd_text):
    # Dummy relevance score (just scaled ATS score)
    return ats_score(resume_text, jd_text) * 0.9

def main():
    initialize_db()
    st.title("AI-Powered Resume Assistant")

    menu = ["Login", "Signup"]
    choice = st.sidebar.selectbox("Choose Action", menu)

    if choice == "Signup":
        st.subheader("Create New Account")
        username = st.text_input("Username")
        email = st.text_input("Email")
        password = st.text_input("Password", type="password")
        if st.button("Sign Up"):
            if username and email and password:
                success, msg = signup_user(username, email, password)
                if success:
                    st.success(msg)
                else:
                    st.error(msg)
            else:
                st.warning("Please enter all fields")

    elif choice == "Login":
        st.subheader("Login")
        username = st.text_input("Username")
        password = st.text_input("Password", type="password")
        if st.button("Login"):
            if username and password:
                valid, user_id = verify_login(username, password)
                if valid:
                    st.success(f"Welcome, {username}!")
                    
                    resume_file = st.file_uploader("Upload Resume (.pdf, .docx, .txt)", type=['pdf','docx','txt'])
                    jd_text = st.text_area("Paste Job Description here", height=150)

                    if st.button("Analyze Resume"):
                        resume_text = extract_text(resume_file)
                        if not resume_text:
                            st.error("Failed to extract resume text.")
                        elif not jd_text.strip():
                            st.warning("Please provide job description.")
                        else:
                            ats = ats_score(resume_text, jd_text)
                            ml = ml_score_dummy(resume_text, jd_text)
                            resume_skills = clean_text(resume_text)
                            jd_skills = clean_text(jd_text)
                            matched = sorted(resume_skills & jd_skills)
                            missing = sorted(jd_skills - resume_skills)

                            st.markdown(f"**ATS Score:** {ats:.2f}%")
                            st.markdown(f"**ML Relevance Score:** {ml:.2f}%")
                            st.markdown(f"**Matched Skills:** {', '.join(matched) if matched else 'None'}")
                            st.markdown(f"**Missing Skills:** {', '.join(missing) if missing else 'None'}")
                else:
                    st.error("Invalid username or password")
            else:
                st.warning("Please enter username and password")

if __name__ == "__main__":
    main()