Spaces:
Sleeping
Sleeping
File size: 2,430 Bytes
91207a8 e91e514 91207a8 71a0d39 91207a8 71a0d39 91207a8 71a0d39 91207a8 |
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 |
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel, PeftConfig
import torch
# Initialize the FastAPI app
app = FastAPI()
# Load model and tokenizer once at startup
base_model_name = "akjindal53244/Llama-3.1-Storm-8B"
peft_model_id = "LlamaFactoryAI/cv-job-description-matching"
base_model = AutoModelForCausalLM.from_pretrained(base_model_name, torch_dtype=torch.float16)
model = PeftModel.from_pretrained(base_model, peft_model_id, torch_dtype=torch.float16)
tokenizer = AutoTokenizer.from_pretrained(base_model_name)
config = PeftConfig.from_pretrained(peft_model_id)
# Define request model
class AnalysisRequest(BaseModel):
cv: str
job_description: str
@app.post("/analyze")
async def analyze(request: AnalysisRequest):
try:
# Prepare input text with formatted message
system_prompt = """
You are an advanced AI model designed to analyze the compatibility between a CV and a job description. You will receive a CV and a job description. Your task is to output a structured JSON format that includes the following:
1. matching_analysis: Analyze the CV against the job description to identify key strengths and gaps.
2. description: Summarize the relevance of the CV to the job description in a few concise sentences.
3. score: Provide a numerical compatibility score (0-100) based on qualifications, skills, and experience.
4. recommendation: Suggest actions for the candidate to improve their match or readiness for the role.
Your output must be in JSON format as follows:
{
"matching_analysis": "Your detailed analysis here.",
"description": "A brief summary here.",
"score": 85,
"recommendation": "Your suggestions here."
}
"""
user_input = f"<CV> {request.cv} </CV>\n<job_description> {request.job_description} </job_description>"
input_text = system_prompt + user_input
# Tokenize and generate response
inputs = tokenizer(input_text, return_tensors="pt")
outputs = model.generate(**inputs, max_new_tokens=64)
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
return {"analysis": generated_text}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
|