Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,90 +1,93 @@
|
|
1 |
-
from fastapi import FastAPI, UploadFile, File
|
2 |
-
import json, re, io
|
3 |
-
from llama_cpp import Llama
|
4 |
-
from PyPDF2 import PdfReader
|
5 |
-
from docx import Document
|
6 |
-
import os
|
7 |
-
|
8 |
-
# ✅
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
def extract_text_from_resume(uploaded_file):
|
20 |
-
file_content = uploaded_file.file.read()
|
21 |
-
file_stream = io.BytesIO(file_content)
|
22 |
-
|
23 |
-
if uploaded_file.filename.endswith(".pdf"):
|
24 |
-
reader = PdfReader(file_stream)
|
25 |
-
return "\n".join([page.extract_text() for page in reader.pages if page.extract_text()])
|
26 |
-
elif uploaded_file.filename.endswith(".docx"):
|
27 |
-
doc = Document(file_stream)
|
28 |
-
return "\n".join([para.text for para in doc.paragraphs])
|
29 |
-
return None
|
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 |
-
return
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
extracted_info
|
90 |
-
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, UploadFile, File
|
2 |
+
import json, re, io
|
3 |
+
from llama_cpp import Llama
|
4 |
+
from PyPDF2 import PdfReader
|
5 |
+
from docx import Document
|
6 |
+
import os
|
7 |
+
|
8 |
+
# ✅ Load Mistral 7B Model from Hugging Face Model Hub
|
9 |
+
MODEL_PATH = "TheBloke/CapybaraHermes-2.5-Mistral-7B-GGUF"
|
10 |
+
|
11 |
+
print(f"🔹 Loading Mistral 7B from Hugging Face Model Hub: {MODEL_PATH} (This may take a while)")
|
12 |
+
|
13 |
+
llm = Llama.from_pretrained(MODEL_PATH, n_ctx=4096, n_gpu_layers=-1) # Use GPU if available
|
14 |
+
print("✅ Model loaded successfully!")
|
15 |
+
|
16 |
+
app = FastAPI(title="Resume Parsing API", description="Extracts key details from resumes using Mistral 7B")
|
17 |
+
|
18 |
+
# ✅ Extract Text from PDF or DOCX
|
19 |
+
def extract_text_from_resume(uploaded_file):
|
20 |
+
file_content = uploaded_file.file.read()
|
21 |
+
file_stream = io.BytesIO(file_content)
|
22 |
+
|
23 |
+
if uploaded_file.filename.endswith(".pdf"):
|
24 |
+
reader = PdfReader(file_stream)
|
25 |
+
return "\n".join([page.extract_text() for page in reader.pages if page.extract_text()])
|
26 |
+
elif uploaded_file.filename.endswith(".docx"):
|
27 |
+
doc = Document(file_stream)
|
28 |
+
return "\n".join([para.text for para in doc.paragraphs])
|
29 |
+
return None
|
30 |
+
|
31 |
+
# ✅ Extract Email & Phone Number
|
32 |
+
def extract_email_phone(text):
|
33 |
+
email_pattern = r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}"
|
34 |
+
phone_pattern = r"\+?\d{1,3}?[-.\s]?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}"
|
35 |
+
|
36 |
+
email_match = re.search(email_pattern, text)
|
37 |
+
phone_match = re.search(phone_pattern, text)
|
38 |
+
|
39 |
+
return {
|
40 |
+
"email": email_match.group() if email_match else "Email not found",
|
41 |
+
"phone": phone_match.group() if phone_match else "Phone not found"
|
42 |
+
}
|
43 |
+
|
44 |
+
# ✅ Analyze Resume using Mistral 7B
|
45 |
+
def analyze_resume(text):
|
46 |
+
truncated_text = text[:3500] # Keep within context limit
|
47 |
+
|
48 |
+
prompt = f"""
|
49 |
+
Extract these details from the resume:
|
50 |
+
1. Full Name
|
51 |
+
2. Work Experience (Company Names, Roles, Responsibilities, Duration)
|
52 |
+
3. Qualifications (Degrees, Certifications)
|
53 |
+
4. List of Skills
|
54 |
+
|
55 |
+
Resume Text: {truncated_text}
|
56 |
+
|
57 |
+
Format response as a **strict JSON object**:
|
58 |
+
{{
|
59 |
+
"name": "Candidate Name",
|
60 |
+
"experience": [
|
61 |
+
{{
|
62 |
+
"company": "Company Name",
|
63 |
+
"role": "Job Title",
|
64 |
+
"duration": "Start Date - End Date",
|
65 |
+
"responsibilities": "Brief work responsibilities"
|
66 |
+
}}
|
67 |
+
],
|
68 |
+
"qualifications": "Degree, Certifications",
|
69 |
+
"skills": ["List of skills"]
|
70 |
+
}}
|
71 |
+
"""
|
72 |
+
|
73 |
+
response = llm(prompt, max_tokens=700)
|
74 |
+
output = response["choices"][0]["text"].strip()
|
75 |
+
print("🔹 Raw LLaMA Output:\n", output)
|
76 |
+
|
77 |
+
try:
|
78 |
+
return json.loads(output)
|
79 |
+
except json.JSONDecodeError:
|
80 |
+
return {"error": "Failed to parse LLaMA output", "raw_output": output}
|
81 |
+
|
82 |
+
# ✅ FastAPI Route to Parse Resume
|
83 |
+
@app.post("/parse-resume/")
|
84 |
+
async def parse_resume(file: UploadFile = File(...)):
|
85 |
+
text = extract_text_from_resume(file)
|
86 |
+
if not text:
|
87 |
+
return {"error": "Unsupported file format or could not extract text"}
|
88 |
+
|
89 |
+
extracted_info = extract_email_phone(text)
|
90 |
+
llm_data = analyze_resume(text)
|
91 |
+
|
92 |
+
extracted_info.update(llm_data)
|
93 |
+
return {"success": True, "data": extracted_info}
|