Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,109 +1,100 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from transformers import pipeline
|
| 3 |
-
from fpdf import FPDF
|
| 4 |
-
from docx import Document
|
| 5 |
-
from PyPDF2 import PdfReader
|
| 6 |
import os
|
|
|
|
| 7 |
import tempfile
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
|
|
|
| 28 |
pdf = FPDF()
|
| 29 |
pdf.add_page()
|
| 30 |
pdf.set_font("Arial", size=12)
|
| 31 |
|
| 32 |
-
if
|
| 33 |
-
pdf.set_text_color(0,
|
| 34 |
-
elif
|
| 35 |
-
pdf.set_text_color(0,
|
| 36 |
-
|
| 37 |
-
pdf.set_text_color(
|
| 38 |
|
| 39 |
-
|
| 40 |
-
for line in lines:
|
| 41 |
pdf.multi_cell(0, 10, line)
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
def generate_resume_with_skills(resume_text, job_description):
|
| 48 |
-
prompt = f"Rewrite this resume to better match the following job or scholarship description. Include relevant skills and align it accordingly.\n\nResume:\n{resume_text}\n\nJob Description:\n{job_description}\n\nImproved Resume:"
|
| 49 |
-
response = model(prompt, max_length=512, truncation=True, do_sample=True)
|
| 50 |
-
return response[0]['generated_text']
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
keywords = ["Python", "Machine Learning", "Research", "Communication", "Teamwork"]
|
| 55 |
-
found = [k for k in keywords if k.lower() in job_description.lower()]
|
| 56 |
-
return found
|
| 57 |
|
| 58 |
-
|
| 59 |
-
required = extract_required_skills(job_description)
|
| 60 |
-
matched = sum(1 for skill in required if skill.lower() in resume_text.lower())
|
| 61 |
-
if required:
|
| 62 |
-
return int((matched / len(required)) * 100), required
|
| 63 |
-
return 0, []
|
| 64 |
-
|
| 65 |
-
def generate_human_like_answer(question, resume_text, job_description):
|
| 66 |
-
prompt = f"Based on the following resume and job description, write a natural human-style answer to this application question: '{question}'.\n\nResume: {resume_text}\n\nJob Description: {job_description}\n\nAnswer:"
|
| 67 |
-
response = model(prompt, max_length=256, truncation=True, do_sample=True)
|
| 68 |
-
return response[0]['generated_text']
|
| 69 |
-
|
| 70 |
-
# --- UI ---
|
| 71 |
-
st.title("๐ฏ Smart Resume Generator for Job & Scholarship Applications")
|
| 72 |
-
|
| 73 |
-
resume_file = st.file_uploader("Upload your Resume (PDF, DOCX, TXT)", type=["pdf", "docx", "txt"])
|
| 74 |
-
job_description = st.text_area("Paste Job or Scholarship Description")
|
| 75 |
|
| 76 |
if st.button("Submit"):
|
| 77 |
if resume_file and job_description:
|
| 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 |
else:
|
| 109 |
-
st.
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
+
import re
|
| 4 |
import tempfile
|
| 5 |
+
from PyPDF2 import PdfReader
|
| 6 |
+
from docx import Document
|
| 7 |
+
from transformers import pipeline
|
| 8 |
+
from fpdf import FPDF
|
| 9 |
|
| 10 |
+
# Load GPT model using PyTorch
|
| 11 |
+
generator = pipeline("text-generation", model="distilgpt2", framework="pt")
|
| 12 |
+
|
| 13 |
+
# Helpers to read file content
|
| 14 |
+
def extract_text(file):
|
| 15 |
+
if file.name.endswith('.pdf'):
|
| 16 |
+
reader = PdfReader(file)
|
| 17 |
+
text = ''
|
| 18 |
+
for page in reader.pages:
|
| 19 |
+
text += page.extract_text() or ''
|
| 20 |
+
return text
|
| 21 |
+
elif file.name.endswith('.docx'):
|
| 22 |
+
doc = Document(file)
|
| 23 |
+
return '\n'.join([p.text for p in doc.paragraphs])
|
| 24 |
+
elif file.name.endswith('.txt') or file.name.endswith('.html'):
|
| 25 |
+
return str(file.read(), 'utf-8')
|
| 26 |
+
else:
|
| 27 |
+
return ''
|
| 28 |
|
| 29 |
+
# Basic keyword matching for fit score
|
| 30 |
+
def calculate_fit_score(resume_text, job_text):
|
| 31 |
+
resume_words = set(re.findall(r'\w+', resume_text.lower()))
|
| 32 |
+
job_words = set(re.findall(r'\w+', job_text.lower()))
|
| 33 |
+
overlap = resume_words.intersection(job_words)
|
| 34 |
+
return round((len(overlap) / len(job_words)) * 100, 2) if job_words else 0
|
| 35 |
|
| 36 |
+
# PDF templates
|
| 37 |
+
def generate_pdf(content, template_id=1):
|
| 38 |
pdf = FPDF()
|
| 39 |
pdf.add_page()
|
| 40 |
pdf.set_font("Arial", size=12)
|
| 41 |
|
| 42 |
+
if template_id == 1:
|
| 43 |
+
pdf.set_text_color(0, 0, 128)
|
| 44 |
+
elif template_id == 2:
|
| 45 |
+
pdf.set_text_color(0, 100, 0)
|
| 46 |
+
else:
|
| 47 |
+
pdf.set_text_color(128, 0, 0)
|
| 48 |
|
| 49 |
+
for line in content.split('\n'):
|
|
|
|
| 50 |
pdf.multi_cell(0, 10, line)
|
| 51 |
+
tmp_path = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf").name
|
| 52 |
+
pdf.output(tmp_path)
|
| 53 |
+
return tmp_path
|
| 54 |
|
| 55 |
+
# Streamlit UI
|
| 56 |
+
st.title("Smart Resume Generator ๐")
|
| 57 |
+
st.markdown("Upload your Resume and Paste the Job/Scholarship details")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
resume_file = st.file_uploader("Upload Resume (pdf, docx, txt, html)", type=["pdf", "docx", "txt", "html"])
|
| 60 |
+
job_description = st.text_area("Paste Job or Scholarship Details")
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
template_choice = st.selectbox("Choose Resume Template", [1, 2, 3])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
if st.button("Submit"):
|
| 65 |
if resume_file and job_description:
|
| 66 |
+
with st.spinner("Analyzing and Generating..."):
|
| 67 |
+
resume_text = extract_text(resume_file)
|
| 68 |
+
fit_score = calculate_fit_score(resume_text, job_description)
|
| 69 |
+
|
| 70 |
+
# Prompting GPT for rewriting
|
| 71 |
+
prompt = f"Rewrite the following resume to better fit the job description. Include required skills.\n\nResume:\n{resume_text}\n\nJob:\n{job_description}"
|
| 72 |
+
enhanced_resume = generator(prompt, max_length=1024, do_sample=True)[0]['generated_text']
|
| 73 |
+
|
| 74 |
+
# Show Fit Score and Recommendations
|
| 75 |
+
st.subheader("๐งฎ Fit Score:")
|
| 76 |
+
st.success(f"{fit_score}% match based on your current resume.")
|
| 77 |
+
|
| 78 |
+
missing_skills = set(re.findall(r'\w+', job_description.lower())) - set(re.findall(r'\w+', resume_text.lower()))
|
| 79 |
+
st.markdown("**Missing or Recommended Skills:**")
|
| 80 |
+
st.info(', '.join(list(missing_skills)[:10]))
|
| 81 |
+
|
| 82 |
+
# Preview Resume
|
| 83 |
+
st.subheader("๐ Preview Generated Resume")
|
| 84 |
+
st.text_area("Enhanced Resume Text", enhanced_resume, height=400)
|
| 85 |
+
|
| 86 |
+
# Generate PDF
|
| 87 |
+
pdf_path = generate_pdf(enhanced_resume, template_id=template_choice)
|
| 88 |
+
with open(pdf_path, "rb") as f:
|
| 89 |
+
st.download_button("Download as PDF", data=f, file_name="smart_resume.pdf", mime="application/pdf")
|
| 90 |
+
|
| 91 |
+
# Question Helper
|
| 92 |
+
st.subheader("โ๏ธ Application Form Assistant")
|
| 93 |
+
user_question = st.text_input("Enter a question from the application form:")
|
| 94 |
+
if user_question:
|
| 95 |
+
answer_prompt = f"Answer the question in a human tone (not robotic): {user_question}\nContext: {resume_text} + {job_description}"
|
| 96 |
+
response = generator(answer_prompt, max_length=200, do_sample=True)[0]['generated_text']
|
| 97 |
+
st.write("Suggested Answer:")
|
| 98 |
+
st.write(response)
|
| 99 |
else:
|
| 100 |
+
st.error("Please upload a resume and enter job description.")
|