astro21 commited on
Commit
35236b5
1 Parent(s): 2c4a5b7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, File, UploadFile
2
+ from fastapi.responses import JSONResponse
3
+ from fastapi.middleware.cors import CORSMiddleware
4
+ from typing import List
5
+ from langchain_community.chat_models import ChatOpenAI
6
+ from utils import process_file_with_dedoc, extract_text_from_all_levels, generate_formatted_resume, \
7
+ generate_json_structured_resume
8
+ import shutil
9
+ import os
10
+
11
+ app = FastAPI()
12
+
13
+ app.add_middleware(
14
+ CORSMiddleware,
15
+ allow_origins=["*"],
16
+ allow_credentials=True,
17
+ allow_methods=["*"],
18
+ allow_headers=["*"],
19
+ )
20
+
21
+ ALLOWED_EXTENSIONS = {"jpg", "jpeg", "png", "docx", "pdf", "html", "doc"}
22
+
23
+
24
+ def allowed_file(filename):
25
+ return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
26
+
27
+
28
+ @app.post("/parse_resume/")
29
+ async def parse_resume(files: List[UploadFile] = File(...)):
30
+ parsed_resumes = []
31
+
32
+ for uploaded_file in files:
33
+ if allowed_file(uploaded_file.filename):
34
+ chat_llm_text = ChatOpenAI(model='gpt-3.5-turbo', temperature=0.0)
35
+ chat_llm_json = ChatOpenAI(model='gpt-3.5-turbo', temperature=0.4)
36
+
37
+ if not os.path.exists("temp"):
38
+ os.makedirs("temp")
39
+
40
+ file_path = f"temp/{uploaded_file.filename}"
41
+
42
+ # Process the uploaded file asynchronously
43
+ text = await process_file_with_dedoc(uploaded_file)
44
+ text_f = await extract_text_from_all_levels(text)
45
+
46
+ # Generate parsed resume and parsed JSON resume asynchronously
47
+ parsed_resume = generate_formatted_resume(text_f, chat_llm_text)
48
+
49
+ parsed_json_resume = None
50
+ while parsed_json_resume is None:
51
+ # Execute your code to generate parsed_json_resume
52
+ parsed_json_resume = generate_json_structured_resume(text_f, chat_llm_json)
53
+
54
+ parsed_resumes.append({
55
+ "file_name": uploaded_file.filename,
56
+ "parsed_resume": parsed_resume,
57
+ "parsed_json_resume": parsed_json_resume
58
+ })
59
+ else:
60
+ return JSONResponse(status_code=400, content={
61
+ "message": "Invalid file type. Allowed file types are: jpg, jpeg, png, docx, pdf, html, doc"})
62
+
63
+ return parsed_resumes
64
+
65
+
66
+ if __name__ == "__main__":
67
+ import uvicorn
68
+
69
+ uvicorn.run(app, host="0.0.0.0", port=8000)