|
try: from pip._internal.operations import freeze |
|
except ImportError: |
|
from pip.operations import freeze |
|
|
|
pkgs = freeze.freeze() |
|
for pkg in pkgs: print(pkg) |
|
import os |
|
from fastapi import FastAPI, HTTPException, File, UploadFile |
|
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 |
|
import os |
|
import jwt |
|
|
|
|
|
from dotenv import load_dotenv |
|
|
|
load_dotenv() |
|
|
|
|
|
secret = os.environ["GEMINI"] |
|
SECRET_KEY = os.environ["SECRET_KEY"] |
|
genai.configure(api_key=secret) |
|
model_vision = genai.GenerativeModel('gemini-1.5-flash') |
|
model_text = genai.GenerativeModel('gemini-1.5-flash') |
|
|
|
|
|
|
|
|
|
|
|
|
|
app = FastAPI() |
|
|
|
app.add_middleware( |
|
CORSMiddleware, |
|
allow_origins=["*"], |
|
allow_credentials=True, |
|
allow_methods=["*"], |
|
allow_headers=["*"], |
|
) |
|
|
|
|
|
|
|
|
|
|
|
def vision(file_content): |
|
|
|
pdf_document = fitz.open("pdf",file_content) |
|
gemini_input = ["extract the whole text"] |
|
|
|
for page_num in range(len(pdf_document)): |
|
|
|
page = pdf_document.load_page(page_num) |
|
|
|
|
|
pix = page.get_pixmap() |
|
print(type(pix)) |
|
|
|
|
|
img_bytes = pix.tobytes("png") |
|
|
|
|
|
img = Image.open(io.BytesIO(img_bytes)) |
|
gemini_input.append(img) |
|
|
|
|
|
|
|
print("PDF pages converted to images successfully!") |
|
|
|
|
|
response = model_vision.generate_content(gemini_input).text |
|
return response |
|
|
|
|
|
@app.post("/get_ocr_data/") |
|
async def get_data(input_file: UploadFile = File(...),user_id:int,token:str): |
|
|
|
try: |
|
decoded_payload = jwt.decode(token, SECRET_KEY, algorithms=["HS256"]) |
|
print(f"Decoded payload: {decoded_payload}") |
|
except: |
|
return "Invalid token" |
|
|
|
|
|
file_content = await input_file.read() |
|
file_type = input_file.content_type |
|
|
|
text = "" |
|
|
|
if file_type == "application/pdf": |
|
|
|
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") |
|
|
|
|
|
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", |
|
"contact_number" : "contact number" |
|
"total_years_of_experience" : "total years of experience", |
|
"LinkedIn_link" : "LinkedIn link", |
|
"experience" : "experience", |
|
"skills" : skills |
|
] |
|
""" |
|
|
|
response = model_text.generate_content(prompt) |
|
print(response.text) |
|
data = json.loads(response.text.replace("JSON", "").replace("json", "").replace("```", "")) |
|
return {"data": data,"user_id":user_id} |
|
|
|
|
|
|
|
|