Spaces:
Runtime error
Runtime error
File size: 3,542 Bytes
ad6e901 |
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 |
import os
import tempfile
from dotenv import load_dotenv
import google.generativeai as genai
import gradio as gr
from google.generativeai.types import HarmCategory, HarmBlockThreshold
# Load environment variables
load_dotenv()
# Configure Google Gemini API
genai.configure(api_key=os.getenv('gemini_key'))
# Prompt template for detailed analysis
system_prompt = """
As a Highly Skilled medical practitioner specializing in image analysis, you are tasked with examining medical images for a renowned hospital.your expertise
is crucial in idetifying any anomalies, diseases or health issues that maybe present in the images.
Your Responsibilities:
1. Detailed Analysis: Thoroughly analyze each image, focusing on identifying any abnormal findings.
2. Finding reports: Document all observed anomalies or signs of diseases. Clearly articulate these findings in a Structured form.
3. Recommendations and Next steps: Based on your analysis, suggest potential next steps, including further tests or treatment as applicable.
4. if appropriate, recommend possible treatment options or interventions.
Important notes:
Scope of response: only respond if the image is related to human health.
Clarity of image: In cases where the image quality impedes clear analysis, note that certian aspects are 'Unable to be determind based on the provided image.'
"""
# Configuration for the generative model
generation_config = {
"temperature": 1,
"top_p": 0.95,
"top_k": 64,
"max_output_tokens": 8192,
"response_mime_type": "text/plain",
}
def upload_to_gemini(path, mime_type=None):
"""Uploads the given file to Gemini."""
file = genai.upload_file(path, mime_type=mime_type)
print(f"Uploaded file '{file.display_name}' as: {file.uri}")
return file
# Create the generative model
model = genai.GenerativeModel(
model_name="gemini-1.5-flash",
generation_config=generation_config,
safety_settings={
HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
}
)
def analyze_image(image_path):
# Ensure the uploaded file is one of the allowed types
allowed_extensions = ['.png', '.jpg', '.jpeg', '.webp']
_, ext = os.path.splitext(image_path)
if ext.lower() not in allowed_extensions:
return "Unsupported file type. Please upload a PNG, JPG, JPEG, or WEBP image."
# Upload the image to Gemini
uploaded_file_info = upload_to_gemini(image_path, mime_type="image/png")
# Create a chat session with the model and send the image for analysis
chat_session = model.start_chat(
history=[
{
"role": "user",
"parts": [
uploaded_file_info,
system_prompt,
],
},
]
)
response = chat_session.send_message(system_prompt)
return response.text
# Gradio interface
iface = gr.Interface(
fn=analyze_image,
inputs=gr.Image(type="filepath"),
outputs=gr.Textbox(label="Analysis Results"),
title="π¨ββοΈ Vital Image π· Analytics",
description="Upload a medical image to get a detailed analysis.",
)
if __name__ == "__main__":
iface.launch()
|