saifeddinemk commited on
Commit
a15fb10
1 Parent(s): 9f26a6c
Files changed (2) hide show
  1. app.py +25 -29
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,9 +1,8 @@
1
- import torch
2
  from fastapi import FastAPI, File, UploadFile, Form
3
- from transformers import GPT2Tokenizer, GPT2Model
4
- from sklearn.metrics.pairwise import cosine_similarity
5
  from typing import List
6
  from fastapi.middleware.cors import CORSMiddleware
 
7
 
8
  # Initialize FastAPI app
9
  app = FastAPI()
@@ -17,21 +16,11 @@ app.add_middleware(
17
  allow_headers=["*"],
18
  )
19
 
20
- # Load GPT-2 model and tokenizer
21
- tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
22
- model = GPT2Model.from_pretrained("gpt2")
23
-
24
- # Helper function to get GPT-2 embeddings
25
- def get_gpt2_embedding(text):
26
- inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
27
- outputs = model(**inputs)
28
- embeddings = torch.mean(outputs.last_hidden_state, dim=1).detach().numpy()
29
- return embeddings
30
-
31
- # Helper function to calculate cosine similarity
32
- def calculate_similarity(embedding1, embedding2):
33
- similarity = cosine_similarity(embedding1, embedding2)
34
- return similarity[0][0]
35
 
36
  # Endpoint to upload CV file and store CV text
37
  @app.post("/upload-cv/")
@@ -43,22 +32,29 @@ async def upload_cv(file: UploadFile = File(...)):
43
  # Endpoint to compare job descriptions with the CV text
44
  @app.post("/compare/")
45
  async def compare_job_cv(job_descriptions: str = Form(...), cv_text: str = Form(...)):
46
- # Generate embedding for the CV text
47
- cv_embedding = get_gpt2_embedding(cv_text)
48
-
49
- # Split job descriptions by line and calculate scores
50
  descriptions = job_descriptions.strip().split("\n")
51
  results = []
52
 
53
  for description in descriptions:
54
- job_embedding = get_gpt2_embedding(description)
55
- similarity_score = calculate_similarity(cv_embedding, job_embedding)
 
 
 
 
 
 
56
 
57
- # Append results
58
- results.append({
59
- "Job Description": description,
60
- "Match Score": round(similarity_score, 2)
61
- })
 
 
 
 
62
 
63
  return {"results": results}
64
 
 
 
1
  from fastapi import FastAPI, File, UploadFile, Form
2
+ from llama_cpp import Llama
 
3
  from typing import List
4
  from fastapi.middleware.cors import CORSMiddleware
5
+ import json
6
 
7
  # Initialize FastAPI app
8
  app = FastAPI()
 
16
  allow_headers=["*"],
17
  )
18
 
19
+ # Load the Llama model
20
+ llm = Llama.from_pretrained(
21
+ repo_id="HuggingFaceTB/SmolLM2-1.7B-Instruct-GGUF",
22
+ filename="smollm2-1.7b-instruct-q4_k_m.gguf",
23
+ )
 
 
 
 
 
 
 
 
 
 
24
 
25
  # Endpoint to upload CV file and store CV text
26
  @app.post("/upload-cv/")
 
32
  # Endpoint to compare job descriptions with the CV text
33
  @app.post("/compare/")
34
  async def compare_job_cv(job_descriptions: str = Form(...), cv_text: str = Form(...)):
35
+ # Split job descriptions by line
 
 
 
36
  descriptions = job_descriptions.strip().split("\n")
37
  results = []
38
 
39
  for description in descriptions:
40
+ # Create chat messages to prompt Llama for each job description
41
+ messages = [
42
+ {"role": "user", "content": f"Compare the following job description with this resume. Job Description: {description}. Resume: {cv_text}. Give a match score and brief analysis."}
43
+ ]
44
+
45
+ # Generate response using Llama
46
+ response = llm.create_chat_completion(messages=messages)
47
+ response_content = response["choices"][0]["message"]["content"]
48
 
49
+ # Parse response content for a score and summary
50
+ try:
51
+ response_data = json.loads(response_content)
52
+ results.append(response_data)
53
+ except json.JSONDecodeError:
54
+ results.append({
55
+ "Job Description": description,
56
+ "Analysis": response_content # Use raw response if JSON parsing fails
57
+ })
58
 
59
  return {"results": results}
60
 
requirements.txt CHANGED
@@ -3,4 +3,5 @@ uvicorn
3
  torch
4
  transformers
5
  scikit-learn
6
- peft
 
 
3
  torch
4
  transformers
5
  scikit-learn
6
+ peft
7
+ git+https://github.com/abetlen/llama-cpp-python.git