Yashvj123 commited on
Commit
d4b093a
·
verified ·
1 Parent(s): 842e3d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +166 -33
app.py CHANGED
@@ -3,19 +3,17 @@ import cv2
3
  import numpy as np
4
  import tempfile
5
  import os
6
- # import pytesseract
7
  import easyocr
8
 
9
- # from langchain.document_loaders.image import UnstructuredImageLoader
10
- # from langchain_community.document_loaders import UnstructuredImageLoader
11
  from langchain.prompts import PromptTemplate
12
  from langchain.chains import LLMChain
13
- from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
14
 
15
  # Set Hugging Face API keys
16
  os.environ["HUGGINGFACEHUB_API_KEY"] = os.getenv("HF")
17
  os.environ["HF_TOKEN"] = os.getenv("HF")
18
 
 
19
  st.set_page_config(
20
  page_title="MediAssist - Prescription Analyzer",
21
  layout="wide",
@@ -25,7 +23,6 @@ st.set_page_config(
25
  st.sidebar.title("💊 MediAssist")
26
  st.sidebar.markdown("Analyze prescriptions with ease using AI")
27
  st.sidebar.markdown("---")
28
-
29
  st.sidebar.markdown("🔗 **Connect with me:**")
30
  st.sidebar.markdown("""
31
  <div style='display: flex; gap: 10px;'>
@@ -53,66 +50,61 @@ if uploaded_file:
53
  temp_file.write(uploaded_file.read())
54
  orig_path = temp_file.name
55
 
56
- # Step 1: Read and preprocess image
57
  image = cv2.imread(orig_path)
58
  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
59
  _, binary_inv = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY_INV)
60
  kernel = np.ones((3, 3), np.uint8)
61
  dilated = cv2.dilate(binary_inv, kernel, iterations=1)
62
 
 
 
 
 
 
63
  reader = easyocr.Reader(['en'])
64
  text_list = reader.readtext(dilated, detail=0)
65
  text = "\n".join(text_list)
66
 
67
- # text = pytesseract.image_to_string(dilated)
68
-
69
- # Save preprocessed image for OCR
70
- # dilated_path = orig_path.replace(".png", "_dilated.png")
71
- # cv2.imwrite(dilated_path, dilated)
72
-
73
- # loader = UnstructuredImageLoader(dilated_path)
74
- # documents = loader.load()
75
- # extracted_text = "\n".join([doc.page_content for doc in documents])
76
-
77
  template = """
78
  You are a helpful medical assistant.
79
-
80
  Here is a prescription text extracted from an image:
81
-
82
  {prescription_text}
83
-
84
  Please do the following:
85
-
86
  1. Extract only the medicine names mentioned in the prescription (ignore any other text).
87
  2. For each medicine, provide:
88
  - When to take it (timing and dosage)
89
  - Possible side effects
90
  - Any special instructions
91
-
92
  Format your answer as bullet points, listing only medicines and their details.
93
  """
94
  prompt = PromptTemplate(input_variables=["prescription_text"], template=template)
95
 
96
  llm_model = HuggingFaceEndpoint(
97
- repo_id="mistralai/Mistral-7B-Instruct-v0.3",
98
- provider="novita",
99
  temperature=0.6,
100
  max_new_tokens=300,
101
  task="conversational"
102
  )
103
 
104
- model = ChatHuggingFace(
105
  llm=llm_model,
106
- repo_id="mistralai/Mistral-7B-Instruct-v0.3",
107
- provider="novita",
108
  temperature=0.6,
109
  max_new_tokens=300,
110
  task="conversational"
111
  )
112
-
113
- chain = LLMChain(llm=model, prompt=prompt)
114
 
115
-
 
116
  col1, col2 = st.columns([1, 2])
117
 
118
  with col1:
@@ -120,16 +112,12 @@ if uploaded_file:
120
 
121
  with col2:
122
  st.success("✅ Prescription Uploaded & Preprocessed Successfully")
123
-
124
  st.markdown("### 📜 Extracted Text")
125
  st.code(text)
126
-
127
- # st.code(extracted_text)
128
 
129
  if st.button("🔍 Analyze Text"):
130
  with st.spinner("Analyzing..."):
131
  response = chain.run(prescription_text=text)
132
- # response = chain.run(prescription_text=extracted_text)
133
  st.success(response)
134
 
135
  # Cleanup temp files
@@ -141,6 +129,151 @@ else:
141
 
142
 
143
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
  # st.markdown("### 🌐 Translated Text")
145
  # st.code("पेरासिटामोल 500 मिलीग्राम\nभोजन के बाद दिन में दो बार 1 गोली लें", language='text')
146
 
 
3
  import numpy as np
4
  import tempfile
5
  import os
 
6
  import easyocr
7
 
 
 
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
 
16
+ # Streamlit page setup
17
  st.set_page_config(
18
  page_title="MediAssist - Prescription Analyzer",
19
  layout="wide",
 
23
  st.sidebar.title("💊 MediAssist")
24
  st.sidebar.markdown("Analyze prescriptions with ease using AI")
25
  st.sidebar.markdown("---")
 
26
  st.sidebar.markdown("🔗 **Connect with me:**")
27
  st.sidebar.markdown("""
28
  <div style='display: flex; gap: 10px;'>
 
50
  temp_file.write(uploaded_file.read())
51
  orig_path = temp_file.name
52
 
53
+ # Preprocessing
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 future reference/removal
61
+ dilated_path = orig_path.replace(".png", "_dilated.png")
62
+ cv2.imwrite(dilated_path, dilated)
63
+
64
+ # OCR using EasyOCR
65
  reader = easyocr.Reader(['en'])
66
  text_list = reader.readtext(dilated, detail=0)
67
  text = "\n".join(text_list)
68
 
69
+ # Prompt Template
 
 
 
 
 
 
 
 
 
70
  template = """
71
  You are a helpful medical assistant.
72
+
73
  Here is a prescription text extracted from an image:
74
+
75
  {prescription_text}
76
+
77
  Please do the following:
78
+
79
  1. Extract only the medicine names mentioned in the prescription (ignore any other text).
80
  2. For each medicine, provide:
81
  - When to take it (timing and dosage)
82
  - Possible side effects
83
  - Any special instructions
84
+
85
  Format your answer as bullet points, listing only medicines and their details.
86
  """
87
  prompt = PromptTemplate(input_variables=["prescription_text"], template=template)
88
 
89
  llm_model = HuggingFaceEndpoint(
90
+ repo_id="aaditya/Llama3-OpenBioLLM-70B",
91
+ provider="nebius",
92
  temperature=0.6,
93
  max_new_tokens=300,
94
  task="conversational"
95
  )
96
 
97
+ llm = ChatHuggingFace(
98
  llm=llm_model,
99
+ repo_id="aaditya/Llama3-OpenBioLLM-70B",
100
+ provider="nebius",
101
  temperature=0.6,
102
  max_new_tokens=300,
103
  task="conversational"
104
  )
 
 
105
 
106
+ chain = LLMChain(llm=llm, prompt=prompt)
107
+
108
  col1, col2 = st.columns([1, 2])
109
 
110
  with col1:
 
112
 
113
  with col2:
114
  st.success("✅ Prescription Uploaded & Preprocessed Successfully")
 
115
  st.markdown("### 📜 Extracted Text")
116
  st.code(text)
 
 
117
 
118
  if st.button("🔍 Analyze Text"):
119
  with st.spinner("Analyzing..."):
120
  response = chain.run(prescription_text=text)
 
121
  st.success(response)
122
 
123
  # Cleanup temp files
 
129
 
130
 
131
 
132
+
133
+
134
+ # import streamlit as st
135
+ # import cv2
136
+ # import numpy as np
137
+ # import tempfile
138
+ # import os
139
+ # # import pytesseract
140
+ # import easyocr
141
+
142
+ # # from langchain.document_loaders.image import UnstructuredImageLoader
143
+ # # from langchain_community.document_loaders import UnstructuredImageLoader
144
+ # from langchain.prompts import PromptTemplate
145
+ # from langchain.chains import LLMChain
146
+ # from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
147
+
148
+ # # Set Hugging Face API keys
149
+ # os.environ["HUGGINGFACEHUB_API_KEY"] = os.getenv("HF")
150
+ # os.environ["HF_TOKEN"] = os.getenv("HF")
151
+
152
+ # st.set_page_config(
153
+ # page_title="MediAssist - Prescription Analyzer",
154
+ # layout="wide",
155
+ # page_icon="💊"
156
+ # )
157
+
158
+ # st.sidebar.title("💊 MediAssist")
159
+ # st.sidebar.markdown("Analyze prescriptions with ease using AI")
160
+ # st.sidebar.markdown("---")
161
+
162
+ # st.sidebar.markdown("�� **Connect with me:**")
163
+ # st.sidebar.markdown("""
164
+ # <div style='display: flex; gap: 10px;'>
165
+ # <a href="https://github.com/Yashvj22" target="_blank">
166
+ # <img src="https://img.shields.io/badge/GitHub-100000?style=for-the-badge&logo=github&logoColor=white" style="height:30px;">
167
+ # </a>
168
+ # <a href="https://www.linkedin.com/in/yash-jadhav-454b0a237/" target="_blank">
169
+ # <img src="https://img.shields.io/badge/LinkedIn-0A66C2?style=for-the-badge&logo=linkedin&logoColor=white" style="height:30px;">
170
+ # </a>
171
+ # </div>
172
+ # """, unsafe_allow_html=True)
173
+ # st.sidebar.markdown("---")
174
+
175
+ # st.markdown("""
176
+ # <h1 style='text-align: center; color: #4A90E2;'>🧠 MediAssist</h1>
177
+ # <h3 style='text-align: center;'>Prescription Analyzer using AI and OCR</h3>
178
+ # <p style='text-align: center;'>Upload a doctor's prescription image, and MediAssist will extract, translate, and explain it for you.</p>
179
+ # <br>
180
+ # """, unsafe_allow_html=True)
181
+
182
+ # uploaded_file = st.file_uploader("📤 Upload Prescription Image (JPG/PNG)", type=["jpg", "jpeg", "png"])
183
+
184
+ # if uploaded_file:
185
+ # with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
186
+ # temp_file.write(uploaded_file.read())
187
+ # orig_path = temp_file.name
188
+
189
+ # # Step 1: Read and preprocess image
190
+ # image = cv2.imread(orig_path)
191
+ # gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
192
+ # _, binary_inv = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY_INV)
193
+ # kernel = np.ones((3, 3), np.uint8)
194
+ # dilated = cv2.dilate(binary_inv, kernel, iterations=1)
195
+
196
+ # reader = easyocr.Reader(['en'])
197
+ # text_list = reader.readtext(dilated, detail=0)
198
+ # text = "\n".join(text_list)
199
+
200
+ # # text = pytesseract.image_to_string(dilated)
201
+
202
+ # # Save preprocessed image for OCR
203
+ # # dilated_path = orig_path.replace(".png", "_dilated.png")
204
+ # # cv2.imwrite(dilated_path, dilated)
205
+
206
+ # # loader = UnstructuredImageLoader(dilated_path)
207
+ # # documents = loader.load()
208
+ # # extracted_text = "\n".join([doc.page_content for doc in documents])
209
+
210
+ # template = """
211
+ # You are a helpful medical assistant.
212
+
213
+ # Here is a prescription text extracted from an image:
214
+
215
+ # {prescription_text}
216
+
217
+ # Please do the following:
218
+
219
+ # 1. Extract only the medicine names mentioned in the prescription (ignore any other text).
220
+ # 2. For each medicine, provide:
221
+ # - When to take it (timing and dosage)
222
+ # - Possible side effects
223
+ # - Any special instructions
224
+
225
+ # Format your answer as bullet points, listing only medicines and their details.
226
+ # """
227
+ # prompt = PromptTemplate(input_variables=["prescription_text"], template=template)
228
+
229
+ # llm_model = HuggingFaceEndpoint(
230
+ # repo_id="aaditya/Llama3-OpenBioLLM-70B",
231
+ # provider="nebius",
232
+ # temperature=0.6,
233
+ # max_new_tokens=300,
234
+ # task="conversational"
235
+ # )
236
+
237
+ # model = ChatHuggingFace(
238
+ # llm=llm_model,
239
+ # repo_id="aaditya/Llama3-OpenBioLLM-70B",
240
+ # provider="nebius",
241
+ # temperature=0.6,
242
+ # max_new_tokens=300,
243
+ # task="conversational"
244
+ # )
245
+
246
+ # chain = LLMChain(llm=model, prompt=prompt)
247
+
248
+
249
+ # col1, col2 = st.columns([1, 2])
250
+
251
+ # with col1:
252
+ # st.image(dilated, caption="Preprocessed Prescription", channels="GRAY", use_container_width=True)
253
+
254
+ # with col2:
255
+ # st.success("✅ Prescription Uploaded & Preprocessed Successfully")
256
+
257
+ # st.markdown("### 📜 Extracted Text")
258
+ # st.code(text)
259
+
260
+ # # st.code(extracted_text)
261
+
262
+ # if st.button("🔍 Analyze Text"):
263
+ # with st.spinner("Analyzing..."):
264
+ # response = chain.run(prescription_text=text)
265
+ # # response = chain.run(prescription_text=extracted_text)
266
+ # st.success(response)
267
+
268
+ # # Cleanup temp files
269
+ # os.remove(orig_path)
270
+ # os.remove(dilated_path)
271
+
272
+ # else:
273
+ # st.markdown("<center><i>Upload a prescription image to begin analysis.</i></center>", unsafe_allow_html=True)
274
+
275
+
276
+
277
  # st.markdown("### 🌐 Translated Text")
278
  # st.code("पेरासिटामोल 500 मिलीग्राम\nभोजन के बाद दिन में दो बार 1 गोली लें", language='text')
279