Spaces:
Sleeping
Sleeping
File size: 10,774 Bytes
da3cb05 ea97af8 da3cb05 6e7206a ea97af8 33c7116 6fcd09e ea97af8 6fcd09e 896df36 6fcd09e ea97af8 da3cb05 33c7116 ea97af8 33c7116 a8c751e 33c7116 ea97af8 50e2080 ea97af8 a693879 ea97af8 33c7116 a8c751e ea97af8 33c7116 ea97af8 33c7116 ea97af8 a8c751e ea97af8 a8c751e ea97af8 6fcd09e a6549e8 6fcd09e 3725d22 6fcd09e f77d6e1 6fcd09e 0f8c74a 6fcd09e 3725d22 6fcd09e ea97af8 da3cb05 a8c751e ea97af8 33c7116 a693879 a8c751e 33c7116 a8c751e ea97af8 a8c751e ea97af8 a8c751e ea97af8 33c7116 ea97af8 6fcd09e ea97af8 da3cb05 a8c751e ea97af8 da3cb05 6e7206a da3cb05 a8c751e da3cb05 a8c751e da3cb05 ea97af8 a8c751e ea97af8 a8c751e da3cb05 a8c751e da3cb05 a8c751e da3cb05 a8c751e da3cb05 a8c751e da3cb05 a8c751e da3cb05 a8c751e da3cb05 6e7206a a8c751e da3cb05 a8c751e da3cb05 a8c751e da3cb05 a8c751e da3cb05 a8c751e da3cb05 a8c751e da3cb05 a8c751e da3cb05 a8c751e da3cb05 a8c751e da3cb05 a8c751e da3cb05 a8c751e da3cb05 |
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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
import streamlit as st
import os
import tempfile
from pathlib import Path
import time
from typing import List, Dict, Tuple
import pandas as pd
from streamlit.runtime.uploaded_file_manager import UploadedFile
from anthropic import Anthropic
import pymongo
from dotenv import load_dotenv
import fitz # PyMuPDF
import voyageai
from pinecone.grpc import PineconeGRPC as Pinecone
from pinecone import ServerlessSpec
from pinecone import Index
# Load environment variables
load_dotenv()
# Initialize VoyageAI constants
VOYAGEAI_BATCH_SIZE = 128
VOYAGEAI_VECTOR_DIM = 512
# Initialize Pinecone
PINECONE_ID = "intratalent-v2"
# Initialize MongoDB client
MONGO_URI = os.getenv('MONGO_URI')
mongo_client = pymongo.MongoClient(MONGO_URI)
db = mongo_client['intratalent']
resume_collection = db['resumes']
# Initialize Anthropic client
anthropic = Anthropic(api_key=os.getenv('ANTHROPIC_API_KEY'))
# Initialize Streamlit app
st.set_page_config(
page_title="IntraTalent Resume Processor",
page_icon="π",
layout="wide"
)
def extract_text_from_pdf(pdf_content: bytes) -> str:
"""Extract text from PDF content."""
try:
# Create a temporary file to store the PDF content
with tempfile.NamedTemporaryFile(mode='w+b', suffix='.pdf', delete=False) as temp_file:
temp_file.write(pdf_content)
temp_file_path = temp_file.name
# Extract text from PDF
doc = fitz.open(temp_file_path)
text = ""
for page_num in range(doc.page_count):
page = doc.load_page(page_num)
text += page.get_text() + "\n"
doc.close()
# Clean up temporary file
os.unlink(temp_file_path)
return text
except Exception as e:
st.error(f"Error extracting text from PDF: {e}")
return ""
def extract_info_with_claude(resume_text: str) -> str:
"""Extract information from resume text using Claude."""
st.write("π€ Sending request to Claude API...")
prompt = """
Extract the following information from the given resume:
1. Full Name
2. List of all experiences with their descriptions (copy exactly from resume)
Please format the output as follows:
Name: [Full Name]
Projects:
1. [Experience/Project Name]: [Experience/Project Description]
2. [Experience/Project Name]: [Experience/Project Description]
...
Extract all experiences, including projects, leadership, work experience, research, etc. Don't include hyphens and put the entire description on one line.
Here's the resume text:
{resume_text}
""".format(resume_text=resume_text)
try:
message = anthropic.messages.create(
model="claude-3-haiku-20240307",
max_tokens=4096,
system="You are a helpful assistant that extracts information from resumes.",
messages=[{
"role": "user",
"content": prompt
}]
)
extracted_info = message.content[0].text
st.write("β
Received response from Claude:")
st.code(extracted_info, language="text")
except Exception as e:
extracted_info = f"An error occurred: {e}"
st.error(f"β API Error: {e}")
return extracted_info
def get_pinecone_index(database_id: str) -> Index:
# initialize connection to pinecone
pc = Pinecone(api_key=os.getenv('PINECONE_API_KEY'))
# if the index does not exist, we create it
if not database_id in pc.list_indexes():
pc.create_index(
database_id,
dimension=VOYAGEAI_VECTOR_DIM,
spec=ServerlessSpec(
cloud='aws',
region='us-east-1'
),
metric='cosine'
)
# connect to index
index = pc.Index(database_id)
def add_to_voyage(person_name: str, person_projects: list) -> None:
embeds = []
metas = []
ids = []
index = get_pinecone_index(PINECONE_ID)
vo = voyageai.Client(api_key=os.getenv('VOYAGEAI_API_KEY'))
for i in range(len(person_projects)):
# Get the ith project
project = person_projects[i]
# Embed the description
embed = vo.embed(
texts=project["description"],
model='voyage-3-lite',
truncation=False
).embeddings[0]
embeds.append(embed)
# Create metadata using person's name + project name
meta = f"{person_name} {project['name']}"
metas.append(meta)
# Give it a unique id
id = i
ids.append(i)
# create list of (id, vector, metadata) tuples to be upserted
to_upsert = list(zip(ids, embeds, meta))
for i in range(0, len(ids), VOYAGEAI_BATCH_SIZE):
i_end = min(i+VOYAGEAI_BATCH_SIZE, len(ids))
index.upsert(vectors=to_upsert[i:i_end])
# let's view the index statistics
st.write(index.describe_index_stats())
def parse_resume(uploaded_file: UploadedFile) -> Tuple[str, List[Dict]]:
"""Parse a resume file and return name and projects."""
try:
st.write(f"π Processing resume: {uploaded_file.name}")
resume_content = uploaded_file.getvalue()
st.write("π Extracting text from PDF...")
resume_text = extract_text_from_pdf(resume_content)
st.write("π Extracted text from PDF:")
st.code(resume_text)
extracted_info = extract_info_with_claude(resume_text)
st.write("π Parsing extracted information...")
# Parse the extracted information
lines = extracted_info.split('\n')
name = lines[0].split(': ')[1] if len(lines) > 0 and ': ' in lines[0] else "Unknown"
st.write(f"π€ Extracted name: {name}")
projects = []
project_started = False
for line in lines:
if line.strip() == "Projects:":
project_started = True
continue
if project_started and line.strip():
project_parts = line.split(': ', 1)
if len(project_parts) == 2:
project_name = project_parts[0].split('. ', 1)[-1] # Remove the number
project_description = project_parts[1]
projects.append({"name": project_name, "description": project_description})
st.write("π Extracted projects:")
st.json(projects)
# Store in MongoDB
resume_data = {
"name": name,
"projects": projects,
"full_content": resume_text
}
add_to_voyage(name, projects)
st.write("πΎ Stored data in VoyageAI")
return name, projects
except Exception as e:
st.error(f"β Error processing resume: {e}")
return "Unknown", []
def process_resumes(uploaded_files: List[UploadedFile]) -> Dict:
"""Process multiple resumes and return results."""
results = {}
progress_bar = st.progress(0)
for idx, file in enumerate(uploaded_files):
st.write(f"\n---\n### Processing file {idx + 1} of {len(uploaded_files)}")
if file.type != "application/pdf":
st.warning(f"β οΈ Skipping {file.name}: Not a PDF file")
continue
try:
name, projects = parse_resume(file)
results[file.name] = {
"name": name,
"projects": projects
}
# Update progress
progress_bar.progress((idx + 1) / len(uploaded_files))
st.write(f"β
Successfully processed {file.name}")
except Exception as e:
st.error(f"β Error processing {file.name}: {e}")
return results
def display_results(results: Dict):
"""Display processed resume results in an organized manner."""
if not results:
return
st.subheader("π Processed Resumes")
for filename, data in results.items():
with st.expander(f"π {data['name']} ({filename})"):
st.write("π·οΈ File details:")
st.json({
"filename": filename,
"name": data['name'],
"number_of_projects": len(data['projects'])
})
if data['projects']:
st.write("π Projects:")
df = pd.DataFrame(data['projects'])
st.dataframe(
df,
column_config={
"name": "Project Name",
"description": "Description"
},
hide_index=True
)
else:
st.info("βΉοΈ No projects found in this resume")
def main():
st.title("π― IntraTalent Resume Processor")
# File uploader section
st.header("π€ Upload Resumes")
uploaded_files = st.file_uploader(
"Upload up to 10 resumes (PDF only)",
type=['pdf'],
accept_multiple_files=True,
key="resume_uploader"
)
# Validate number of files
if uploaded_files and len(uploaded_files) > 10:
st.error("β οΈ Maximum 10 files allowed. Please remove some files.")
return
# Process button
if uploaded_files and st.button("π Process Resumes"):
with st.spinner("Processing resumes..."):
st.write("π Starting resume processing...")
results = process_resumes(uploaded_files)
st.session_state['processed_results'] = results
st.write("β¨ Processing complete!")
display_results(results)
# Query section
st.header("π Search Projects")
query = st.text_area(
"Enter your project requirements",
placeholder="Example: Looking for team members with experience in machine learning and computer vision...",
height=100
)
if query and st.button("π Search"):
if 'processed_results' not in st.session_state:
st.warning("β οΈ Please process some resumes first!")
return
with st.spinner("Searching for matches..."):
st.write("π Preparing to search...")
# Here you would implement the embedding and similarity search
# Using the code from your original script
st.success("β
Search completed!")
# Display results in a nice format
st.subheader("π― Top Matches")
# Placeholder for search results
st.info("π Feature coming soon: Will display matching projects and candidates based on similarity search")
if __name__ == "__main__":
main() |