Spaces:
Sleeping
Sleeping
File size: 8,423 Bytes
d26b384 67371ef d26b384 3a9c63d d26b384 60d2934 d26b384 60d2934 d26b384 60d2934 d26b384 |
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 |
import google.generativeai as genai
import os
import gradio as gr
import tempfile
import json
api_key = os.getenv("GEMINI_API_KEY")
genai.configure(api_key=api_key)
def figureTableEqAnalyser(filePath):
myfile = genai.upload_file(filePath)
s="give the answer in the given JSON schema for every page having figure, table or equations accurately.\n"
prompt = '''
[
{
"pageNumber":{
"type": "integer",
"description":"Indicates if the page number of the pdf if present else -1"
},
"Figure": [
{
"figureNumber":{
"type":"integer",
"description":"Indicates the figure number if present else -1"
},
"details":{
"isPresent": {
"type": "bool",
"description": "Indicates if there is a figure present in the image."
},
"inTextDescription": {
"type": "bool",
"description": "Indicates if there is a description of the figure present in the main text other than caption."
},
"captionLocation":{
"type": "enum",
"enum": ["Above", "Below", "None"],
"description": "Indicates the location of the table caption ('above', 'below', or 'none')."
},
"inCaptionDescription": {
"type": "bool",
"description": "Indicates if the caption description is about the table."
}
},
"coordinates":{
"type": "Array",
"description": "give the coordinates for the bounding box for this Table in the order: x, y, height, width"
}
}
],
"Equation": [
{
"equationNumber":{
"type": "integer",
"description":"Indicates if there is a number of the equation if present else -1"
},
"details":{
"isPresent": {
"type": "bool",
"description": "Indicates if there is an equation present in the image."
},
"inTextDescription": {
"type": "bool",
"description": "Indicates if there is a description of the equation present in the main text other than caption."
}
},
"coordinates":{
"type": "Array",
"description": "give the coordinates for the bounding box for this Table in the order: x, y, height, width"
}
}
],
"Table": [
{
"tableNumber":{
"type":"integer",
"description":"Indicates if there is a number of the equation if present else -1"
},
"details":{
"isPresent": {
"type": "bool",
"description": "Indicates if there is a table present in the image."
},
"inTextDescription": {
"type": "bool",
"description": "Indicates if there is a description of the table present in the main text other than caption."
},
"captionLocation": {
"type": "enum",
"enum": ["Above", "Below", "None"],
"description": "Indicates the location of the table caption ('above', 'below', or 'none')."
},
"inCaptionDescription": {
"type": "bool",
"description": "Indicates if the caption description is about the table."
}
},
"coordinates":{
"type": "Array",
"description": "give the coordinates for the bounding box for this Table in the order: x, y, height, width"
}
}
]
}
]
'''
model = genai.GenerativeModel(model_name="gemini-1.5-pro-exp-0827")
response = model.generate_content([s+prompt, myfile])
resp = response.text
n=len(resp)
resp=resp[8:n-3]
ans = json.loads(resp)
return ans
# ------------------------------
# Gradio Interface
# ------------------------------
def process_upload(file):
"""
Process the uploaded PDF file and return analysis results and annotated PDF.
"""
# print(file.name)
if file is None:
return json.dumps({"error": "No file uploaded"}, indent=2), None
# # Create a temporary file to work with
print("Here in Process upload")
# with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as temp_input:
# temp_input.write(file)
# temp_input_path = temp_input.name
# print(temp_input_path)
print("test 1")
temp_input = tempfile.NamedTemporaryFile(delete=True, suffix='.pdf')
temp_input.write(file)
temp_input_path = temp_input.name
print(temp_input_path)
# Analyze the PDF
results = figureTableEqAnalyser(temp_input_path)
print(results)
results_json = json.dumps(results, indent=2)
# Clean up the temporary input file
os.unlink(temp_input_path)
return results_json
def create_interface():
with gr.Blocks(title="PDF Analyzer") as interface:
gr.Markdown("# PDF Analyzer")
gr.Markdown("Upload a PDF document to analyze its structure, references, language, and more.")
with gr.Row():
file_input = gr.File(
label="Upload PDF",
file_types=[".pdf"],
type="binary"
)
with gr.Row():
analyze_btn = gr.Button("Analyze PDF")
with gr.Row():
results_output = gr.JSON(
label="Analysis Results",
show_label=True
)
analyze_btn.click(
fn=process_upload,
inputs=[file_input],
outputs=[results_output]
)
return interface
if __name__ == "__main__":
interface = create_interface()
interface.launch(
share=True,
server_port=None
) |