Alexvatti commited on
Commit
6c0bb7c
·
verified ·
1 Parent(s): 952b328

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +18 -24
main.py CHANGED
@@ -21,33 +21,27 @@ app.add_middleware(
21
  allow_headers=["*"],
22
  )
23
 
24
- # Save uploaded file asynchronously
25
- async def save_file(file: UploadFile) -> str:
26
- filename = f"{uuid4()}_{file.filename}"
27
- file_path = os.path.join(UPLOAD_FOLDER, filename)
28
- async with aiofiles.open(file_path, 'wb') as out_file:
29
- content = await file.read()
30
- await out_file.write(content)
31
- return file_path
32
-
33
- # Extract text from PDF using PyPDF2
34
- def extract_text_from_pdf(pdf_path: str) -> str:
35
- text = ""
36
  try:
37
- with open(pdf_path, "rb") as file:
38
- pdf_reader = PyPDF2.PdfReader(file)
39
- for page in pdf_reader.pages:
40
- text += page.extract_text() + "\n"
41
- return text
 
 
 
 
 
 
 
 
42
  except Exception as e:
43
- return f"Error extracting text: {str(e)}"
 
 
44
 
45
- @app.post("/parse-resume")
46
- async def parse_resume(file: UploadFile = File(...)):
47
- path = await save_file(file)
48
- text = extract_text_from_pdf(path)
49
- os.remove(path) # Clean up uploaded file
50
- return JSONResponse(content={"text": text}, media_type="application/json")
51
 
52
  @app.get("/")
53
  async def root():
 
21
  allow_headers=["*"],
22
  )
23
 
24
+ @app.post("/parse-resume")
25
+ async def parse_resume(file: UploadFile = File(...)):
 
 
 
 
 
 
 
 
 
 
26
  try:
27
+ print("🔄 Saving file...")
28
+ path = await save_file(file)
29
+ print(f"✅ File saved at {path}")
30
+
31
+ print("📄 Extracting text...")
32
+ text = extract_text_from_pdf(path)
33
+ print("✅ Text extracted.")
34
+
35
+ os.remove(path)
36
+ print("🧹 File removed.")
37
+
38
+ return JSONResponse(content={"text": text}, media_type="application/json")
39
+
40
  except Exception as e:
41
+ import traceback
42
+ print("❌ Exception occurred:\n", traceback.format_exc())
43
+ return JSONResponse(status_code=500, content={"error": str(e)})
44
 
 
 
 
 
 
 
45
 
46
  @app.get("/")
47
  async def root():