-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
74 lines (57 loc) · 2.17 KB
/
main.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from fastapi import FastAPI, File, UploadFile, Form
from fastapi.middleware.cors import CORSMiddleware
import os, io, base64
from PIL import Image
import pdf2image
import google.generativeai as genai
from dotenv import load_dotenv
load_dotenv()
from langchain.chat_models import ChatOpenAI
llm = ChatOpenAI(openai_api_key=os.getenv("OpenAI"))
app = FastAPI(title="ATS")
# Configure CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
input_prompt3 = """
You are an skilled ATS (Applicant Tracking System) scanner with a deep understanding of data science and ATS functionality,
your task is to evaluate the resume against the provided job description. give me the percentage of match if the resume matches
First the output should come as percentage and then keywords missing and last final thoughts.
"""
@app.get("/")
def read_root():
return {"Hello": "World"}
def get_gemini_response(input, pdf_cotent, prompt):
model = genai.GenerativeModel("gemini-pro-vision")
response = model.generate_content([input, pdf_cotent[0], prompt])
return response.text
@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile = File(...), JD: str = Form(...)):
contents = await file.read()
pdf_content = await pdf_tokens(contents) # Call pdf_tokens and await its result
response = get_gemini_response(JD, pdf_content, input_prompt3)
processed_response = llm.invoke(response)
return {
# "filename": file.filename,
# "text": JD,
# "file_size": len(contents),
"res": response,
"langsmit": processed_response,
}
async def pdf_tokens(contents):
if contents is not None:
images = pdf2image.convert_from_bytes(contents)
first_page = images[0]
img_byte_arr = io.BytesIO()
first_page.save(img_byte_arr, format="JPEG")
img_byte_arr = img_byte_arr.getvalue()
pdf_parts = [
{"mime_type": "image/jpeg", "data": base64.b64encode(img_byte_arr).decode()}
]
return pdf_parts
else:
raise FileNotFoundError("no file uploaded!!")