Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,10 +24,28 @@ ACHIEVEMENT_PATTERN = re.compile(r'(increased|reduced|saved|improved)\s+by\s+(\d
|
|
| 24 |
TYPO_PATTERN = re.compile(r'\b(?:responsibilities|accomplishment|experiance)\b', re.I)
|
| 25 |
|
| 26 |
def extract_text_from_pdf(pdf_file):
|
| 27 |
-
"""Extract text with
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
try:
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
return text[:10000] # Limit to first 10k chars
|
|
|
|
|
|
|
| 31 |
finally:
|
| 32 |
gc.collect()
|
| 33 |
|
|
@@ -70,36 +88,41 @@ def calculate_scores(resume_text, job_desc=None):
|
|
| 70 |
|
| 71 |
def analyze_resume(pdf_file, job_desc=None, inference_fn=None):
|
| 72 |
"""Analyze resume using Together AI inference"""
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
scores, total_score = calculate_scores(resume_text, job_desc)
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
- "
|
| 79 |
-
- "
|
| 80 |
-
|
| 81 |
-
Output ONLY a valid JSON string, no extra text or markdown."""
|
| 82 |
|
| 83 |
try:
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
# Debug: Log the raw result
|
| 87 |
-
print(f"Raw inference result: {result}")
|
| 88 |
|
|
|
|
|
|
|
| 89 |
if not result or result.strip() == "":
|
| 90 |
-
return {"error": "Empty response from
|
| 91 |
|
| 92 |
# Parse the response as JSON
|
| 93 |
parsed_result = json.loads(result)
|
| 94 |
return {
|
| 95 |
"score": {"total": total_score, "breakdown": scores},
|
| 96 |
"analysis": parsed_result,
|
| 97 |
-
"raw_text": resume_text[:500]
|
|
|
|
| 98 |
}
|
| 99 |
except json.JSONDecodeError as e:
|
| 100 |
-
return {"error": f"
|
| 101 |
except Exception as e:
|
| 102 |
-
return {"error": str(e)}
|
| 103 |
|
| 104 |
# --- Gradio Interface --- #
|
| 105 |
with gr.Blocks(theme=gr.themes.Soft(), fill_height=True) as demo:
|
|
|
|
| 24 |
TYPO_PATTERN = re.compile(r'\b(?:responsibilities|accomplishment|experiance)\b', re.I)
|
| 25 |
|
| 26 |
def extract_text_from_pdf(pdf_file):
|
| 27 |
+
"""Extract text from PDF with robust error handling"""
|
| 28 |
+
if pdf_file is None:
|
| 29 |
+
raise ValueError("No PDF file uploaded")
|
| 30 |
+
|
| 31 |
+
# Check if pdf_file is bytes (binary data from Gradio)
|
| 32 |
+
if not isinstance(pdf_file, bytes):
|
| 33 |
+
raise TypeError(f"Expected binary data (bytes), got {type(pdf_file)}")
|
| 34 |
+
|
| 35 |
try:
|
| 36 |
+
# Read binary data into PdfReader
|
| 37 |
+
pdf_reader = PyPDF2.PdfReader(io.BytesIO(pdf_file))
|
| 38 |
+
if len(pdf_reader.pages) == 0:
|
| 39 |
+
raise ValueError("PDF has no pages")
|
| 40 |
+
|
| 41 |
+
# Extract text from first page
|
| 42 |
+
text = pdf_reader.pages[0].extract_text()
|
| 43 |
+
if text is None or text.strip() == "":
|
| 44 |
+
raise ValueError("No text extracted from PDF (possibly image-based)")
|
| 45 |
+
|
| 46 |
return text[:10000] # Limit to first 10k chars
|
| 47 |
+
except Exception as e:
|
| 48 |
+
raise Exception(f"PDF extraction failed: {str(e)}")
|
| 49 |
finally:
|
| 50 |
gc.collect()
|
| 51 |
|
|
|
|
| 88 |
|
| 89 |
def analyze_resume(pdf_file, job_desc=None, inference_fn=None):
|
| 90 |
"""Analyze resume using Together AI inference"""
|
| 91 |
+
try:
|
| 92 |
+
# Extract text from the uploaded PDF
|
| 93 |
+
resume_text = extract_text_from_pdf(pdf_file)
|
| 94 |
+
except Exception as e:
|
| 95 |
+
return {"error": f"Text extraction error: {str(e)}", "raw_result": "Not applicable"}
|
| 96 |
+
|
| 97 |
scores, total_score = calculate_scores(resume_text, job_desc)
|
| 98 |
|
| 99 |
+
prompt = f"""[Return valid JSON]: Based on these scores: {scores}, provide:
|
| 100 |
+
- "strengths": 2 key strengths (e.g., "High experience quality" if score is high),
|
| 101 |
+
- "improvements": 3 specific improvements,
|
| 102 |
+
- "missing_skills": 2 missing skills (use job description if provided: {job_desc or "None"}).
|
| 103 |
+
Output a valid JSON string only, no extra text."""
|
|
|
|
| 104 |
|
| 105 |
try:
|
| 106 |
+
if inference_fn is None:
|
| 107 |
+
return {"error": "Inference function not provided", "raw_result": "Not available"}
|
|
|
|
|
|
|
| 108 |
|
| 109 |
+
# Send prompt to Together AI (no file upload, just text)
|
| 110 |
+
result = inference_fn(prompt)
|
| 111 |
if not result or result.strip() == "":
|
| 112 |
+
return {"error": "Empty response from Together AI", "raw_result": result}
|
| 113 |
|
| 114 |
# Parse the response as JSON
|
| 115 |
parsed_result = json.loads(result)
|
| 116 |
return {
|
| 117 |
"score": {"total": total_score, "breakdown": scores},
|
| 118 |
"analysis": parsed_result,
|
| 119 |
+
"raw_text": resume_text[:500],
|
| 120 |
+
"raw_result": result # Debug: Show raw response
|
| 121 |
}
|
| 122 |
except json.JSONDecodeError as e:
|
| 123 |
+
return {"error": f"Failed to parse JSON: {str(e)}", "raw_result": result}
|
| 124 |
except Exception as e:
|
| 125 |
+
return {"error": f"Unexpected error: {str(e)}", "raw_result": result if 'result' in locals() else "Not available"}
|
| 126 |
|
| 127 |
# --- Gradio Interface --- #
|
| 128 |
with gr.Blocks(theme=gr.themes.Soft(), fill_height=True) as demo:
|