Update main.py
Browse files
main.py
CHANGED
@@ -8,10 +8,12 @@ import os
|
|
8 |
from fastapi import FastAPI, HTTPException, File, UploadFile
|
9 |
from fastapi.middleware.cors import CORSMiddleware
|
10 |
from PyPDF2 import PdfReader
|
11 |
-
|
12 |
-
|
13 |
import google.generativeai as genai
|
14 |
import json
|
|
|
|
|
|
|
|
|
15 |
|
16 |
secret = os.environ["key"]
|
17 |
genai.configure(api_key=secret)
|
@@ -28,30 +30,95 @@ app.add_middleware(
|
|
28 |
allow_headers=["*"],
|
29 |
)
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
@app.post("/get_ocr_data/")
|
32 |
-
async def get_data(
|
33 |
try:
|
34 |
-
#
|
35 |
-
|
|
|
|
|
36 |
text = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
text += page.extract_text()
|
41 |
|
42 |
-
#
|
43 |
-
prompt = f"""
|
44 |
-
|
45 |
|
46 |
-
|
47 |
|
48 |
-
in
|
49 |
|
50 |
response = model_text.generate_content(prompt)
|
51 |
-
data = json.loads(response.text.replace("```json","").replace("```",""))
|
52 |
-
return {"data":data}
|
53 |
|
54 |
except Exception as e:
|
55 |
-
raise HTTPException(status_code=500, detail=f"Error processing
|
56 |
-
|
57 |
-
|
|
|
8 |
from fastapi import FastAPI, HTTPException, File, UploadFile
|
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 requests
|
17 |
|
18 |
secret = os.environ["key"]
|
19 |
genai.configure(api_key=secret)
|
|
|
30 |
allow_headers=["*"],
|
31 |
)
|
32 |
|
33 |
+
|
34 |
+
|
35 |
+
def encode_image(image):
|
36 |
+
# Convert image to BytesIO object (in memory)
|
37 |
+
buffered = BytesIO()
|
38 |
+
image.save(buffered, format=image.format) # Use the original image format (e.g., PNG, JPEG)
|
39 |
+
img_bytes = buffered.getvalue()
|
40 |
+
|
41 |
+
# Encode image to base64
|
42 |
+
base64_image = base64.b64encode(img_bytes).decode('utf-8')
|
43 |
+
return base64_image
|
44 |
+
|
45 |
+
|
46 |
+
|
47 |
+
def vision(image):
|
48 |
+
# OpenAI API Key
|
49 |
+
api_key = "sk-proj-1j1aFDCU8KrWAeFMAGPPT3BlbkFJ6rDxGgu8C99E3Wh6siUs"
|
50 |
+
|
51 |
+
|
52 |
+
# Getting the base64 string
|
53 |
+
base64_image = encode_image(image)
|
54 |
+
|
55 |
+
headers = {
|
56 |
+
"Content-Type": "application/json",
|
57 |
+
"Authorization": f"Bearer {api_key}"
|
58 |
+
}
|
59 |
+
|
60 |
+
payload = {
|
61 |
+
"model": "gpt-4o-mini",
|
62 |
+
"messages": [
|
63 |
+
{
|
64 |
+
"role": "user",
|
65 |
+
"content": [
|
66 |
+
{
|
67 |
+
"type": "text",
|
68 |
+
"text": "extract all data from this image"
|
69 |
+
},
|
70 |
+
{
|
71 |
+
"type": "image_url",
|
72 |
+
"image_url": {
|
73 |
+
"url": f"data:image/jpeg;base64,{base64_image}"
|
74 |
+
}
|
75 |
+
}
|
76 |
+
]
|
77 |
+
}
|
78 |
+
],
|
79 |
+
"max_tokens": 300
|
80 |
+
}
|
81 |
+
|
82 |
+
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
|
83 |
+
|
84 |
+
print(response.json()['choices'][0]['message']['content'])
|
85 |
+
|
86 |
+
|
87 |
@app.post("/get_ocr_data/")
|
88 |
+
async def get_data(input_file: UploadFile = File(...)):
|
89 |
try:
|
90 |
+
# Determine the file type by reading the first few bytes
|
91 |
+
file_content = await input_file.read()
|
92 |
+
file_type = input_file.content_type
|
93 |
+
|
94 |
text = ""
|
95 |
+
|
96 |
+
if file_type == "application/pdf":
|
97 |
+
# Read PDF file using PyPDF2
|
98 |
+
pdf_reader = PdfReader(io.BytesIO(file_content))
|
99 |
+
for page in pdf_reader.pages:
|
100 |
+
text += page.extract_text()
|
101 |
+
|
102 |
+
elif file_type in ["image/jpeg", "image/png", "image/jpg"]:
|
103 |
+
# Read Image file using PIL and pytesseract
|
104 |
+
image = Image.open(io.BytesIO(file_content))
|
105 |
+
return encode_image(image)
|
106 |
+
text = vision(image)
|
107 |
|
108 |
+
else:
|
109 |
+
raise HTTPException(status_code=400, detail="Unsupported file type")
|
|
|
110 |
|
111 |
+
# Call Gemini (or another model) to extract required data
|
112 |
+
prompt = f"""This is CV data: {text.strip()}
|
113 |
+
I want only:
|
114 |
|
115 |
+
firstname, lastname, contact number, total years of experience, LinkedIn link, experience, skills
|
116 |
|
117 |
+
in JSON format only"""
|
118 |
|
119 |
response = model_text.generate_content(prompt)
|
120 |
+
data = json.loads(response.text.replace("```json", "").replace("```", ""))
|
121 |
+
return {"data": data}
|
122 |
|
123 |
except Exception as e:
|
124 |
+
raise HTTPException(status_code=500, detail=f"Error processing file: {str(e)}")
|
|
|
|