File size: 4,375 Bytes
c7ebe9d ebaf733 c7ebe9d ebaf733 c7ebe9d ebaf733 c7ebe9d ebaf733 c7ebe9d ebaf733 c7ebe9d ebaf733 c7ebe9d ebaf733 c7ebe9d ebaf733 c7ebe9d ebaf733 c7ebe9d | 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 | import google.generativeai as genai
from pathlib import Path
import gradio as gr
from dotenv import load_dotenv
import os
# Load environment variables from a .env file
load_dotenv()
# Configure the GenerativeAI API key using the loaded environment variable
genai.configure(api_key=os.getenv("AIzaSyDGsO9HM1KDZ0BdZ1RGWP8lC2XR4A_Oz5w"))
# Set up the model configuration for text generation
generation_config = {
"temperature": 0.4,
"top_p": 1,
"top_k": 32,
"max_output_tokens": 4096,
}
# Define safety settings for content generation
safety_settings = [
{"category": f"HARM_CATEGORY_{category}",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"}
for category in ["HARASSMENT", "HATE_SPEECH", "SEXUALLY_EXPLICIT", "DANGEROUS_CONTENT"]
]
# Initialize the GenerativeModel with the specified model name, configuration, and safety settings
model = genai.GenerativeModel(
model_name="gemini-pro-vision",
generation_config=generation_config,
safety_settings=safety_settings,
)
# Function to read image data from a file path
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()}
# Function to generate a response based on a prompt and an image path
def generate_gemini_response(prompt, image_path):
image_data = read_image_data(image_path)
response = model.generate_content([prompt, image_data])
return response.texts
# Initial input prompt for the plant pathologist
input_prompt = """
As a highly skilled radiologist, your expertise is crucial in our pursuit of accurate medical diagnoses. You will be provided with a human bone X-ray image, and your role involves conducting a detailed analysis to identify any abnormalities, provide a diagnosis, and offer recommendations for further treatment or evaluation.
**Analysis Guidelines:**
1. **Image Examination:** Carefully examine the provided human bone X-ray image to identify any abnormalities or signs of injury or disease.
2. **Abnormal Findings:** Provide a detailed description of any abnormal findings observed in the X-ray image, including the affected bone(s), location, and characteristics of the abnormality.
3. **Diagnosis:** Based on the observed abnormalities, provide a diagnosis or a list of possible diagnoses that could explain the findings in the X-ray image.
4. **Further Evaluation:** Recommend any additional tests or evaluations that may be necessary to confirm the diagnosis or gather more information about the condition.
5. **Treatment Recommendations:** Offer informed recommendations for the treatment or management of the diagnosed condition, including any necessary medical interventions, therapies, or referrals to specialists.
6. **Important Note:** As a radiologist, your expertise is essential for accurate diagnosis and patient care. Your response should be thorough, concise, and focused on providing the best possible medical guidance.
**Disclaimer:**
*"Please note that the information provided is based on radiological analysis and should not replace professional medical advice. Consult with qualified healthcare professionals before making any medical decisions or implementing treatment plans."*
Your role is pivotal in ensuring accurate diagnoses and optimal patient care. Proceed to analyze the provided human bone X-ray image, adhering to the structured
"""
# Function to process uploaded files and generate a response
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
# Gradio interface setup
with gr.Blocks() as demo:
file_output = gr.Textbox()
image_output = gr.Image()
combined_output = [image_output, file_output]
# Upload button for user to provide images
upload_button = gr.UploadButton(
"Click to Upload an Image",
file_types=["image"],
file_count="multiple",
)
# Set up the upload button to trigger the processing function
upload_button.upload(process_uploaded_files,
upload_button, combined_output)
# Launch the Gradio interface with debug mode enabled
demo.launch(debug=True)
|