Update pages/01_Insights.py
Browse files- pages/01_Insights.py +92 -92
pages/01_Insights.py
CHANGED
|
@@ -1,92 +1,92 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import tempfile
|
| 3 |
-
import fitz # PyMuPDF
|
| 4 |
-
from langchain_openai import ChatOpenAI
|
| 5 |
-
from langchain.prompts import PromptTemplate
|
| 6 |
-
import os
|
| 7 |
-
|
| 8 |
-
# Set your OpenAI API key here
|
| 9 |
-
os.environ["OPENAI_API_KEY"] = 'sk-proj-
|
| 10 |
-
|
| 11 |
-
# Function to generate insights based on context
|
| 12 |
-
def generate_insights(resume_text, resume_links):
|
| 13 |
-
prompt_template = f"""
|
| 14 |
-
You are an experienced Technical Recruiter specializing in AI roles.
|
| 15 |
-
Review the candidate's resume and extract key insights relevant to an AI engineering position.
|
| 16 |
-
|
| 17 |
-
Resume Details:
|
| 18 |
-
- Parsed Resume Text Content: {resume_text}
|
| 19 |
-
- Hyperlinked Information: {resume_links}
|
| 20 |
-
|
| 21 |
-
Please extract and organize the following information:
|
| 22 |
-
1. **Candidate Name**
|
| 23 |
-
2. **Educational Qualifications**
|
| 24 |
-
3. **Total Work Experience**
|
| 25 |
-
4. **Relevant AI Experience**
|
| 26 |
-
5. **Gaps in Employment**
|
| 27 |
-
6. **Publications**
|
| 28 |
-
7. **Professional Links**
|
| 29 |
-
8. **Other Noteworthy Details**
|
| 30 |
-
|
| 31 |
-
Output the insights in a clear, summarized format.
|
| 32 |
-
"""
|
| 33 |
-
prompt = PromptTemplate.from_template(prompt_template)
|
| 34 |
-
chat_model = ChatOpenAI(temperature=0, model="gpt-3.5-turbo")
|
| 35 |
-
response = chat_model.generate([prompt.format(resume_text=resume_text, resume_links=resume_links)])
|
| 36 |
-
insights = response.generations[0][0].text.strip()
|
| 37 |
-
return insights
|
| 38 |
-
|
| 39 |
-
# Function to parse PDF with text and hyperlinks
|
| 40 |
-
def parse_pdf_with_links(uploaded_file):
|
| 41 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
|
| 42 |
-
temp_file.write(uploaded_file.read())
|
| 43 |
-
temp_file_path = temp_file.name
|
| 44 |
-
|
| 45 |
-
text_content = []
|
| 46 |
-
links_with_display_names = []
|
| 47 |
-
|
| 48 |
-
with fitz.open(temp_file_path) as doc:
|
| 49 |
-
for page_num, page in enumerate(doc):
|
| 50 |
-
text_content.append(page.get_text("text"))
|
| 51 |
-
for link in page.get_links():
|
| 52 |
-
if link.get("uri"):
|
| 53 |
-
rect = fitz.Rect(link["from"])
|
| 54 |
-
display_name = page.get_text("text", clip=rect).strip() or "Unnamed Link"
|
| 55 |
-
links_with_display_names.append({
|
| 56 |
-
"page": page_num + 1,
|
| 57 |
-
"display_name": display_name,
|
| 58 |
-
"uri": link["uri"],
|
| 59 |
-
"rect": rect
|
| 60 |
-
})
|
| 61 |
-
|
| 62 |
-
full_text = "\n".join(text_content)
|
| 63 |
-
return full_text, links_with_display_names
|
| 64 |
-
|
| 65 |
-
# Initialize session states for page control
|
| 66 |
-
if "page" not in st.session_state:
|
| 67 |
-
st.session_state.page = "upload" # Set default page to "upload"
|
| 68 |
-
|
| 69 |
-
# Display upload page if "upload" page is active
|
| 70 |
-
if st.session_state.page == "upload":
|
| 71 |
-
st.header("Resume Insights Generator")
|
| 72 |
-
resume_file = st.file_uploader("Upload Resume (PDF only)", type="pdf")
|
| 73 |
-
jd_file = st.file_uploader("Upload Job Description (PDF only)", type="pdf")
|
| 74 |
-
|
| 75 |
-
if resume_file:
|
| 76 |
-
resume_text, resume_links = parse_pdf_with_links(resume_file)
|
| 77 |
-
formatted_links = [f"Page {link['page']}: [{link['display_name']}]({link['uri']}) at {link['rect']}" for link in resume_links]
|
| 78 |
-
st.session_state.resume_text = resume_text
|
| 79 |
-
st.session_state.resume_links = "\n".join(formatted_links)
|
| 80 |
-
|
| 81 |
-
if jd_file:
|
| 82 |
-
jd_text, jd_links = parse_pdf_with_links(jd_file)
|
| 83 |
-
st.session_state.jd_text = jd_text
|
| 84 |
-
|
| 85 |
-
if st.button("Generate Insights from Resume"):
|
| 86 |
-
if "resume_text" in st.session_state and "resume_links" in st.session_state:
|
| 87 |
-
insights = generate_insights(st.session_state.resume_text, st.session_state.resume_links)
|
| 88 |
-
st.session_state.insights = insights
|
| 89 |
-
st.write("### Extracted Insights from the Resume")
|
| 90 |
-
st.write(insights)
|
| 91 |
-
else:
|
| 92 |
-
st.error("Please upload a resume before generating insights.")
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import tempfile
|
| 3 |
+
import fitz # PyMuPDF
|
| 4 |
+
from langchain_openai import ChatOpenAI
|
| 5 |
+
from langchain.prompts import PromptTemplate
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Set your OpenAI API key here
|
| 9 |
+
os.environ["OPENAI_API_KEY"] = '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'
|
| 10 |
+
|
| 11 |
+
# Function to generate insights based on context
|
| 12 |
+
def generate_insights(resume_text, resume_links):
|
| 13 |
+
prompt_template = f"""
|
| 14 |
+
You are an experienced Technical Recruiter specializing in AI roles.
|
| 15 |
+
Review the candidate's resume and extract key insights relevant to an AI engineering position.
|
| 16 |
+
|
| 17 |
+
Resume Details:
|
| 18 |
+
- Parsed Resume Text Content: {resume_text}
|
| 19 |
+
- Hyperlinked Information: {resume_links}
|
| 20 |
+
|
| 21 |
+
Please extract and organize the following information:
|
| 22 |
+
1. **Candidate Name**
|
| 23 |
+
2. **Educational Qualifications**
|
| 24 |
+
3. **Total Work Experience**
|
| 25 |
+
4. **Relevant AI Experience**
|
| 26 |
+
5. **Gaps in Employment**
|
| 27 |
+
6. **Publications**
|
| 28 |
+
7. **Professional Links**
|
| 29 |
+
8. **Other Noteworthy Details**
|
| 30 |
+
|
| 31 |
+
Output the insights in a clear, summarized format.
|
| 32 |
+
"""
|
| 33 |
+
prompt = PromptTemplate.from_template(prompt_template)
|
| 34 |
+
chat_model = ChatOpenAI(temperature=0, model="gpt-3.5-turbo")
|
| 35 |
+
response = chat_model.generate([prompt.format(resume_text=resume_text, resume_links=resume_links)])
|
| 36 |
+
insights = response.generations[0][0].text.strip()
|
| 37 |
+
return insights
|
| 38 |
+
|
| 39 |
+
# Function to parse PDF with text and hyperlinks
|
| 40 |
+
def parse_pdf_with_links(uploaded_file):
|
| 41 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
|
| 42 |
+
temp_file.write(uploaded_file.read())
|
| 43 |
+
temp_file_path = temp_file.name
|
| 44 |
+
|
| 45 |
+
text_content = []
|
| 46 |
+
links_with_display_names = []
|
| 47 |
+
|
| 48 |
+
with fitz.open(temp_file_path) as doc:
|
| 49 |
+
for page_num, page in enumerate(doc):
|
| 50 |
+
text_content.append(page.get_text("text"))
|
| 51 |
+
for link in page.get_links():
|
| 52 |
+
if link.get("uri"):
|
| 53 |
+
rect = fitz.Rect(link["from"])
|
| 54 |
+
display_name = page.get_text("text", clip=rect).strip() or "Unnamed Link"
|
| 55 |
+
links_with_display_names.append({
|
| 56 |
+
"page": page_num + 1,
|
| 57 |
+
"display_name": display_name,
|
| 58 |
+
"uri": link["uri"],
|
| 59 |
+
"rect": rect
|
| 60 |
+
})
|
| 61 |
+
|
| 62 |
+
full_text = "\n".join(text_content)
|
| 63 |
+
return full_text, links_with_display_names
|
| 64 |
+
|
| 65 |
+
# Initialize session states for page control
|
| 66 |
+
if "page" not in st.session_state:
|
| 67 |
+
st.session_state.page = "upload" # Set default page to "upload"
|
| 68 |
+
|
| 69 |
+
# Display upload page if "upload" page is active
|
| 70 |
+
if st.session_state.page == "upload":
|
| 71 |
+
st.header("Resume Insights Generator")
|
| 72 |
+
resume_file = st.file_uploader("Upload Resume (PDF only)", type="pdf")
|
| 73 |
+
jd_file = st.file_uploader("Upload Job Description (PDF only)", type="pdf")
|
| 74 |
+
|
| 75 |
+
if resume_file:
|
| 76 |
+
resume_text, resume_links = parse_pdf_with_links(resume_file)
|
| 77 |
+
formatted_links = [f"Page {link['page']}: [{link['display_name']}]({link['uri']}) at {link['rect']}" for link in resume_links]
|
| 78 |
+
st.session_state.resume_text = resume_text
|
| 79 |
+
st.session_state.resume_links = "\n".join(formatted_links)
|
| 80 |
+
|
| 81 |
+
if jd_file:
|
| 82 |
+
jd_text, jd_links = parse_pdf_with_links(jd_file)
|
| 83 |
+
st.session_state.jd_text = jd_text
|
| 84 |
+
|
| 85 |
+
if st.button("Generate Insights from Resume"):
|
| 86 |
+
if "resume_text" in st.session_state and "resume_links" in st.session_state:
|
| 87 |
+
insights = generate_insights(st.session_state.resume_text, st.session_state.resume_links)
|
| 88 |
+
st.session_state.insights = insights
|
| 89 |
+
st.write("### Extracted Insights from the Resume")
|
| 90 |
+
st.write(insights)
|
| 91 |
+
else:
|
| 92 |
+
st.error("Please upload a resume before generating insights.")
|