Update main.py
Browse files
main.py
CHANGED
@@ -1,39 +1,152 @@
|
|
1 |
-
try: from pip._internal.operations import freeze
|
2 |
-
except ImportError: # pip < 10.0
|
3 |
-
|
4 |
-
|
5 |
-
pkgs = freeze.freeze()
|
6 |
-
for pkg in pkgs: print(pkg)
|
7 |
-
import os
|
8 |
-
from fastapi import FastAPI, HTTPException, File, UploadFile,Query
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
from fastapi.middleware.cors import CORSMiddleware
|
10 |
from PyPDF2 import PdfReader
|
11 |
import google.generativeai as genai
|
12 |
import json
|
13 |
-
import base64
|
14 |
-
from io import BytesIO
|
15 |
from PIL import Image
|
16 |
import io
|
17 |
-
import requests
|
18 |
import fitz # PyMuPDF
|
19 |
import os
|
20 |
-
|
21 |
-
|
22 |
from dotenv import load_dotenv
|
23 |
-
# Load the environment variables from the .env file
|
24 |
-
load_dotenv()
|
25 |
|
26 |
-
#
|
|
|
27 |
secret = os.environ["GEMINI"]
|
28 |
genai.configure(api_key=secret)
|
29 |
model_vision = genai.GenerativeModel('gemini-1.5-flash')
|
30 |
model_text = genai.GenerativeModel('gemini-pro')
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
app = FastAPI()
|
38 |
|
39 |
app.add_middleware(
|
@@ -44,87 +157,69 @@ app.add_middleware(
|
|
44 |
allow_headers=["*"],
|
45 |
)
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
56 |
for page_num in range(len(pdf_document)):
|
57 |
-
# Select the page
|
58 |
page = pdf_document.load_page(page_num)
|
59 |
-
|
60 |
-
# Render the page to a pixmap (image)
|
61 |
pix = page.get_pixmap()
|
62 |
-
print(type(pix))
|
63 |
-
|
64 |
-
# Convert the pixmap to bytes
|
65 |
img_bytes = pix.tobytes("png")
|
66 |
-
|
67 |
-
# Convert bytes to a PIL Image
|
68 |
img = Image.open(io.BytesIO(img_bytes))
|
69 |
gemini_input.append(img)
|
70 |
-
# # Save the image if needed
|
71 |
-
# img.save(f'page_{page_num + 1}.png')
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
response = model_vision.generate_content(gemini_input).text
|
77 |
-
return response
|
78 |
-
|
79 |
|
80 |
@app.post("/get_ocr_data/")
|
81 |
-
async def get_data(input_file: UploadFile = File(...)):
|
82 |
-
|
83 |
-
# Determine the file type by reading the first few bytes
|
84 |
file_content = await input_file.read()
|
85 |
file_type = input_file.content_type
|
86 |
-
|
87 |
-
text = ""
|
88 |
|
89 |
-
if file_type
|
90 |
-
# Read PDF file using PyPDF2
|
91 |
-
pdf_reader = PdfReader(io.BytesIO(file_content))
|
92 |
-
for page in pdf_reader.pages:
|
93 |
-
text += page.extract_text()
|
94 |
-
|
95 |
-
if len(text)<10:
|
96 |
-
print("vision called")
|
97 |
-
text = vision(file_content)
|
98 |
-
else:
|
99 |
raise HTTPException(status_code=400, detail="Unsupported file type")
|
100 |
|
101 |
-
#
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
|
|
|
|
|
|
|
|
|
|
107 |
"firstname" : "firstname",
|
108 |
"lastname" : "lastname",
|
109 |
-
"gender" : "gender",
|
110 |
"email" : "email",
|
111 |
"contact_number" : "contact number",
|
112 |
-
"age" : "age",
|
113 |
"home_address" : "full home address",
|
114 |
"home_town" : "home town or city",
|
115 |
"total_years_of_experience" : "total years of experience",
|
|
|
116 |
"LinkedIn_link" : "LinkedIn link",
|
117 |
-
"positions": [ "Job title 1", "Job title 2", "Job title 3" ],
|
118 |
-
"industry": "industry of work",
|
119 |
"experience" : "experience",
|
120 |
-
"
|
121 |
-
|
122 |
-
|
123 |
-
|
|
|
|
|
124 |
response = model_text.generate_content(prompt)
|
125 |
-
|
126 |
-
data = json.loads(response.text.replace("JSON", "").replace("json", "").replace("```", ""))
|
127 |
return {"data": data}
|
128 |
|
129 |
-
|
130 |
-
|
|
|
1 |
+
# try: from pip._internal.operations import freeze
|
2 |
+
# except ImportError: # pip < 10.0
|
3 |
+
# from pip.operations import freeze
|
4 |
+
|
5 |
+
# pkgs = freeze.freeze()
|
6 |
+
# for pkg in pkgs: print(pkg)
|
7 |
+
# import os
|
8 |
+
# from fastapi import FastAPI, HTTPException, File, UploadFile,Query
|
9 |
+
# from fastapi.middleware.cors import CORSMiddleware
|
10 |
+
# from PyPDF2 import PdfReader
|
11 |
+
# import google.generativeai as genai
|
12 |
+
# import json
|
13 |
+
# import base64
|
14 |
+
# from io import BytesIO
|
15 |
+
# from PIL import Image
|
16 |
+
# import io
|
17 |
+
# import requests
|
18 |
+
# import fitz # PyMuPDF
|
19 |
+
# import os
|
20 |
+
|
21 |
+
|
22 |
+
# from dotenv import load_dotenv
|
23 |
+
# # Load the environment variables from the .env file
|
24 |
+
# load_dotenv()
|
25 |
+
|
26 |
+
# # Configure Gemini API
|
27 |
+
# secret = os.environ["GEMINI"]
|
28 |
+
# genai.configure(api_key=secret)
|
29 |
+
# model_vision = genai.GenerativeModel('gemini-1.5-flash')
|
30 |
+
# model_text = genai.GenerativeModel('gemini-pro')
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
+
|
36 |
+
|
37 |
+
# app = FastAPI()
|
38 |
+
|
39 |
+
# app.add_middleware(
|
40 |
+
# CORSMiddleware,
|
41 |
+
# allow_origins=["*"],
|
42 |
+
# allow_credentials=True,
|
43 |
+
# allow_methods=["*"],
|
44 |
+
# allow_headers=["*"],
|
45 |
+
# )
|
46 |
+
|
47 |
+
|
48 |
+
|
49 |
+
|
50 |
+
|
51 |
+
# def vision(file_content):
|
52 |
+
# # Open the PDF
|
53 |
+
# pdf_document = fitz.open("pdf",file_content)
|
54 |
+
# gemini_input = ["extract the whole text"]
|
55 |
+
# # Iterate through the pages
|
56 |
+
# for page_num in range(len(pdf_document)):
|
57 |
+
# # Select the page
|
58 |
+
# page = pdf_document.load_page(page_num)
|
59 |
+
|
60 |
+
# # Render the page to a pixmap (image)
|
61 |
+
# pix = page.get_pixmap()
|
62 |
+
# print(type(pix))
|
63 |
+
|
64 |
+
# # Convert the pixmap to bytes
|
65 |
+
# img_bytes = pix.tobytes("png")
|
66 |
+
|
67 |
+
# # Convert bytes to a PIL Image
|
68 |
+
# img = Image.open(io.BytesIO(img_bytes))
|
69 |
+
# gemini_input.append(img)
|
70 |
+
# # # Save the image if needed
|
71 |
+
# # img.save(f'page_{page_num + 1}.png')
|
72 |
+
|
73 |
+
# print("PDF pages converted to images successfully!")
|
74 |
+
|
75 |
+
# # Now you can pass the PIL image to the model_vision
|
76 |
+
# response = model_vision.generate_content(gemini_input).text
|
77 |
+
# return response
|
78 |
+
|
79 |
+
|
80 |
+
# @app.post("/get_ocr_data/")
|
81 |
+
# async def get_data(input_file: UploadFile = File(...)):
|
82 |
+
# #try:
|
83 |
+
# # Determine the file type by reading the first few bytes
|
84 |
+
# file_content = await input_file.read()
|
85 |
+
# file_type = input_file.content_type
|
86 |
+
|
87 |
+
# text = ""
|
88 |
+
|
89 |
+
# if file_type == "application/pdf":
|
90 |
+
# # Read PDF file using PyPDF2
|
91 |
+
# pdf_reader = PdfReader(io.BytesIO(file_content))
|
92 |
+
# for page in pdf_reader.pages:
|
93 |
+
# text += page.extract_text()
|
94 |
+
|
95 |
+
# if len(text)<10:
|
96 |
+
# print("vision called")
|
97 |
+
# text = vision(file_content)
|
98 |
+
# else:
|
99 |
+
# raise HTTPException(status_code=400, detail="Unsupported file type")
|
100 |
+
|
101 |
+
# # Call Gemini (or another model) to extract required data
|
102 |
+
# prompt = f"""This is CV data: {text.strip()}
|
103 |
+
# IMPORTANT: The output should be a JSON array! Make Sure the JSON is valid.
|
104 |
+
|
105 |
+
# Example Output:
|
106 |
+
# [
|
107 |
+
# "firstname" : "firstname",
|
108 |
+
# "lastname" : "lastname",
|
109 |
+
# "gender" : "gender",
|
110 |
+
# "email" : "email",
|
111 |
+
# "contact_number" : "contact number",
|
112 |
+
# "age" : "age",
|
113 |
+
# "home_address" : "full home address",
|
114 |
+
# "home_town" : "home town or city",
|
115 |
+
# "total_years_of_experience" : "total years of experience",
|
116 |
+
# "LinkedIn_link" : "LinkedIn link",
|
117 |
+
# "positions": [ "Job title 1", "Job title 2", "Job title 3" ],
|
118 |
+
# "industry": "industry of work",
|
119 |
+
# "experience" : "experience",
|
120 |
+
# "skills" : Skills(Identify and list specific skills mentioned in both the skills section and inferred from the experience section)
|
121 |
+
# ]
|
122 |
+
# """
|
123 |
+
|
124 |
+
# response = model_text.generate_content(prompt)
|
125 |
+
# print(response.text)
|
126 |
+
# data = json.loads(response.text.replace("JSON", "").replace("json", "").replace("```", ""))
|
127 |
+
# return {"data": data}
|
128 |
+
|
129 |
+
# #except Exception as e:
|
130 |
+
# #raise HTTPException(status_code=500, detail=f"Error processing file: {str(e)}")
|
131 |
+
|
132 |
+
from fastapi import FastAPI, HTTPException, File, UploadFile, Query
|
133 |
from fastapi.middleware.cors import CORSMiddleware
|
134 |
from PyPDF2 import PdfReader
|
135 |
import google.generativeai as genai
|
136 |
import json
|
|
|
|
|
137 |
from PIL import Image
|
138 |
import io
|
|
|
139 |
import fitz # PyMuPDF
|
140 |
import os
|
|
|
|
|
141 |
from dotenv import load_dotenv
|
|
|
|
|
142 |
|
143 |
+
# Load environment variables
|
144 |
+
load_dotenv()
|
145 |
secret = os.environ["GEMINI"]
|
146 |
genai.configure(api_key=secret)
|
147 |
model_vision = genai.GenerativeModel('gemini-1.5-flash')
|
148 |
model_text = genai.GenerativeModel('gemini-pro')
|
149 |
|
|
|
|
|
|
|
|
|
|
|
150 |
app = FastAPI()
|
151 |
|
152 |
app.add_middleware(
|
|
|
157 |
allow_headers=["*"],
|
158 |
)
|
159 |
|
160 |
+
def process_pdf_text(file_content):
|
161 |
+
"""Extract text from PDF using PyPDF2."""
|
162 |
+
pdf_reader = PdfReader(io.BytesIO(file_content))
|
163 |
+
text = ""
|
164 |
+
for page in pdf_reader.pages:
|
165 |
+
text += page.extract_text()
|
166 |
+
return text
|
167 |
+
|
168 |
+
def process_pdf_images(file_content):
|
169 |
+
"""Extract images from PDF and pass to Gemini Vision."""
|
170 |
+
pdf_document = fitz.open("pdf", file_content)
|
171 |
+
gemini_input = []
|
172 |
+
|
173 |
for page_num in range(len(pdf_document)):
|
|
|
174 |
page = pdf_document.load_page(page_num)
|
|
|
|
|
175 |
pix = page.get_pixmap()
|
|
|
|
|
|
|
176 |
img_bytes = pix.tobytes("png")
|
|
|
|
|
177 |
img = Image.open(io.BytesIO(img_bytes))
|
178 |
gemini_input.append(img)
|
|
|
|
|
179 |
|
180 |
+
# Call Gemini Vision with extracted images
|
181 |
+
response = model_vision.generate_content(["extract the whole text", *gemini_input])
|
182 |
+
return response.text
|
|
|
|
|
|
|
183 |
|
184 |
@app.post("/get_ocr_data/")
|
185 |
+
async def get_data(user_id: str = Query(...), input_file: UploadFile = File(...)):
|
186 |
+
try:
|
|
|
187 |
file_content = await input_file.read()
|
188 |
file_type = input_file.content_type
|
|
|
|
|
189 |
|
190 |
+
if file_type != "application/pdf":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
191 |
raise HTTPException(status_code=400, detail="Unsupported file type")
|
192 |
|
193 |
+
# Process PDF
|
194 |
+
text = process_pdf_text(file_content)
|
195 |
+
if len(text.strip()) < 10: # Fallback to image-based OCR if text is minimal
|
196 |
+
text = process_pdf_images(file_content)
|
197 |
+
|
198 |
+
# Call Gemini Text model
|
199 |
+
prompt = f"""
|
200 |
+
This is CV data: {text.strip()}
|
201 |
+
IMPORTANT: The output should be a JSON array! Make sure the JSON is valid.
|
202 |
+
Example Output:
|
203 |
+
[
|
204 |
"firstname" : "firstname",
|
205 |
"lastname" : "lastname",
|
|
|
206 |
"email" : "email",
|
207 |
"contact_number" : "contact number",
|
|
|
208 |
"home_address" : "full home address",
|
209 |
"home_town" : "home town or city",
|
210 |
"total_years_of_experience" : "total years of experience",
|
211 |
+
"education": "Institution Name, Degree Name",
|
212 |
"LinkedIn_link" : "LinkedIn link",
|
|
|
|
|
213 |
"experience" : "experience",
|
214 |
+
"industry": "industry of work",
|
215 |
+
"skills" : skills(Identify and list specific skills mentioned in both the skills section and inferred from the experience section),
|
216 |
+
"positions": [ "Job title 1", "Job title 2", "Job title 3" ],
|
217 |
+
"summary": "Generate a summary of the CV, including key qualifications, notable experiences, and relevant skills."
|
218 |
+
]
|
219 |
+
"""
|
220 |
response = model_text.generate_content(prompt)
|
221 |
+
data = json.loads(response.text.replace("```", "")) # Sanitize response
|
|
|
222 |
return {"data": data}
|
223 |
|
224 |
+
except Exception as e:
|
225 |
+
raise HTTPException(status_code=500, detail=f"Error processing file: {str(e)}")
|