Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import PyPDF2
|
|
| 4 |
import docx
|
| 5 |
import requests
|
| 6 |
import json
|
|
|
|
| 7 |
|
| 8 |
# Function to extract text from PDF
|
| 9 |
def extract_text_from_pdf(file):
|
|
@@ -29,6 +30,24 @@ def process_uploaded_file(file):
|
|
| 29 |
else:
|
| 30 |
raise ValueError("Unsupported file format. Please upload a PDF or Word document.")
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
# Function to call Together API
|
| 33 |
def analyze_with_mistral(resume_text, job_description):
|
| 34 |
TOGETHER_API_KEY = os.getenv("HUGGINGFACE_API_KEY")
|
|
@@ -50,7 +69,10 @@ Follow these guidelines when analyzing resumes:
|
|
| 50 |
6. Provide specific, actionable recommendations
|
| 51 |
7. Be thorough but concise in your analysis
|
| 52 |
|
| 53 |
-
Your analysis must be comprehensive yet practical for the candidate to implement.
|
|
|
|
|
|
|
|
|
|
| 54 |
},
|
| 55 |
{
|
| 56 |
"role": "user",
|
|
@@ -63,7 +85,7 @@ RESUME:
|
|
| 63 |
JOB DESCRIPTION:
|
| 64 |
{job_description}
|
| 65 |
|
| 66 |
-
Provide a detailed analysis in EXACTLY the following JSON format:
|
| 67 |
|
| 68 |
{{
|
| 69 |
"ATS_Compatibility": {{
|
|
@@ -110,7 +132,7 @@ Provide a detailed analysis in EXACTLY the following JSON format:
|
|
| 110 |
}}
|
| 111 |
}}
|
| 112 |
|
| 113 |
-
Ensure all recommendations are specific, actionable, and tailored to both the resume content and job description.
|
| 114 |
"""
|
| 115 |
}
|
| 116 |
]
|
|
@@ -135,16 +157,37 @@ Ensure all recommendations are specific, actionable, and tailored to both the re
|
|
| 135 |
result = response.json()
|
| 136 |
content = result.get("choices", [{}])[0].get("message", {}).get("content", "{}")
|
| 137 |
|
| 138 |
-
#
|
|
|
|
|
|
|
| 139 |
try:
|
| 140 |
-
parsed = json.loads(
|
| 141 |
# Check if the JSON structure is valid for our use case
|
| 142 |
-
# Note: We've updated this validation to match our new JSON structure
|
| 143 |
if "ATS_Compatibility" not in parsed or "Overall_Assessment" not in parsed:
|
| 144 |
-
return {
|
|
|
|
|
|
|
|
|
|
| 145 |
return parsed
|
| 146 |
except json.JSONDecodeError as e:
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
except requests.exceptions.RequestException as e:
|
| 150 |
return {"error": f"API request failed: {str(e)}"}
|
|
@@ -163,7 +206,7 @@ def analyze_resume(file, job_description):
|
|
| 163 |
result = analyze_with_mistral(resume_text, job_description)
|
| 164 |
|
| 165 |
# Return as formatted JSON string for better display
|
| 166 |
-
return json.dumps(result, indent=2)
|
| 167 |
|
| 168 |
except Exception as e:
|
| 169 |
return json.dumps({"error": f"Error analyzing resume: {str(e)}"}, indent=2)
|
|
|
|
| 4 |
import docx
|
| 5 |
import requests
|
| 6 |
import json
|
| 7 |
+
import re # Added for regex pattern replacement
|
| 8 |
|
| 9 |
# Function to extract text from PDF
|
| 10 |
def extract_text_from_pdf(file):
|
|
|
|
| 30 |
else:
|
| 31 |
raise ValueError("Unsupported file format. Please upload a PDF or Word document.")
|
| 32 |
|
| 33 |
+
# Function to clean JSON string with escaped backslashes
|
| 34 |
+
def clean_json_string(json_str):
|
| 35 |
+
# Replace escaped backslashes with a temporary marker
|
| 36 |
+
temp_str = json_str.replace('\\_', '__UNDERSCORE__')
|
| 37 |
+
|
| 38 |
+
# Attempt to fix any other common escape sequence issues
|
| 39 |
+
temp_str = temp_str.replace('\\n', '\n')
|
| 40 |
+
temp_str = temp_str.replace('\\t', '\t')
|
| 41 |
+
temp_str = temp_str.replace('\\r', '\r')
|
| 42 |
+
|
| 43 |
+
# Remove any remaining unmatched backslashes
|
| 44 |
+
temp_str = temp_str.replace('\\', '')
|
| 45 |
+
|
| 46 |
+
# Restore underscores
|
| 47 |
+
cleaned_str = temp_str.replace('__UNDERSCORE__', '_')
|
| 48 |
+
|
| 49 |
+
return cleaned_str
|
| 50 |
+
|
| 51 |
# Function to call Together API
|
| 52 |
def analyze_with_mistral(resume_text, job_description):
|
| 53 |
TOGETHER_API_KEY = os.getenv("HUGGINGFACE_API_KEY")
|
|
|
|
| 69 |
6. Provide specific, actionable recommendations
|
| 70 |
7. Be thorough but concise in your analysis
|
| 71 |
|
| 72 |
+
Your analysis must be comprehensive yet practical for the candidate to implement.
|
| 73 |
+
|
| 74 |
+
IMPORTANT: Do not use escaped backslashes in your JSON response.
|
| 75 |
+
"""
|
| 76 |
},
|
| 77 |
{
|
| 78 |
"role": "user",
|
|
|
|
| 85 |
JOB DESCRIPTION:
|
| 86 |
{job_description}
|
| 87 |
|
| 88 |
+
Provide a detailed analysis in EXACTLY the following JSON format (do not escape underscores with backslashes):
|
| 89 |
|
| 90 |
{{
|
| 91 |
"ATS_Compatibility": {{
|
|
|
|
| 132 |
}}
|
| 133 |
}}
|
| 134 |
|
| 135 |
+
Do not escape underscores with backslashes in your response. Ensure all recommendations are specific, actionable, and tailored to both the resume content and job description.
|
| 136 |
"""
|
| 137 |
}
|
| 138 |
]
|
|
|
|
| 157 |
result = response.json()
|
| 158 |
content = result.get("choices", [{}])[0].get("message", {}).get("content", "{}")
|
| 159 |
|
| 160 |
+
# Clean the JSON string before parsing
|
| 161 |
+
cleaned_content = clean_json_string(content)
|
| 162 |
+
|
| 163 |
try:
|
| 164 |
+
parsed = json.loads(cleaned_content)
|
| 165 |
# Check if the JSON structure is valid for our use case
|
|
|
|
| 166 |
if "ATS_Compatibility" not in parsed or "Overall_Assessment" not in parsed:
|
| 167 |
+
return {
|
| 168 |
+
"error": "API returned unexpected JSON structure",
|
| 169 |
+
"raw_content": cleaned_content[:500] + "..." if len(cleaned_content) > 500 else cleaned_content
|
| 170 |
+
}
|
| 171 |
return parsed
|
| 172 |
except json.JSONDecodeError as e:
|
| 173 |
+
# Try a more aggressive approach to fix the JSON
|
| 174 |
+
try:
|
| 175 |
+
# Sometimes the model might include trailing characters
|
| 176 |
+
# Try to find the closing bracket of the main JSON object
|
| 177 |
+
match = re.search(r'(\{.*\})', cleaned_content, re.DOTALL)
|
| 178 |
+
if match:
|
| 179 |
+
extracted_json = match.group(1)
|
| 180 |
+
parsed = json.loads(extracted_json)
|
| 181 |
+
if "ATS_Compatibility" in parsed and "Overall_Assessment" in parsed:
|
| 182 |
+
return parsed
|
| 183 |
+
except:
|
| 184 |
+
pass
|
| 185 |
+
|
| 186 |
+
# If all attempts fail, return the error
|
| 187 |
+
return {
|
| 188 |
+
"error": f"Failed to parse API response: {str(e)}",
|
| 189 |
+
"raw_content": cleaned_content[:500] + "..." if len(cleaned_content) > 500 else cleaned_content
|
| 190 |
+
}
|
| 191 |
|
| 192 |
except requests.exceptions.RequestException as e:
|
| 193 |
return {"error": f"API request failed: {str(e)}"}
|
|
|
|
| 206 |
result = analyze_with_mistral(resume_text, job_description)
|
| 207 |
|
| 208 |
# Return as formatted JSON string for better display
|
| 209 |
+
return json.dumps(result, indent=2, ensure_ascii=False)
|
| 210 |
|
| 211 |
except Exception as e:
|
| 212 |
return json.dumps({"error": f"Error analyzing resume: {str(e)}"}, indent=2)
|