Reaper / app.py
Alphar3d's picture
Update app.py
020844f verified
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("AIzaSyCsoOchC3kjG_N_A03VFN0IQ1pGuXC18Hw"))
# 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.text
# Initial input prompt for the plant pathologist
input_prompt = """
As a highly skilled human orthopedic specialist, your expertise is indispensable in finding what the X-ray image says. You will be provided with information or X-ray samples, and your role involves conducting a detailed analysis to identify the specific issues, propose solutions, and offer recommendations.
**Analysis Guidelines:**
1. **Disease Identification:** Examine the provided information or samples to identify and characterize.
2. **Detailed Findings:** Provide in-depth findings on the nature, including affected parts, and potential causes.
3. **Next Steps:** Outline the recommended course of action for managing and controlling the identified defect. This may involve treatment options, preventive measures, or further investigations.
4. **Recommendations:** Offer informed recommendations for maintaining bone health, preventing future bone problems, and optimizing overall bone well-being.
**Disclaimer:**
*"Please note that the information provided is based on X- ray image and data analysis and should not replace professional doctor advice. Consult with qualified experts before implementing any strategies or treatments."*
Your role is pivotal in ensuring the health and productivity of humans. Proceed to analyze the provided information or samples, 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)