Nasma commited on
Commit
a952212
·
verified ·
1 Parent(s): b304fd9

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +142 -0
main.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ import os
3
+ import io
4
+ import json
5
+ from fastapi import FastAPI, HTTPException, File, UploadFile
6
+ 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
+ #from openai import OpenAI
13
+
14
+
15
+
16
+ # Load environment variables
17
+ load_dotenv()
18
+ openai.api_key = os.environ["OPENAI_API_KEY"]
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.")
23
+
24
+ app = FastAPI()
25
+
26
+ app.add_middleware(
27
+ CORSMiddleware,
28
+ allow_origins=["*"],
29
+ allow_credentials=True,
30
+ allow_methods=["*"],
31
+ allow_headers=["*"],
32
+ )
33
+
34
+
35
+ def vision(file_content):
36
+ pdf_document = fitz.open("pdf", file_content)
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
+ img_bytes = pix.tobytes("png")
49
+ img = Image.open(io.BytesIO(img_bytes))
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
+ vision_data.append(
57
+ {
58
+ "type": "image_url",
59
+ "image_url": {"url": f"data:image/jpeg;base64,{img_base64}"},
60
+ }
61
+ )
62
+
63
+ print("PDF pages converted to images successfully!")
64
+
65
+ # Use GPT-4 to process the images (textual representation)
66
+ try:
67
+ response = openai.ChatCompletion.create(
68
+ model="gpt-4o-mini",
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 generating response: {str(e)}")
81
+
82
+
83
+ @app.post("/get_ocr_data/")
84
+ def get_data(input_file: UploadFile = File(...)):
85
+ #try:
86
+ # Read the uploaded file
87
+ file_content = input_file.file.read()
88
+ file_type = input_file.content_type
89
+ text = ""
90
+
91
+ if file_type == "application/pdf":
92
+ pdf_reader = PdfReader(io.BytesIO(file_content))
93
+ for page in pdf_reader.pages:
94
+ text += page.extract_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
+ print(text.strip())
102
+ # Call GPT-4o to process extracted text into structured JSON
103
+ prompt = f"""This is CV data: {text.strip()}.
104
+ IMPORTANT: The output should be a JSON array! Make Sure the JSON is valid.if you not found data then fill with "none" don't add any extra explaition text
105
+ need only json
106
+ Example Output:
107
+ ```json
108
+ [
109
+ "firstname": "firstname",
110
+ "lastname": "lastname",
111
+ "email": "email",
112
+ "contact_number": "contact number",
113
+ "home_address": "full home address",
114
+ "home_town": "home town or city",
115
+ "total_years_of_experience": "total years of experience",
116
+ "education": "Institution Name, Country, Degree Name, Graduation Year; Institution Name, Country, Degree Name, Graduation Year",
117
+ "LinkedIn_link": "LinkedIn link",
118
+ "experience": "experience",
119
+ "industry": "industry of work",
120
+ "skills": "skills(Identify and list specific skills mentioned in both the skills section and inferred from the experience section), formatted as: Skill 1, Skill 2, Skill 3, Skill 4, Skill 5",
121
+ "positions": ["Job title 1, Job title 2, Job title 3"]
122
+ ]
123
+ ```
124
+ """
125
+
126
+ response = openai.ChatCompletion.create(
127
+ model="gpt-4o",
128
+ messages=[
129
+ {"role": "system", "content": """You are an assistant processing CV data and formatting it into structured JSON."""
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
+ #except Exception as e:
142
+ #raise HTTPException(status_code=500, detail=f"Error processing file: {str(e)}")