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