import gradio as gr
import os
import json
import shutil
from vgg_yolo_sam import car_damage_check, damaged_part_detection, severity_detection, car_check_function
css = """
@font-face {
font-family: 'Nokia Pure Headline';
src: url('https://xt-brillio-public.s3.amazonaws.com/nokia-LLM-fonts/NokiaPureHeadline-Bold.woff2') format('woff2'),
url('https://xt-brillio-public.s3.amazonaws.com/nokia-LLM-fonts/NokiaPureHeadline-Bold.woff') format('woff');
font-weight: bold;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: 'Nokia Pure Headline';
src: url('https://xt-brillio-public.s3.amazonaws.com/nokia-LLM-fonts/NokiaPureHeadline-Light.woff2') format('woff2'),
url('https://xt-brillio-public.s3.amazonaws.com/nokia-LLM-fonts/NokiaPureHeadline-Light.woff') format('woff');
font-weight: 300;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: 'Nokia Pure Headline';
src: url('https://xt-brillio-public.s3.amazonaws.com/nokia-LLM-fonts/NokiaPureHeadline-Regular.woff2') format('woff2'),
url('https://xt-brillio-public.s3.amazonaws.com/nokia-LLM-fonts/NokiaPureHeadline-Regular.woff') format('woff');
font-weight: normal;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: 'Nokia Pure Headline Extra';
src: url('https://xt-brillio-public.s3.amazonaws.com/nokia-LLM-fonts/NokiaPureHeadline-ExtraBold.woff2') format('woff2'),
url('https://xt-brillio-public.s3.amazonaws.com/nokia-LLM-fonts/NokiaPureHeadline-ExtraBold.woff') format('woff');
font-weight: bold;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: 'Nokia Pure Headline Ultra';
src: url('https://xt-brillio-public.s3.amazonaws.com/nokia-LLM-fonts/NokiaPureHeadline-UltraLight.woff2') format('woff2'),
url('https://xt-brillio-public.s3.amazonaws.com/nokia-LLM-fonts/NokiaPureHeadline-UltraLight.woff') format('woff');
font-weight: 200;
font-style: normal;
font-display: swap;
}
.gradio-container {margin: 0 !important; padding: 0 !important; max-width: 100% !important; background-color: #F8F9FD !important;font-family: Nokia Pure Headline !important;}
.header-nav { position: relative; display: flex; align-items: center; justify-content: center; font-weight: 800 !important; font-size: 26px !important; padding: 5px; background-color: #fff !important;}
#feature-container { padding: 50px 150px;}
.feature-header * {color: #878787 !important; font-size: 12px; }
.form, .pe-options, input[type=text],input[type=password], textarea { padding: 2px !important; border-radius: 0 !important; border : 0 !important; background: none !important; box-shadow: none !important;}
.form > div.block { border-radius: 8px !important;}
.pe-options { padding: 0 !important;}
.pe-options > span, .feature-field-label, #io-row .form label > span { color : #333; font-size: 14px; font-weight: bold;}
.pe-options input[type=radio], { border: 1px solid #d1d5db !important;}
.pe-options input[type=radio]:checked { background-color: #005AFF !important;}
#io-row .form { gap: 20px; padding: 5px !important;}
#io-row .form > div { box-shadow: rgb(0 0 0 / 16%) 0px 1px 2px 0px !important }
#prompt_choice { margin-top: 20px !important; }
#box-shadow, #form-row .form { box-shadow: rgb(0 0 0 / 16%) 0px 1px 4px 0px !important }
button.secondary {background: #E4F0FF !important;box-shadow: none !important;border: 0 !important; font-weight: normal !important; color: #005AFF !important; font-size: 14px !important; text-transform: capitalize !important; }
#option-dep { margin-top: 35px; }
#option-dep > label > span { position: absolute; top: -30px; left: 0;color: #333; font-size: 14px; font-weight: bold;}
#form-row .form { display: flex; flex-direction: column; background-color: #fff !important; border-radius: 8px !important; margin-top: 20px !important; }
input[type="range"] {
width: 100%;
cursor: pointer;
outline: none;
accent-color: #0168FF !important;
border-radius: 8px !important;
border: none !important;
}
#form-row .form label > span { font-wight: bold !important; color: #333 !important; font-size: 14px !important; }
input[type=number] {
border-radius: 100px !important;
background: #878787 !important;
color: #fff !important;
text-align: center !important;
justify-content: center !important;
}
"""
# header = "
"
header = ""
import gradio as gr
import time
import openai
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer import DocumentAnalysisClient
from PIL import Image
from io import BytesIO
openai.api_key = "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"
endpoint = "https://insuranceformrecognizer.cognitiveservices.azure.com/"
key = "4eca35b579a340abb13a9fba6da21bc2"
def image_buton_fn(image_btn,damage_btn):
# images = ['images/MicrosoftTeams-image (2).png','images/MicrosoftTeams-image (3).png' ]
print(image_btn)
images = [file.name for file in image_btn]
print(images)
if image_btn is not None:
car_damage_check_result = []
damaged_part_check_result = []
severity_check_result = []
image_bbox_result = []
for img_path in images:
car_damage_check_result.append(car_damage_check(img_path))
damaged_part_check_result.append(damaged_part_detection(img_path))
severity_predictions,output_image_path = severity_detection(img_path)
severity_check_result.append(severity_predictions)
image_bbox_result.append(output_image_path)
# print(car_damage_check_result)
car_damage_check_result_final = list(map(car_check_function, car_damage_check_result))
print(car_damage_check_result_final)
print(damaged_part_check_result)
# print(severity_check_result)
severity_check_result_final = list(map(lambda x: [max(x)],severity_check_result))
print(severity_check_result_final)
print(image_bbox_result)
car_damage_check_result_final_flattended = list(set([item for sublist in car_damage_check_result_final for item in sublist]))
damaged_part_check_result_flattended = list(set([item for sublist in damaged_part_check_result for item in sublist]))
severity_check_result_final_flattended = list(set([item for sublist in severity_check_result_final for item in sublist]))
image_bbox_result_flattended = list(set([item for sublist in image_bbox_result for item in sublist]))
print(car_damage_check_result_final_flattended,damaged_part_check_result_flattended,severity_check_result_final_flattended,image_bbox_result_flattended)
details = "Damaged or not :" + str(car_damage_check_result_final_flattended) + '\n' + "Damaged Part :" + str(damaged_part_check_result_flattended) + '\n'
details = details + '\n'+"Severity is :" + str(severity_check_result_final_flattended)
return {other_details:gr.update(value=details),accident_images_processed:gr.update(value=image_bbox_result_flattended)}
# flattened_list = [item for sublist in list_of_lists for item in sublist]
def extract_policy_info(fill_btn):
document_analysis_client = DocumentAnalysisClient(
endpoint=endpoint, credential=AzureKeyCredential(key)
)
with open(fill_btn.name, "rb") as f:#(r"C:\Users\Lavish.Bansal\OneDrive - Brillio\Documents\Project\adhoc\Insurance OCR\data\Insurance1\Insurance1_Page2.jpg", "rb") as f:
poller = document_analysis_client.begin_analyze_document(
"prebuilt-document", document=f
)
result = poller.result()
text = result.content
json_1 = str({
"Policy Holder Name":"Name of the insurer/policyholder"
,"Policy Number":"Policy serial number"
,"Policy Start Date":"start date of the policy"
,"Policy End Date":"End date of the policy"
,"Contact No":" Contact number of the insurer"
,"Email Address":"Email address of the insurer"
,"Vehicle Registration No":"Registartion no of the vehicle"
,"Vehicle Make":"Make of the vehicle"
,"Vehicle Model":"Model of the vehicle"
,"Manufacturing Year":"Year of Manufacturing of the vehicle"
,"Vehicle Engine No":"Engine no of the vehicle"
,"Vehicle Chassis No":"Chassis no of the vechicle"
})
prompt = """Provide te details required in the {0} as per the description from the given text {1}.
You have to output a JSON which has the following format from {2}'key-value'.ky has to be enclised in doube quotes.
Do not hallucinate any information. If information is not available in the blob, say "Information is not available". """.format(json_1,text,json_1)
response = openai.Completion.create(
engine = "text-davinci-003",
prompt = prompt,
temperature = 0.6,
max_tokens = 250,
)
output_result = response.choices[0].text
output_result = json.loads(output_result)
return {full_name:gr.update(value=output_result['Policy Holder Name']),policy_number:gr.update(value=output_result['Policy Number']),
start_date:gr.update(value=output_result['Policy Start Date']),end_date:gr.update(value=output_result['Policy End Date']),
contact_no:gr.update(value=output_result['Contact No']),email_add:gr.update(value=output_result['Email Address']),
veh_num:gr.update(value=output_result['Vehicle Registration No']),veh_make:gr.update(value=output_result['Vehicle Make']),
veh_model:gr.update(value=output_result['Vehicle Model']),veh_manufacturing_year:gr.update(value=output_result['Manufacturing Year']),
veh_engine_no:gr.update(value=output_result['Vehicle Engine No']),veh_chassis_no:gr.update(value=output_result['Vehicle Chassis No'])}
# text = extract_policy_info()
def policy_image_fn(policy_btn):
"""Shows images from a folder."""
if policy_btn is not None:
# Process the uploaded policy document
return {policy_image:gr.update(value=policy_btn.name)}
def accident_image_upload_fn(image_btn):
print(image_btn)
file_paths = [file.name for file in image_btn]
print(file_paths)
if image_btn is not None:
# Process the uploaded policy document
return {accident_images:gr.update(value=file_paths)}
def submit_fn():
time.sleep(1.5)
gr.Info("Your request has been processed")
with gr.Blocks(css = css) as demo: #css=".gradio-container {background-color: lightgray}" css=css
gr.HTML(header)
with gr.Column(elem_id = "feature-container") :
with gr.Row(elem_id= "io-row"):
with gr.Column():
# gr.HTML("""Select the first model
""")
policy_btn = gr.UploadButton("📁 Click to Upload the policy document ", file_types=["image","file"])
image_btn = gr.UploadButton("📁 Click to Upload the vehicle images ", file_types=["image","file"],file_count='multiple')
# model_ft_classi = gr.Dropdown(
# ["openai-zeroshot"],interactive=True, show_label=False, visible =True
# )
with gr.Column():
# gr.HTML("""Select the second model
""")
fill_btn = gr.Button(value="Fill the claim form")
damage_btn = gr.Button(value="Check damages")
with gr.Accordion("Policy document", open=False):
policy_image = gr.Image()
policy_btn.upload(policy_image_fn,policy_btn,policy_image)
with gr.Accordion("Accident Images", open=False):
accident_images = gr.Gallery()
image_btn.upload(accident_image_upload_fn,image_btn,accident_images)
with gr.Row(elem_id= "io-row"):
with gr.Column():
full_name= gr.Textbox(interactive=True, label="Policy Holder Name",visible =True)
policy_number= gr.Textbox(interactive=True, label="Policy Number",visible =True)
start_date= gr.Textbox(interactive=True, label="Insurance Start Date",visible =True)
end_date= gr.Textbox(interactive=True, label="Insurance End Date",visible =True)
contact_no= gr.Textbox(interactive=True, label="Contact No",visible =True)
email_add= gr.Textbox(interactive=True, label="Email Address",visible =True)
with gr.Column():
veh_num = gr.Textbox(interactive=True, label="Vehicle Registration No",visible =True)
veh_make = gr.Textbox(interactive=True, label="Vehicle Make",visible =True)
veh_model = gr.Textbox(interactive=True, label="Vehicle Model",visible =True)
veh_manufacturing_year = gr.Textbox(interactive=True, label="Vehicle Manufacturing Year",visible =True)
veh_engine_no = gr.Textbox(interactive=True, label="Vehicle Engine No",visible =True)
veh_chassis_no = gr.Textbox(interactive=True, label="Vehicle Chassis No",visible =True)
other_details = gr.TextArea(interactive=True, label="Other Details",visible =True)
images = ['images/MicrosoftTeams-image (2).png','images/MicrosoftTeams-image (3).png' ]
with gr.Accordion("Accident Images Processed", open=False):
accident_images_processed = gr.Gallery()
damage_btn.click(image_buton_fn,[image_btn,damage_btn],[other_details,accident_images_processed])
fill_btn.click(extract_policy_info,policy_btn,[full_name,policy_number,start_date,end_date,contact_no,email_add,veh_num,veh_make,veh_model,veh_manufacturing_year,veh_engine_no,veh_chassis_no])
submit_btn = gr.Button(value="Submit")
submit_btn.click(submit_fn,None,None)
demo.launch(debug=True)