File size: 4,189 Bytes
e577b7d
7856bc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fabd2dd
 
 
 
 
 
 
 
 
 
7856bc3
 
 
 
 
 
 
 
 
 
 
fabd2dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7856bc3
 
 
 
 
 
 
fabd2dd
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
import streamlit as st
from PIL import Image
import tempfile
from preprocessing.process import modify_image
from model.loader import ImageProcessor
from text_classifier.classifier import extract_medical_info, extract_and_save_medical_info
from search.extractor import get_medication_details
import os

# βœ… Must be the first Streamlit command
st.set_page_config(page_title="Medical Image Analyzer", page_icon="πŸ”", layout="wide")

# Title
st.title("Medical Image Text Analyzer")

# Image upload widget
uploaded_image = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg", "gif", "webp"])

# If an image is uploaded
if uploaded_image:
    # Show the uploaded image
    image = Image.open(uploaded_image)
    st.image(image, caption="Uploaded Image", use_column_width=True)

    # Process the image when the button is clicked
    if st.button("Analyze Image"):
        try:
            # Save the image temporarily to disk
            with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
                image.save(temp_file.name)

            # Image processing (preprocessing steps: grayscale, sharpen, enhance contrast)
            processed_image = modify_image(temp_file.name)

            if processed_image is not None:
                st.image(processed_image, caption="Processed Image", use_column_width=True)

                # Use the loader to analyze the image and get the markdown content
                processor = ImageProcessor()
                markdown_result = processor.analyze_image(image_path=temp_file.name)

                # Extract the text data from the markdown result
                medical_info = extract_medical_info(markdown_result)

                # Optional: Save to file
                extract_and_save_medical_info(markdown_result)

                # Display the extracted medical information
                st.subheader("Extracted Medical Information")
                if isinstance(medical_info, dict):
                    st.json(medical_info)

                    # Get medication details (with fuzzy matching)
                    medications = medical_info.get("medications", [])
                    medication_details_df = get_medication_details(medications)


                # Display the extracted medical information
                st.subheader("Extracted Medical Information")
                if isinstance(medical_info, dict):
                    st.json(medical_info)

                    # Get medication details (with fuzzy matching)
                    medications = medical_info.get("medications", [])
                    medication_details_df = get_medication_details(medications)

                    # Display the medication details as a table
                    st.subheader("Medication Details")

                    # Loop over each medication and render it in a styled box
                    for _, row in medication_details_df.iterrows():
                        st.markdown(
                            f"""
                            <div style="color:black; border: 1px solid #ccc; border-radius: 10px; padding: 15px; margin-bottom: 20px; background-color: #f9f9f9;">
                                <h4 style="color: #2c3e50;">🩺 {row['Name']}</h4>
                                <p style="color: #2c3e50;"><strong>Contains:</strong> {row['Contains']}</p>
                                <p style="color: #2c3e50;"><strong>How to Use:</strong> {row['How to Use']}</p>
                                <p style="color: #2c3e50;"><strong>Advice:</strong><br>{row['Advice']}</p>
                                <p style="color: #2c3e50;"><a href="{row['Link']}" target="_blank">πŸ”— More Info</a></p>
                            </div>
                            """,
                            unsafe_allow_html=True
                        )
                        
                else:
                    st.error("Failed to extract medical information.")
                # βœ… Cleanup
                if os.path.exists(temp_file.name):
                    os.remove(temp_file.name)
        except Exception as e:
            st.error(f"Error analyzing image: {str(e)}")