Spaces:
Runtime error
Runtime error
| 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() | |