Nasma commited on
Commit
6c1dd30
·
verified ·
1 Parent(s): 90c9398

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +137 -0
main.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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(
39
+ CORSMiddleware,
40
+ allow_origins=["*"],
41
+ allow_credentials=True,
42
+ allow_methods=["*"],
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
+ async def get_data(input_file: UploadFile = File(...)):
81
+ #try:
82
+ # Determine the file type by reading the first few bytes
83
+ file_content = await input_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",
111
+ "contact_number" : "contact number",
112
+ "home_address" : "full home address",
113
+ "home_town" : "home town or city",
114
+ "total_years_of_experience" : "total years of experience",
115
+ "education": "Institution Name, Degree Name",
116
+ "LinkedIn_link" : "LinkedIn link",
117
+ "experience" : "experience",
118
+ "industry": "industry of work",
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)}")