File size: 2,326 Bytes
35236b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from typing import List
from langchain_community.chat_models import ChatOpenAI
from utils import process_file_with_dedoc, extract_text_from_all_levels, generate_formatted_resume, \
    generate_json_structured_resume
import shutil
import os

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

ALLOWED_EXTENSIONS = {"jpg", "jpeg", "png", "docx", "pdf", "html", "doc"}


def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS


@app.post("/parse_resume/")
async def parse_resume(files: List[UploadFile] = File(...)):
    parsed_resumes = []

    for uploaded_file in files:
        if allowed_file(uploaded_file.filename):
            chat_llm_text = ChatOpenAI(model='gpt-3.5-turbo', temperature=0.0)
            chat_llm_json = ChatOpenAI(model='gpt-3.5-turbo', temperature=0.4)

            if not os.path.exists("temp"):
                os.makedirs("temp")

            file_path = f"temp/{uploaded_file.filename}"

            # Process the uploaded file asynchronously
            text = await process_file_with_dedoc(uploaded_file)
            text_f = await extract_text_from_all_levels(text)

            # Generate parsed resume and parsed JSON resume asynchronously
            parsed_resume = generate_formatted_resume(text_f, chat_llm_text)

            parsed_json_resume = None
            while parsed_json_resume is None:
                # Execute your code to generate parsed_json_resume
                parsed_json_resume = generate_json_structured_resume(text_f, chat_llm_json)

            parsed_resumes.append({
                "file_name": uploaded_file.filename,
                "parsed_resume": parsed_resume,
                "parsed_json_resume": parsed_json_resume
            })
        else:
            return JSONResponse(status_code=400, content={
                "message": "Invalid file type. Allowed file types are: jpg, jpeg, png, docx, pdf, html, doc"})

    return parsed_resumes


if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app, host="0.0.0.0", port=8000)