Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,8 +6,10 @@ import os
|
|
6 |
|
7 |
from langchain_community.document_loaders import UnstructuredImageLoader
|
8 |
from langchain.prompts import PromptTemplate
|
9 |
-
from
|
|
|
10 |
|
|
|
11 |
os.environ["HUGGINGFACEHUB_API_KEY"] = os.getenv("HF")
|
12 |
os.environ["HF_TOKEN"] = os.getenv("HF")
|
13 |
|
@@ -17,7 +19,6 @@ st.set_page_config(
|
|
17 |
page_icon="💊"
|
18 |
)
|
19 |
|
20 |
-
# Sidebar
|
21 |
st.sidebar.title("💊 MediAssist")
|
22 |
st.sidebar.markdown("Analyze prescriptions with ease using AI")
|
23 |
st.sidebar.markdown("---")
|
@@ -33,10 +34,8 @@ st.sidebar.markdown("""
|
|
33 |
</a>
|
34 |
</div>
|
35 |
""", unsafe_allow_html=True)
|
36 |
-
|
37 |
st.sidebar.markdown("---")
|
38 |
|
39 |
-
# Main Title
|
40 |
st.markdown("""
|
41 |
<h1 style='text-align: center; color: #4A90E2;'>🧠 MediAssist</h1>
|
42 |
<h3 style='text-align: center;'>Prescription Analyzer using AI and OCR</h3>
|
@@ -44,40 +43,48 @@ st.markdown("""
|
|
44 |
<br>
|
45 |
""", unsafe_allow_html=True)
|
46 |
|
47 |
-
# Image Upload
|
48 |
uploaded_file = st.file_uploader("📤 Upload Prescription Image (JPG/PNG)", type=["jpg", "jpeg", "png"])
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
53 |
|
54 |
-
#
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
58 |
|
59 |
-
#
|
60 |
-
|
61 |
-
|
62 |
|
63 |
-
|
|
|
|
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
prompt = PromptTemplate(input_variables=["prescription_text"], template=template)
|
68 |
|
69 |
-
|
70 |
-
repo_id="mistralai/Mistral-7B-Instruct-v0.3",
|
71 |
-
provider="novita",
|
72 |
-
temperature=0.6,
|
73 |
-
max_new_tokens=300,
|
74 |
-
task="conversational"
|
75 |
-
)
|
76 |
|
77 |
-
|
|
|
|
|
78 |
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
with col1:
|
83 |
st.image(dilated, caption="Preprocessed Prescription", channels="GRAY", use_container_width=True)
|
@@ -87,13 +94,21 @@ col1, col2 = st.columns([1, 2])
|
|
87 |
|
88 |
st.markdown("### 📜 Extracted Text")
|
89 |
st.code(extracted_text)
|
90 |
-
|
91 |
if st.button("🔍 Analyze Text"):
|
92 |
with st.spinner("Analyzing..."):
|
93 |
response = chain.run(prescription_text=extracted_text)
|
94 |
-
# st.markdown("### LLM Output")
|
95 |
st.success(response)
|
96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
# st.markdown("### 🌐 Translated Text")
|
99 |
# st.code("पेरासिटामोल 500 मिलीग्राम\nभोजन के बाद दिन में दो बार 1 गोली लें", language='text')
|
@@ -104,10 +119,6 @@ col1, col2 = st.columns([1, 2])
|
|
104 |
# st.markdown("### ⚠️ Possible Side Effects")
|
105 |
# st.warning("- Nausea\n- Dizziness\n- Liver damage (on overdose)")
|
106 |
|
107 |
-
os.remove(temp_path)
|
108 |
-
os.remove(orig_path)
|
109 |
-
os.remove(dilated_path)
|
110 |
-
|
111 |
-
else:
|
112 |
-
st.image("https://cdn.dribbble.com/users/285475/screenshots/14711920/media/bd46dc2873f7099e4ef9fd53e6f7f1df.png", width=600)
|
113 |
-
st.markdown("<center><i>Upload a prescription image to begin analysis.</i></center>", unsafe_allow_html=True)
|
|
|
6 |
|
7 |
from langchain_community.document_loaders import UnstructuredImageLoader
|
8 |
from langchain.prompts import PromptTemplate
|
9 |
+
from langchain.chains import LLMChain
|
10 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
11 |
|
12 |
+
# Set Hugging Face API keys
|
13 |
os.environ["HUGGINGFACEHUB_API_KEY"] = os.getenv("HF")
|
14 |
os.environ["HF_TOKEN"] = os.getenv("HF")
|
15 |
|
|
|
19 |
page_icon="💊"
|
20 |
)
|
21 |
|
|
|
22 |
st.sidebar.title("💊 MediAssist")
|
23 |
st.sidebar.markdown("Analyze prescriptions with ease using AI")
|
24 |
st.sidebar.markdown("---")
|
|
|
34 |
</a>
|
35 |
</div>
|
36 |
""", unsafe_allow_html=True)
|
|
|
37 |
st.sidebar.markdown("---")
|
38 |
|
|
|
39 |
st.markdown("""
|
40 |
<h1 style='text-align: center; color: #4A90E2;'>🧠 MediAssist</h1>
|
41 |
<h3 style='text-align: center;'>Prescription Analyzer using AI and OCR</h3>
|
|
|
43 |
<br>
|
44 |
""", unsafe_allow_html=True)
|
45 |
|
|
|
46 |
uploaded_file = st.file_uploader("📤 Upload Prescription Image (JPG/PNG)", type=["jpg", "jpeg", "png"])
|
47 |
|
48 |
+
if uploaded_file:
|
49 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
|
50 |
+
temp_file.write(uploaded_file.read())
|
51 |
+
orig_path = temp_file.name
|
52 |
|
53 |
+
# Step 1: Read and preprocess image
|
54 |
+
image = cv2.imread(orig_path)
|
55 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
56 |
+
_, binary_inv = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY_INV)
|
57 |
+
kernel = np.ones((3, 3), np.uint8)
|
58 |
+
dilated = cv2.dilate(binary_inv, kernel, iterations=1)
|
59 |
|
60 |
+
# Save preprocessed image for OCR
|
61 |
+
dilated_path = orig_path.replace(".png", "_dilated.png")
|
62 |
+
cv2.imwrite(dilated_path, dilated)
|
63 |
|
64 |
+
loader = UnstructuredImageLoader(dilated_path)
|
65 |
+
documents = loader.load()
|
66 |
+
extracted_text = "\n".join([doc.page_content for doc in documents])
|
67 |
|
68 |
+
template = """
|
69 |
+
You are a helpful assistant. Here is a prescription text extracted from an image:
|
|
|
70 |
|
71 |
+
{prescription_text}
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
+
Please summarize the key medicine names and instructions in bullet points.
|
74 |
+
"""
|
75 |
+
prompt = PromptTemplate(input_variables=["prescription_text"], template=template)
|
76 |
|
77 |
+
model = HuggingFaceEndpoint(
|
78 |
+
repo_id="mistralai/Mistral-7B-Instruct-v0.3",
|
79 |
+
provider="novita",
|
80 |
+
temperature=0.6,
|
81 |
+
max_new_tokens=300,
|
82 |
+
task="conversational"
|
83 |
+
)
|
84 |
+
chain = LLMChain(llm=model, prompt=prompt)
|
85 |
+
|
86 |
+
|
87 |
+
col1, col2 = st.columns([1, 2])
|
88 |
|
89 |
with col1:
|
90 |
st.image(dilated, caption="Preprocessed Prescription", channels="GRAY", use_container_width=True)
|
|
|
94 |
|
95 |
st.markdown("### 📜 Extracted Text")
|
96 |
st.code(extracted_text)
|
97 |
+
|
98 |
if st.button("🔍 Analyze Text"):
|
99 |
with st.spinner("Analyzing..."):
|
100 |
response = chain.run(prescription_text=extracted_text)
|
|
|
101 |
st.success(response)
|
102 |
|
103 |
+
# Cleanup temp files
|
104 |
+
os.remove(orig_path)
|
105 |
+
os.remove(dilated_path)
|
106 |
+
|
107 |
+
else:
|
108 |
+
st.image("https://cdn.dribbble.com/users/285475/screenshots/14711920/media/bd46dc2873f7099e4ef9fd53e6f7f1df.png", width=600)
|
109 |
+
st.markdown("<center><i>Upload a prescription image to begin analysis.</i></center>", unsafe_allow_html=True)
|
110 |
+
|
111 |
+
|
112 |
|
113 |
# st.markdown("### 🌐 Translated Text")
|
114 |
# st.code("पेरासिटामोल 500 मिलीग्राम\nभोजन के बाद दिन में दो बार 1 गोली लें", language='text')
|
|
|
119 |
# st.markdown("### ⚠️ Possible Side Effects")
|
120 |
# st.warning("- Nausea\n- Dizziness\n- Liver damage (on overdose)")
|
121 |
|
122 |
+
# os.remove(temp_path)
|
123 |
+
# os.remove(orig_path)
|
124 |
+
# os.remove(dilated_path)
|
|
|
|
|
|
|
|