File size: 4,824 Bytes
aa4ce78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
104
105
106
107
import streamlit as st
import os
from PIL import Image
import google.generativeai as genai
from dotenv import load_dotenv
from mysrap import search_medicine_supertails
from google.generativeai.types import HarmCategory, HarmBlockThreshold

def presc_analyze():
    # Load environment variables
    load_dotenv()

    # Configure the API key
    genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))

    # Function to load Gemini Vision Pro model and get response
    def get_gemini_response(input_prompt, image_data, user_prompt):
        # model = genai.GenerativeModel("gemini-pro-vision")
        safety_settings={
        HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
        HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
        # HarmCategory.HARM_CATEGORY_DANGEROUS: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
        HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
    }
        model = genai.GenerativeModel("gemini-pro-vision",safety_settings=safety_settings)
        response = model.generate_content([input_prompt, image_data[0], user_prompt])
        return response.text

    # Function to extract data from uploaded image
    def input_image_setup(uploaded_file):
        if uploaded_file is not None:
            bytes_data = uploaded_file.getvalue()
            image_parts = [
                {
                    "mime_type": uploaded_file.type,
                    "data": bytes_data,
                }
            ]
            return image_parts
        else:
            raise FileNotFoundError("No file uploaded")

    def extract_medicine_names_heuristic(text):
        import re
        # Split based on common delimiters, numbers, or new lines
        potential_medicines = re.split(r'\d+\.\s*|\n|\r|,', text)
        # Clean up and filter out non-medicines
        potential_medicines = [med.strip() for med in potential_medicines if med.strip() and med.lower() not in ["the", "to", "of", "and", "is"]]
        return potential_medicines

    # Streamlit app configuration
    # st.set_page_config(page_title="Prescription & Medicine Information Extractor")
    st.header("Prescription & Medicine Information Extractor")

    user_prompt = "Give me the details about the Prescription and along with basic personal details and tell about medications and frequency in a proper tabular form"
    uploaded_file = st.file_uploader("Choose a Prescription Image...", type=["jpg", "jpeg", "png"])
    image = ""
    if uploaded_file is not None:
        image = Image.open(uploaded_file)
        st.image(image, caption="Uploaded Image", use_column_width=True)

    submit = st.button("Extract Information")

    # System prompt for understanding prescriptions
    info_prompt = """You are an expert in understanding prescriptions. 

    You will receive prescription images and answer questions based on them. 

    Please consider the following information: {user_prompt}"""

    # System prompt for extracting medicine names (heuristic)
    medicine_prompt = "Please list all the medications mentioned in the prescription.Make sure that you just give medicine name with strength."

    if submit:
        image_data = input_image_setup(uploaded_file)

        # Extract information based on user prompt
        info_response = get_gemini_response(info_prompt.format(user_prompt=user_prompt), image_data, "")
        st.subheader("Extracted Information:")
        st.write(info_response)

        # Extract medicine names using a separate query
        medicine_response = get_gemini_response(medicine_prompt, image_data, "")
        medicine_names = extract_medicine_names_heuristic(medicine_response)

        if medicine_names:
            st.subheader("Medicine Names:")
            # Store medicine names in a list
            medicine_list = ", ".join(medicine_names)
            st.write(medicine_list)

            # Display at least 6-7 medicines with clickable links for each extracted medicine name
            st.subheader("Medicines with Links:")
            for medicine in medicine_names:
                st.markdown(f"### {medicine}")
                results_df = search_medicine_supertails(medicine)
                if results_df is not None and not results_df.empty:
                    for index, row in results_df.head(7).iterrows():
                        st.markdown(f"[{row['Product Name']}]({row['Link']}) - {row['Price']} ₹")
                else:
                    st.write(f"No results found for {medicine}")
        else:
            st.write("No medicine names found using the heuristic approach.")

        st.balloons()

if __name__ == '__main__':
    presc_analyze()