Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -10,13 +10,27 @@ from translate import Translator
|
|
10 |
from langchain.prompts import PromptTemplate
|
11 |
from langchain.chains import LLMChain
|
12 |
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
13 |
-
from transformers import pipeline
|
14 |
|
15 |
# Set API keys
|
16 |
os.environ["HUGGINGFACEHUB_API_KEY"] = os.getenv("HF")
|
17 |
os.environ["HF_TOKEN"] = os.getenv("HF")
|
18 |
|
19 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
def save_text_as_image(text, file_path):
|
21 |
font = ImageFont.load_default()
|
22 |
lines = text.split('\n')
|
@@ -34,7 +48,7 @@ def save_text_as_image(text, file_path):
|
|
34 |
img.save(file_path)
|
35 |
return file_path
|
36 |
|
37 |
-
# Setup
|
38 |
st.set_page_config(page_title="MediAssist π", layout="wide")
|
39 |
|
40 |
st.markdown("""
|
@@ -59,7 +73,7 @@ if uploaded_file:
|
|
59 |
temp_file.write(uploaded_file.read())
|
60 |
orig_path = temp_file.name
|
61 |
|
62 |
-
#
|
63 |
image = cv2.imread(orig_path)
|
64 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
65 |
_, binary_inv = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY_INV)
|
@@ -74,16 +88,15 @@ if uploaded_file:
|
|
74 |
text_list = reader.readtext(dilated, detail=0)
|
75 |
text = "\n".join(text_list)
|
76 |
|
77 |
-
# Display
|
78 |
col1, col2 = st.columns([1, 2])
|
79 |
with col1:
|
80 |
-
st.image(dilated, caption="Preprocessed Prescription", channels="GRAY", use_container_width=True)
|
81 |
with col2:
|
82 |
st.success("β
Image Uploaded and Preprocessed")
|
83 |
-
st.markdown("#### π Extracted Text
|
84 |
st.code(text)
|
85 |
|
86 |
-
# Prompt
|
87 |
template = """
|
88 |
You are a helpful medical assistant.
|
89 |
Here is a prescription text extracted from an image:
|
@@ -117,51 +130,69 @@ if uploaded_file:
|
|
117 |
|
118 |
chain = LLMChain(llm=llm, prompt=prompt)
|
119 |
|
|
|
|
|
|
|
120 |
if st.button("π Analyze Extracted Text"):
|
121 |
with st.spinner("Analyzing with LLM..."):
|
122 |
response = chain.run(prescription_text=text)
|
123 |
-
st.markdown("#### π‘
|
124 |
-
st.text_area("Output", response, height=300)
|
125 |
|
126 |
# Save txt and image
|
127 |
-
txt_path = "
|
128 |
with open(txt_path, "w") as f:
|
129 |
f.write(response)
|
130 |
|
131 |
-
img_path = "
|
132 |
save_text_as_image(response, img_path)
|
133 |
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
# # Translation to Hindi
|
141 |
-
# translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-hi")
|
142 |
-
# hindi_text = translator(response, max_length=400)[0]['translation_text']
|
143 |
-
|
144 |
-
st.markdown("#### π Translate to Hindi")
|
145 |
-
st.text_area("Translated (Hindi)", hindi_text, height=300)
|
146 |
-
|
147 |
-
st.markdown("#### π₯ Download Options")
|
148 |
-
colA, colB, colC, colD = st.columns(4)
|
149 |
-
with colA:
|
150 |
-
st.download_button("β¬οΈ Download TXT", data=response, file_name="medicine_analysis.txt")
|
151 |
-
with colB:
|
152 |
with open(img_path, "rb") as img_file:
|
153 |
-
st.download_button("πΌοΈ
|
154 |
-
|
155 |
-
|
156 |
-
with
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
|
162 |
# Cleanup
|
163 |
-
|
164 |
-
|
|
|
|
|
|
|
|
|
|
|
165 |
else:
|
166 |
st.markdown("<center><i>πΈ Upload a prescription image to get started</i></center>", unsafe_allow_html=True)
|
167 |
|
|
|
10 |
from langchain.prompts import PromptTemplate
|
11 |
from langchain.chains import LLMChain
|
12 |
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
|
|
13 |
|
14 |
# Set API keys
|
15 |
os.environ["HUGGINGFACEHUB_API_KEY"] = os.getenv("HF")
|
16 |
os.environ["HF_TOKEN"] = os.getenv("HF")
|
17 |
|
18 |
+
# Split large response into smaller chunks (for translation)
|
19 |
+
def split_text_into_chunks(text, max_length=450):
|
20 |
+
lines = text.split('\n')
|
21 |
+
chunks = []
|
22 |
+
current = ""
|
23 |
+
for line in lines:
|
24 |
+
if len(current) + len(line) + 1 <= max_length:
|
25 |
+
current += line + '\n'
|
26 |
+
else:
|
27 |
+
chunks.append(current.strip())
|
28 |
+
current = line + '\n'
|
29 |
+
if current:
|
30 |
+
chunks.append(current.strip())
|
31 |
+
return chunks
|
32 |
+
|
33 |
+
# Save text to image
|
34 |
def save_text_as_image(text, file_path):
|
35 |
font = ImageFont.load_default()
|
36 |
lines = text.split('\n')
|
|
|
48 |
img.save(file_path)
|
49 |
return file_path
|
50 |
|
51 |
+
# Setup UI
|
52 |
st.set_page_config(page_title="MediAssist π", layout="wide")
|
53 |
|
54 |
st.markdown("""
|
|
|
73 |
temp_file.write(uploaded_file.read())
|
74 |
orig_path = temp_file.name
|
75 |
|
76 |
+
# Image preprocessing
|
77 |
image = cv2.imread(orig_path)
|
78 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
79 |
_, binary_inv = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY_INV)
|
|
|
88 |
text_list = reader.readtext(dilated, detail=0)
|
89 |
text = "\n".join(text_list)
|
90 |
|
|
|
91 |
col1, col2 = st.columns([1, 2])
|
92 |
with col1:
|
93 |
+
st.image(dilated, caption="π§Ύ Preprocessed Prescription", channels="GRAY", use_container_width=True)
|
94 |
with col2:
|
95 |
st.success("β
Image Uploaded and Preprocessed")
|
96 |
+
st.markdown("#### π Extracted Text")
|
97 |
st.code(text)
|
98 |
|
99 |
+
# Prompt LLM
|
100 |
template = """
|
101 |
You are a helpful medical assistant.
|
102 |
Here is a prescription text extracted from an image:
|
|
|
130 |
|
131 |
chain = LLMChain(llm=llm, prompt=prompt)
|
132 |
|
133 |
+
response = ""
|
134 |
+
hindi_text = ""
|
135 |
+
|
136 |
if st.button("π Analyze Extracted Text"):
|
137 |
with st.spinner("Analyzing with LLM..."):
|
138 |
response = chain.run(prescription_text=text)
|
139 |
+
st.markdown("#### π‘ AI-based Medicine Analysis")
|
140 |
+
st.text_area("LLM Output", response, height=300)
|
141 |
|
142 |
# Save txt and image
|
143 |
+
txt_path = "medicine_analysis.txt"
|
144 |
with open(txt_path, "w") as f:
|
145 |
f.write(response)
|
146 |
|
147 |
+
img_path = "medicine_analysis.png"
|
148 |
save_text_as_image(response, img_path)
|
149 |
|
150 |
+
st.markdown("#### π₯ Download (English)")
|
151 |
+
col1, col2 = st.columns(2)
|
152 |
+
with col1:
|
153 |
+
st.download_button("β¬οΈ English TXT", data=response.encode(), file_name="medicine_analysis.txt")
|
154 |
+
with col2:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
with open(img_path, "rb") as img_file:
|
156 |
+
st.download_button("πΌοΈ English Image", data=img_file, file_name="medicine_analysis.png", mime="image/png")
|
157 |
+
|
158 |
+
if response and st.button("π Translate to Hindi"):
|
159 |
+
with st.spinner("Translating to Hindi..."):
|
160 |
+
target_lang = "hi"
|
161 |
+
translator = Translator(to_lang=target_lang)
|
162 |
+
chunks = split_text_into_chunks(response, max_length=450)
|
163 |
+
|
164 |
+
hindi_chunks = []
|
165 |
+
for chunk in chunks:
|
166 |
+
try:
|
167 |
+
translated = translator.translate(chunk)
|
168 |
+
hindi_chunks.append(translated)
|
169 |
+
except Exception as e:
|
170 |
+
hindi_chunks.append("[Error translating chunk]")
|
171 |
+
|
172 |
+
hindi_text = "\n\n".join(hindi_chunks)
|
173 |
+
|
174 |
+
st.markdown("#### π Hindi Translation")
|
175 |
+
st.text_area("Translated Output (Hindi)", hindi_text, height=300)
|
176 |
+
|
177 |
+
hindi_img_path = "hindi_output.png"
|
178 |
+
save_text_as_image(hindi_text, hindi_img_path)
|
179 |
+
|
180 |
+
st.markdown("#### π₯ Download (Hindi)")
|
181 |
+
col3, col4 = st.columns(2)
|
182 |
+
with col3:
|
183 |
+
st.download_button("β¬οΈ Hindi TXT", data=hindi_text.encode(), file_name="hindi_medicine_analysis.txt")
|
184 |
+
with col4:
|
185 |
+
with open(hindi_img_path, "rb") as img_file:
|
186 |
+
st.download_button("πΌοΈ Hindi Image", data=img_file, file_name="hindi_medicine_analysis.png", mime="image/png")
|
187 |
|
188 |
# Cleanup
|
189 |
+
try:
|
190 |
+
os.remove(orig_path)
|
191 |
+
os.remove(dilated_path)
|
192 |
+
llm.clear()
|
193 |
+
except:
|
194 |
+
pass
|
195 |
+
|
196 |
else:
|
197 |
st.markdown("<center><i>πΈ Upload a prescription image to get started</i></center>", unsafe_allow_html=True)
|
198 |
|