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.0) file_path = f"{uploaded_file.filename}" print(file_path) # 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 }) # Delete the uploaded file after processing # os.remove("/temp_files/"+uploaded_file.filename) # print(f"Deleted file: {uploaded_file.filename}") 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)