|
import google.generativeai as genai |
|
from pathlib import Path |
|
import gradio as gr |
|
from dotenv import load_dotenv |
|
import os |
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
genai.configure(api_key=os.getenv("AIzaSyDGsO9HM1KDZ0BdZ1RGWP8lC2XR4A_Oz5w")) |
|
|
|
|
|
generation_config = { |
|
"temperature": 0.4, |
|
"top_p": 1, |
|
"top_k": 32, |
|
"max_output_tokens": 4096, |
|
} |
|
|
|
|
|
safety_settings = [ |
|
{"category": f"HARM_CATEGORY_{category}", |
|
"threshold": "BLOCK_MEDIUM_AND_ABOVE"} |
|
for category in ["HARASSMENT", "HATE_SPEECH", "SEXUALLY_EXPLICIT", "DANGEROUS_CONTENT"] |
|
] |
|
|
|
|
|
model = genai.GenerativeModel( |
|
model_name="gemini-pro-vision", |
|
generation_config=generation_config, |
|
safety_settings=safety_settings, |
|
) |
|
|
|
|
|
|
|
def read_image_data(file_path): |
|
image_path = Path(file_path) |
|
if not image_path.exists(): |
|
raise FileNotFoundError(f"Could not find image: {image_path}") |
|
return {"mime_type": "image/jpeg", "data": image_path.read_bytes()} |
|
|
|
|
|
|
|
|
|
def generate_gemini_response(prompt, image_path): |
|
image_data = read_image_data(image_path) |
|
response = model.generate_content([prompt, image_data]) |
|
return response.text |
|
|
|
|
|
|
|
input_prompt = """ |
|
You are an expertised doctor. The user will upload an image of the injury. Analyse the image and provide a complete analysis to assess for insurance claim estimation. |
|
|
|
Additionally, provide any relevant details such as: |
|
1. Type and severity of the injury (e.g., broken bone, laceration, burn). |
|
2. Location of the injury on the body. |
|
3. Total cost that the user can claim from the insurance company. |
|
|
|
Based on the provided image and information, the model will analyze the injury and estimate the amount that can be claimed from the insurance." |
|
**Disclaimer:** |
|
*"Please note that the information provided is based on the image analysis and should not replace professionals."* |
|
""" |
|
|
|
|
|
|
|
def process_uploaded_files(files): |
|
file_path = files[0].name if files else None |
|
response = generate_gemini_response( |
|
input_prompt, file_path) if file_path else None |
|
return file_path, response |
|
|
|
|
|
with gr.Blocks() as demo: |
|
file_output = gr.Textbox() |
|
image_output = gr.Image() |
|
combined_output = [image_output, file_output] |
|
|
|
|
|
upload_button = gr.UploadButton( |
|
"Click to Upload an Image", |
|
file_types=["image"], |
|
file_count="multiple", |
|
) |
|
|
|
upload_button.upload(process_uploaded_files, |
|
upload_button, combined_output) |
|
|
|
|
|
demo.launch(debug=True) |
|
|