Update main.py
Browse files
main.py
CHANGED
|
@@ -7,16 +7,13 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
| 7 |
from PyPDF2 import PdfReader
|
| 8 |
from PIL import Image
|
| 9 |
import fitz # PyMuPDF
|
| 10 |
-
from dotenv import load_dotenv
|
| 11 |
import openai
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
# Load environment variables
|
| 17 |
load_dotenv()
|
| 18 |
-
openai.api_key = os.
|
| 19 |
-
#client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
|
| 20 |
|
| 21 |
if not openai.api_key:
|
| 22 |
raise RuntimeError("Missing OpenAI API key. Please set OPENAI_API_KEY in the environment variables.")
|
|
@@ -33,110 +30,108 @@ app.add_middleware(
|
|
| 33 |
|
| 34 |
|
| 35 |
def vision(file_content):
|
| 36 |
-
|
|
|
|
| 37 |
base64_images = []
|
| 38 |
-
vision_data = [
|
| 39 |
-
"type": "text",
|
| 40 |
-
"text": "extract the all text from this images",
|
| 41 |
-
}
|
| 42 |
-
]
|
| 43 |
|
| 44 |
-
# Convert PDF pages to images
|
| 45 |
for page_num in range(len(pdf_document)):
|
| 46 |
page = pdf_document.load_page(page_num)
|
| 47 |
pix = page.get_pixmap()
|
| 48 |
-
|
| 49 |
-
|
|
|
|
| 50 |
|
| 51 |
# Convert the image to base64
|
| 52 |
buffered = io.BytesIO()
|
| 53 |
img.save(buffered, format="PNG")
|
| 54 |
img_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 55 |
base64_images.append(img_base64)
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
)
|
| 62 |
|
| 63 |
print("PDF pages converted to images successfully!")
|
| 64 |
|
| 65 |
-
#
|
| 66 |
try:
|
| 67 |
response = openai.ChatCompletion.create(
|
| 68 |
-
model="gpt-4o
|
| 69 |
-
messages=[
|
| 70 |
-
{
|
| 71 |
-
"role": "user",
|
| 72 |
-
"content": vision_data,
|
| 73 |
-
}
|
| 74 |
-
],
|
| 75 |
)
|
| 76 |
-
|
| 77 |
-
print(response.choices[0]["message"]["content"])
|
| 78 |
-
return response.choices[0]["message"]["content"]
|
| 79 |
except Exception as e:
|
| 80 |
-
raise HTTPException(status_code=500, detail=f"Error
|
| 81 |
|
| 82 |
|
| 83 |
@app.post("/get_ocr_data/")
|
| 84 |
def get_data(input_file: UploadFile = File(...)):
|
| 85 |
-
|
|
|
|
| 86 |
# Read the uploaded file
|
| 87 |
file_content = input_file.file.read()
|
| 88 |
file_type = input_file.content_type
|
| 89 |
-
|
| 90 |
|
| 91 |
if file_type == "application/pdf":
|
| 92 |
pdf_reader = PdfReader(io.BytesIO(file_content))
|
|
|
|
| 93 |
for page in pdf_reader.pages:
|
| 94 |
-
text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
if len(text.strip()): # If PDF text extraction is insufficient
|
| 97 |
-
print("\nvision running..........................\n")
|
| 98 |
-
text = vision(file_content)
|
| 99 |
else:
|
| 100 |
raise HTTPException(status_code=400, detail="Unsupported file type")
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
| 106 |
Example Output:
|
| 107 |
```json
|
| 108 |
-
|
| 109 |
-
"firstname": "
|
| 110 |
-
"lastname": "
|
| 111 |
-
"email": "
|
| 112 |
-
"contact_number": "
|
| 113 |
-
"home_address": "
|
| 114 |
-
"home_town": "
|
| 115 |
-
"total_years_of_experience": "
|
| 116 |
-
"education": "Institution Name,
|
| 117 |
-
"LinkedIn_link": "LinkedIn
|
| 118 |
-
"experience": "
|
| 119 |
"industry": "industry of work",
|
| 120 |
-
"skills": "
|
| 121 |
-
"positions": ["Job
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
"""
|
| 125 |
|
| 126 |
response = openai.ChatCompletion.create(
|
| 127 |
model="gpt-4o",
|
| 128 |
messages=[
|
| 129 |
-
{"role": "system", "content": "
|
| 130 |
-
},
|
| 131 |
{"role": "user", "content": prompt}
|
| 132 |
]
|
| 133 |
)
|
| 134 |
-
data = (response["choices"][0]["message"]["content"]).replace("json","").replace("```","")
|
| 135 |
-
print(data)
|
| 136 |
-
data = json.loads(data)
|
| 137 |
-
#data = response["choices"][0]["message"]["content"]
|
| 138 |
-
|
| 139 |
-
return {"data": data}
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from PyPDF2 import PdfReader
|
| 8 |
from PIL import Image
|
| 9 |
import fitz # PyMuPDF
|
|
|
|
| 10 |
import openai
|
| 11 |
+
import pytesseract
|
| 12 |
+
from dotenv import load_dotenv
|
|
|
|
| 13 |
|
| 14 |
# Load environment variables
|
| 15 |
load_dotenv()
|
| 16 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
| 17 |
|
| 18 |
if not openai.api_key:
|
| 19 |
raise RuntimeError("Missing OpenAI API key. Please set OPENAI_API_KEY in the environment variables.")
|
|
|
|
| 30 |
|
| 31 |
|
| 32 |
def vision(file_content):
|
| 33 |
+
"""Extract text from images inside a PDF using PyMuPDF & OCR."""
|
| 34 |
+
pdf_document = fitz.open(stream=file_content, filetype="pdf")
|
| 35 |
base64_images = []
|
| 36 |
+
vision_data = [{"type": "text", "text": "Extract all text from these images."}]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
|
|
|
| 38 |
for page_num in range(len(pdf_document)):
|
| 39 |
page = pdf_document.load_page(page_num)
|
| 40 |
pix = page.get_pixmap()
|
| 41 |
+
|
| 42 |
+
# Convert the image to a PIL image
|
| 43 |
+
img = Image.open(io.BytesIO(pix.tobytes("png")))
|
| 44 |
|
| 45 |
# Convert the image to base64
|
| 46 |
buffered = io.BytesIO()
|
| 47 |
img.save(buffered, format="PNG")
|
| 48 |
img_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 49 |
base64_images.append(img_base64)
|
| 50 |
+
|
| 51 |
+
vision_data.append({
|
| 52 |
+
"type": "image_url",
|
| 53 |
+
"image_url": {"url": f"data:image/png;base64,{img_base64}"},
|
| 54 |
+
})
|
|
|
|
| 55 |
|
| 56 |
print("PDF pages converted to images successfully!")
|
| 57 |
|
| 58 |
+
# Send images to GPT-4o for processing
|
| 59 |
try:
|
| 60 |
response = openai.ChatCompletion.create(
|
| 61 |
+
model="gpt-4o",
|
| 62 |
+
messages=[{"role": "user", "content": vision_data}],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
)
|
| 64 |
+
return response["choices"][0]["message"]["content"]
|
|
|
|
|
|
|
| 65 |
except Exception as e:
|
| 66 |
+
raise HTTPException(status_code=500, detail=f"Error in GPT-4o vision processing: {str(e)}")
|
| 67 |
|
| 68 |
|
| 69 |
@app.post("/get_ocr_data/")
|
| 70 |
def get_data(input_file: UploadFile = File(...)):
|
| 71 |
+
"""Extract structured data from a PDF resume."""
|
| 72 |
+
try:
|
| 73 |
# Read the uploaded file
|
| 74 |
file_content = input_file.file.read()
|
| 75 |
file_type = input_file.content_type
|
| 76 |
+
extracted_text = ""
|
| 77 |
|
| 78 |
if file_type == "application/pdf":
|
| 79 |
pdf_reader = PdfReader(io.BytesIO(file_content))
|
| 80 |
+
|
| 81 |
for page in pdf_reader.pages:
|
| 82 |
+
text = page.extract_text()
|
| 83 |
+
if text:
|
| 84 |
+
extracted_text += text + "\n"
|
| 85 |
+
|
| 86 |
+
if not extracted_text.strip(): # If no text found, use vision processing
|
| 87 |
+
print("\nVision OCR running...\n")
|
| 88 |
+
extracted_text = vision(file_content)
|
| 89 |
|
|
|
|
|
|
|
|
|
|
| 90 |
else:
|
| 91 |
raise HTTPException(status_code=400, detail="Unsupported file type")
|
| 92 |
+
|
| 93 |
+
print("Extracted Text:\n", extracted_text.strip())
|
| 94 |
+
|
| 95 |
+
# Call GPT-4o to structure extracted text into JSON format
|
| 96 |
+
prompt = f"""This is CV data: {extracted_text.strip()}.
|
| 97 |
+
IMPORTANT: The output should be a JSON array! Make sure the JSON is valid.
|
| 98 |
+
If no data is found, fill missing fields with "none". Do not include extra explanation text.
|
| 99 |
+
|
| 100 |
Example Output:
|
| 101 |
```json
|
| 102 |
+
{{
|
| 103 |
+
"firstname": "First Name",
|
| 104 |
+
"lastname": "Last Name",
|
| 105 |
+
"email": "Email Address",
|
| 106 |
+
"contact_number": "Contact Number",
|
| 107 |
+
"home_address": "Full Home Address",
|
| 108 |
+
"home_town": "Home Town or City",
|
| 109 |
+
"total_years_of_experience": "Total Years of Experience",
|
| 110 |
+
"education": "Institution Name, Degree Name",
|
| 111 |
+
"LinkedIn_link": "LinkedIn URL",
|
| 112 |
+
"experience": "Job Title, Start Date - End Date, Company Name; Job Title, Start Date - End Date, Company Name; Job Title, Start Date - End Date, Company Name",
|
| 113 |
"industry": "industry of work",
|
| 114 |
+
"skills": "Skill 1, Skill 2, Skill 3",
|
| 115 |
+
"positions": ["Job Title 1", "Job Title 2"],
|
| 116 |
+
"summary": "Summary of qualifications and experience"
|
| 117 |
+
}}
|
| 118 |
+
```"""
|
| 119 |
|
| 120 |
response = openai.ChatCompletion.create(
|
| 121 |
model="gpt-4o",
|
| 122 |
messages=[
|
| 123 |
+
{"role": "system", "content": "You are an assistant that processes CV data into structured JSON."},
|
|
|
|
| 124 |
{"role": "user", "content": prompt}
|
| 125 |
]
|
| 126 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
+
# Ensure valid JSON output
|
| 129 |
+
json_response = response["choices"][0]["message"]["content"].replace("json", "").replace("```", "").strip()
|
| 130 |
+
structured_data = json.loads(json_response)
|
| 131 |
+
|
| 132 |
+
return {"data": structured_data}
|
| 133 |
+
|
| 134 |
+
except Exception as e:
|
| 135 |
+
raise HTTPException(status_code=500, detail=f"Error processing file: {str(e)}")
|
| 136 |
+
|
| 137 |
+
|