Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from PIL import Image | |
| import google.generativeai as genai | |
| # Configure Google Generative AI | |
| genai_api_key = "AIzaSyCOEqA_IZlpWCHhMOGaDJ3iJjl5cRmzKgQ" | |
| genai.configure(api_key=genai_api_key) | |
| # Initialize Gemini model | |
| def load_gemini_model(): | |
| model = genai.GenerativeModel("gemini-1.5-flash") | |
| return model | |
| # Function to extract text from the image using Gemini model | |
| def extract_text_from_image(uploaded_file, model): | |
| # Open the uploaded file as a PIL image | |
| image = Image.open(uploaded_file).convert("RGB") | |
| # Generate content using the Gemini model with the image | |
| response = model.generate_content(["Extract text from this medical report:", image]) | |
| extracted_text = response.text.strip() | |
| return extracted_text | |
| # Function to interpret the extracted text in layman's language | |
| def interpret_medical_report(extracted_text, model): | |
| # Provide interpretation in layman's terms | |
| prompt = ( | |
| f"The following is a medical report text:\n\n" | |
| f"{extracted_text}\n\n" | |
| "Please interpret this report for 7th grader and non native english speaker, " | |
| "explaining the main findings in as short as possible without any special character" | |
| ) | |
| response = model.generate_content([prompt]) | |
| interpretation = response.text.strip() | |
| return interpretation | |
| # Function to provide recommendations based on the extracted text | |
| def provide_recommendations(extracted_text, model): | |
| # Provide recommendations | |
| prompt = ( | |
| f"Based on the medical report text below:\n\n" | |
| f"{extracted_text}\n\n" | |
| "What recommendations would you give to the patient for managing their health?" | |
| "Provide brief suggestions that are easy to understand for someone without medical knowledge without any special character." | |
| ) | |
| response = model.generate_content([prompt]) | |
| recommendations = response.text.strip() | |
| return recommendations | |
| # Streamlit UI for the web app | |
| def main(): | |
| st.title("Medical Report Analyzer") | |
| st.write("Upload an image of a medical report") | |
| # Load the Gemini model | |
| model = load_gemini_model() | |
| # File uploader for medical report image | |
| uploaded_image = st.file_uploader("Upload Medical Report Image", type=["png", "jpg", "jpeg"]) | |
| if uploaded_image is not None: | |
| image = Image.open(uploaded_image).convert("RGB") | |
| st.image(image, caption="Uploaded Medical Report Image", use_container_width=True) | |
| if st.button("Analyze Report"): | |
| with st.spinner("Processing image and analyzing report..."): | |
| # Extract text from image | |
| extracted_text = extract_text_from_image(uploaded_image, model) | |
| # Interpret the extracted text | |
| st.subheader("Interpretation:") | |
| interpretation = interpret_medical_report(extracted_text, model) | |
| st.text(interpretation) | |
| # Provide health recommendations | |
| st.subheader("Recommendations:") | |
| recommendations = provide_recommendations(extracted_text, model) | |
| st.text(recommendations) | |
| if __name__ == "__main__": | |
| main() | |