Shami96 commited on
Commit
37f34cc
ยท
verified ยท
1 Parent(s): a39bcf9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -90
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
- # Set page config
10
- st.set_page_config(page_title="Smart Resume Generator", layout="wide")
11
-
12
- # Load the text generation model (GPT-based)
13
- model = pipeline("text-generation", model="distilgpt2")
14
-
15
- # --- Utility Functions ---
16
- def extract_text_from_pdf(uploaded_file):
17
- reader = PdfReader(uploaded_file)
18
- text = ""
19
- for page in reader.pages:
20
- text += page.extract_text()
21
- return text
 
 
 
 
 
22
 
23
- def extract_text_from_docx(uploaded_file):
24
- doc = Document(uploaded_file)
25
- return "\n".join([para.text for para in doc.paragraphs])
 
 
 
26
 
27
- def save_text_as_pdf(text, template_style):
 
28
  pdf = FPDF()
29
  pdf.add_page()
30
  pdf.set_font("Arial", size=12)
31
 
32
- if template_style == "Modern":
33
- pdf.set_text_color(0, 102, 204)
34
- elif template_style == "Classic":
35
- pdf.set_text_color(0, 0, 0)
36
- elif template_style == "Minimal":
37
- pdf.set_text_color(100, 100, 100)
38
 
39
- lines = text.split("\n")
40
- for line in lines:
41
  pdf.multi_cell(0, 10, line)
 
 
 
42
 
43
- tmp_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
44
- pdf.output(tmp_pdf.name)
45
- return tmp_pdf.name
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
- def extract_required_skills(job_description):
53
- # Placeholder extraction
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
- def fit_percentage(resume_text, job_description):
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
- # Extract resume content
79
- if resume_file.type == "application/pdf":
80
- resume_text = extract_text_from_pdf(resume_file)
81
- elif resume_file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
82
- resume_text = extract_text_from_docx(resume_file)
83
- else:
84
- resume_text = resume_file.read().decode("utf-8")
85
-
86
- st.subheader("๐Ÿ”„ Generating Improved Resume...")
87
- improved_resume = generate_resume_with_skills(resume_text, job_description)
88
-
89
- st.subheader("๐Ÿ“Š Fit Analysis")
90
- fit_percent, needed_skills = fit_percentage(improved_resume, job_description)
91
- st.markdown(f"**Fit for this Role:** {fit_percent}%")
92
- if needed_skills:
93
- st.markdown("**Skills to Improve or Highlight:**")
94
- st.write(needed_skills)
95
-
96
- st.subheader("๐Ÿ“„ Resume Preview")
97
- template = st.selectbox("Choose a Template", ["Modern", "Classic", "Minimal"])
98
- pdf_path = save_text_as_pdf(improved_resume, template)
99
- with open(pdf_path, "rb") as f:
100
- st.download_button("Download PDF", f, file_name="improved_resume.pdf")
101
-
102
- st.subheader("๐Ÿ—ฃ Application Form Question Help")
103
- question = st.text_input("Enter a Question from Application Form")
104
- if question:
105
- answer = generate_human_like_answer(question, resume_text, job_description)
106
- st.text_area("Suggested Answer", value=answer, height=150)
107
-
 
 
 
108
  else:
109
- st.warning("Please upload a resume and paste a job or scholarship description.")
 
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.")