Spaces:
Sleeping
Sleeping
import json | |
import logging | |
import traceback | |
from django.http import JsonResponse | |
from django.views.decorators.csrf import csrf_exempt | |
from .response import get_response | |
# Set up logging | |
logger = logging.getLogger(__name__) | |
def process_change(request): | |
logger.info("Change request received") | |
if request.method == "POST": | |
try: | |
logger.info("Processing POST request for content change") | |
data = json.loads(request.body) | |
logger.debug("Request body parsed successfully") | |
user_id = data.get("user_id") | |
prompt = data.get("prompt") | |
content = data.get("content") | |
section = data.get("section") | |
job_description = data.get("job_description") | |
logger.debug(f"Request for user_id: {user_id}, section: {section}") | |
if not all([user_id, prompt, content]): | |
logger.warning("Missing required fields in request") | |
return JsonResponse({"error": "Missing required fields"}, status=400) | |
# Customize processing approach based on section | |
logger.info(f"Customizing instructions for section: {section}") | |
section_specific_instruction = "" | |
if section == "skills" or section == "experience" or section == "projects": | |
section_specific_instruction = "Format achievements using the X-Y-Z method (e.g., 'Accomplished X as measured by Y, by doing Z'). Provide at least one compelling example that demonstrates measurable impact." | |
else: | |
section_specific_instruction = "Incorporate relevant keywords from the job description while avoiding generic buzzwords. Focus on specificity and concrete details that align with ATS screening requirements." | |
combined_prompt = f"Content: {content}\nJob Description: {job_description}\nTask: {prompt}" | |
system_instruction = """As an ATS resume optimizer, your primary task is to revise content to align with job requirements while maintaining absolute truthfulness. All accomplishments must be rewritten using the X-Y-Z method: 'Achieved X, as measured by Y, through Z.' Avoid using placeholders like 'Achieved X, as measured by Y, through Z,' and instead provide specific, quantifiable, and truthful details for every accomplishment. Limit each statement to a maximum of 35 words for clarity and 20 words minimum, precision, and ATS compatibility. Use language that highlights measurable results, impact, and relevance to the role. Include industry-specific keywords and technical terms where appropriate to enhance searchability. | |
Examples of the required format: | |
- 'Developed a Python script that improved audio transcription accuracy by 30% and reduced post-processing time by 35%.' | |
- 'Designed and implemented a caching strategy, reducing API latency by 40% and enhancing user experience.' | |
- 'Increased sales by 25% within six months by optimizing email marketing campaigns.' | |
- 'Automated report generation processes, saving 15 hours of manual effort per week.' | |
- 'Managed a cross-functional team of 10, delivering a product launch 3 weeks ahead of schedule with a 20% reduction in budget.' | |
- 'Trained a machine learning model that improved prediction accuracy by 18% for customer retention analysis.' | |
- 'Optimized database queries, leading to a 50% improvement in system response time.' | |
For accomplishments where quantifiable metrics (Y) are unavailable, focus on qualitative outcomes that clearly showcase value and contribution (e.g., 'Enhanced system scalability to support 2x user growth without performance degradation'). Ensure that the content is concise, impactful, and emphasizes skills and achievements relevant to the job description. | |
The output must strictly adhere to this exact JSON format: | |
{ | |
"modified_content": "Your optimized content here." | |
} | |
The final content must be highly ATS-optimized, prioritize measurable results wherever possible, and avoid vague or incomplete statements. Ensure all outputs are actionable, impactful, and tailored to the specific job description provided.""" | |
# Combine system_instruction with section_specific_instruction | |
combined_system_instruction = ( | |
f"{system_instruction} {section_specific_instruction}" | |
) | |
logger.info("Sending request to get_response function") | |
response_str = get_response(combined_prompt, combined_system_instruction) | |
modified_content = json.loads(response_str) | |
logger.info("Content modification completed successfully") | |
return JsonResponse( | |
{ | |
"user_id": user_id, | |
"modified_content": modified_content["modified_content"], | |
}, | |
status=200, | |
) | |
except json.JSONDecodeError as e: | |
logger.error(f"Invalid JSON format: {e}") | |
return JsonResponse({"error": "Invalid JSON format"}, status=400) | |
except Exception as e: | |
error_msg = f"Error in process_change: {e}" | |
logger.error(error_msg) | |
logger.debug(traceback.format_exc()) | |
return JsonResponse({"error": error_msg}, status=500) | |
else: | |
logger.warning(f"Unsupported method: {request.method}") | |
return JsonResponse({"error": "Only POST requests are allowed"}, status=405) | |