Update main.py
Browse files
main.py
CHANGED
@@ -1,152 +1,38 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
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 |
-
|
|
|
|
|
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,50 +43,68 @@ app.add_middleware(
|
|
157 |
allow_headers=["*"],
|
158 |
)
|
159 |
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
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 |
-
#
|
181 |
-
response = model_vision.generate_content(
|
182 |
-
return response
|
|
|
183 |
|
184 |
@app.post("/get_ocr_data/")
|
185 |
-
|
186 |
-
try:
|
187 |
-
|
|
|
188 |
file_type = input_file.content_type
|
|
|
|
|
189 |
|
190 |
-
if file_type
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
191 |
raise HTTPException(status_code=400, detail="Unsupported file type")
|
|
|
|
|
192 |
|
193 |
-
#
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
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",
|
@@ -215,11 +119,19 @@ async def get_data(user_id: str = Query(...), input_file: UploadFile = File(...)
|
|
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 |
-
|
|
|
222 |
return {"data": data}
|
223 |
|
224 |
-
#
|
225 |
-
|
|
|
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 |
+
import uvicorn
|
9 |
+
from fastapi import FastAPI, HTTPException, File, UploadFile,Query
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
from fastapi.middleware.cors import CORSMiddleware
|
11 |
from PyPDF2 import PdfReader
|
12 |
import google.generativeai as genai
|
13 |
import json
|
14 |
from PIL import Image
|
15 |
import io
|
16 |
+
import requests
|
17 |
import fitz # PyMuPDF
|
18 |
import os
|
|
|
19 |
|
20 |
+
|
21 |
+
from dotenv import load_dotenv
|
22 |
+
# Load the environment variables from the .env file
|
23 |
load_dotenv()
|
24 |
+
|
25 |
+
# Configure Gemini API
|
26 |
secret = os.environ["GEMINI"]
|
27 |
genai.configure(api_key=secret)
|
28 |
model_vision = genai.GenerativeModel('gemini-1.5-flash')
|
29 |
model_text = genai.GenerativeModel('gemini-pro')
|
30 |
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
+
|
36 |
app = FastAPI()
|
37 |
|
38 |
app.add_middleware(
|
|
|
43 |
allow_headers=["*"],
|
44 |
)
|
45 |
|
46 |
+
|
47 |
+
|
48 |
+
|
49 |
+
|
50 |
+
def vision(file_content):
|
51 |
+
# Open the PDF
|
52 |
+
pdf_document = fitz.open("pdf",file_content)
|
53 |
+
gemini_input = ["extract the whole text"]
|
54 |
+
# Iterate through the pages
|
|
|
|
|
|
|
|
|
55 |
for page_num in range(len(pdf_document)):
|
56 |
+
# Select the page
|
57 |
page = pdf_document.load_page(page_num)
|
58 |
+
|
59 |
+
# Render the page to a pixmap (image)
|
60 |
pix = page.get_pixmap()
|
61 |
+
print(type(pix))
|
62 |
+
|
63 |
+
# Convert the pixmap to bytes
|
64 |
img_bytes = pix.tobytes("png")
|
65 |
+
|
66 |
+
# Convert bytes to a PIL Image
|
67 |
img = Image.open(io.BytesIO(img_bytes))
|
68 |
gemini_input.append(img)
|
69 |
+
# # Save the image if needed
|
70 |
+
# img.save(f'page_{page_num + 1}.png')
|
71 |
+
|
72 |
+
print("PDF pages converted to images successfully!")
|
73 |
|
74 |
+
# Now you can pass the PIL image to the model_vision
|
75 |
+
response = model_vision.generate_content(gemini_input).text
|
76 |
+
return response
|
77 |
+
|
78 |
|
79 |
@app.post("/get_ocr_data/")
|
80 |
+
def get_data(input_file: UploadFile = File(...)):
|
81 |
+
#try:
|
82 |
+
# Determine the file type by reading the first few bytes
|
83 |
+
file_content = input_file.file.read()
|
84 |
file_type = input_file.content_type
|
85 |
+
|
86 |
+
text = ""
|
87 |
|
88 |
+
if file_type == "application/pdf":
|
89 |
+
# Read PDF file using PyPDF2
|
90 |
+
pdf_reader = PdfReader(io.BytesIO(file_content))
|
91 |
+
for page in pdf_reader.pages:
|
92 |
+
text += page.extract_text()
|
93 |
+
|
94 |
+
if len(text)<10:
|
95 |
+
print("vision called")
|
96 |
+
text = vision(file_content)
|
97 |
+
else:
|
98 |
raise HTTPException(status_code=400, detail="Unsupported file type")
|
99 |
+
|
100 |
+
|
101 |
|
102 |
+
# Call Gemini (or another model) to extract required data
|
103 |
+
prompt = f"""This is CV data: {text.strip()}
|
104 |
+
IMPORTANT: The output should be a JSON array! Make Sure the JSON is valid.
|
105 |
+
|
106 |
+
Example Output:
|
107 |
+
[
|
|
|
|
|
|
|
|
|
|
|
108 |
"firstname" : "firstname",
|
109 |
"lastname" : "lastname",
|
110 |
"email" : "email",
|
|
|
119 |
"skills" : skills(Identify and list specific skills mentioned in both the skills section and inferred from the experience section),
|
120 |
"positions": [ "Job title 1", "Job title 2", "Job title 3" ],
|
121 |
"summary": "Generate a summary of the CV, including key qualifications, notable experiences, and relevant skills."
|
122 |
+
|
123 |
+
|
124 |
+
|
125 |
+
|
126 |
+
|
127 |
+
|
128 |
+
]
|
129 |
+
"""
|
130 |
+
|
131 |
response = model_text.generate_content(prompt)
|
132 |
+
print(response.text)
|
133 |
+
data = json.loads(response.text.replace("JSON", "").replace("json", "").replace("```", ""))
|
134 |
return {"data": data}
|
135 |
|
136 |
+
#except Exception as e:
|
137 |
+
#raise HTTPException(status_code=500, detail=f"Error processing file: {str(e)}")
|