Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -23,19 +23,19 @@ st.set_page_config(
|
|
23 |
|
24 |
|
25 |
# Split large response into smaller chunks (for translation)
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
|
40 |
|
41 |
def set_background(image_file):
|
@@ -166,21 +166,33 @@ if uploaded_file:
|
|
166 |
|
167 |
# Prompt LLM
|
168 |
template = """
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
"""
|
185 |
|
186 |
prompt = PromptTemplate(input_variables=["prescription_text"], template=template)
|
@@ -211,7 +223,7 @@ if uploaded_file:
|
|
211 |
with st.spinner("Analyzing with LLM..."):
|
212 |
response = chain.run(prescription_text=text)
|
213 |
st.markdown("#### ๐ก AI-based Medicine Analysis")
|
214 |
-
st.
|
215 |
|
216 |
# Save txt and image
|
217 |
txt_path = "medicine_analysis.txt"
|
@@ -229,35 +241,35 @@ if uploaded_file:
|
|
229 |
with open(img_path, "rb") as img_file:
|
230 |
st.download_button("๐ผ๏ธ English Image", data=img_file, file_name="medicine_analysis.png", mime="image/png")
|
231 |
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
|
262 |
try:
|
263 |
os.remove(orig_path)
|
|
|
23 |
|
24 |
|
25 |
# Split large response into smaller chunks (for translation)
|
26 |
+
def split_text_into_chunks(text, max_length=450):
|
27 |
+
lines = text.split('\n')
|
28 |
+
chunks = []
|
29 |
+
current = ""
|
30 |
+
for line in lines:
|
31 |
+
if len(current) + len(line) + 1 <= max_length:
|
32 |
+
current += line + '\n'
|
33 |
+
else:
|
34 |
+
chunks.append(current.strip())
|
35 |
+
current = line + '\n'
|
36 |
+
if current:
|
37 |
+
chunks.append(current.strip())
|
38 |
+
return chunks
|
39 |
|
40 |
|
41 |
def set_background(image_file):
|
|
|
166 |
|
167 |
# Prompt LLM
|
168 |
template = """
|
169 |
+
You are a helpful and structured medical assistant.
|
170 |
+
|
171 |
+
Below is a prescription text extracted from an image:
|
172 |
+
|
173 |
+
{prescription_text}
|
174 |
+
|
175 |
+
Your tasks:
|
176 |
+
|
177 |
+
1. Identify and list only the **medicine names** mentioned (ignore other irrelevant text).
|
178 |
+
2. For each identified medicine, provide the following:
|
179 |
+
- Dosage and Timing
|
180 |
+
- Possible Side Effects
|
181 |
+
- Special Instructions
|
182 |
+
|
183 |
+
๐งพ Format your response clearly and neatly as follows:
|
184 |
+
|
185 |
+
- Medicine Name 1
|
186 |
+
- Dosage and Timing: ...
|
187 |
+
- Side Effects: ...
|
188 |
+
- Special Instructions: ...
|
189 |
+
|
190 |
+
- Medicine Name 2
|
191 |
+
- Dosage and Timing: ...
|
192 |
+
- Side Effects: ...
|
193 |
+
- Special Instructions: ...
|
194 |
+
|
195 |
+
Ensure each medicine starts with a new bullet point and all details are on separate lines.
|
196 |
"""
|
197 |
|
198 |
prompt = PromptTemplate(input_variables=["prescription_text"], template=template)
|
|
|
223 |
with st.spinner("Analyzing with LLM..."):
|
224 |
response = chain.run(prescription_text=text)
|
225 |
st.markdown("#### ๐ก AI-based Medicine Analysis")
|
226 |
+
st.text_area("LLM Output", response, height=600)
|
227 |
|
228 |
# Save txt and image
|
229 |
txt_path = "medicine_analysis.txt"
|
|
|
241 |
with open(img_path, "rb") as img_file:
|
242 |
st.download_button("๐ผ๏ธ English Image", data=img_file, file_name="medicine_analysis.png", mime="image/png")
|
243 |
|
244 |
+
if response and st.button("๐ Translate to Hindi"):
|
245 |
+
with st.spinner("Translating to Hindi..."):
|
246 |
+
target_lang = "hi"
|
247 |
+
translator = Translator(to_lang=target_lang)
|
248 |
+
chunks = split_text_into_chunks(response, max_length=450)
|
249 |
+
|
250 |
+
hindi_chunks = []
|
251 |
+
for chunk in chunks:
|
252 |
+
try:
|
253 |
+
translated = translator.translate(chunk)
|
254 |
+
hindi_chunks.append(translated)
|
255 |
+
except Exception as e:
|
256 |
+
hindi_chunks.append("[Error translating chunk]")
|
257 |
+
|
258 |
+
hindi_text = "\n\n".join(hindi_chunks)
|
259 |
+
|
260 |
+
st.markdown("#### ๐ Hindi Translation")
|
261 |
+
st.text_area("Translated Output (Hindi)", hindi_text, height=600)
|
262 |
+
|
263 |
+
hindi_img_path = "hindi_output.png"
|
264 |
+
save_text_as_image(hindi_text, hindi_img_path)
|
265 |
+
|
266 |
+
st.markdown("#### ๐ฅ Download (Hindi)")
|
267 |
+
col3, col4 = st.columns(2)
|
268 |
+
with col3:
|
269 |
+
st.download_button("โฌ๏ธ Hindi TXT", data=hindi_text.encode(), file_name="hindi_medicine_analysis.txt")
|
270 |
+
with col4:
|
271 |
+
with open(hindi_img_path, "rb") as img_file:
|
272 |
+
st.download_button("๐ผ๏ธ Hindi Image", data=img_file, file_name="hindi_medicine_analysis.png", mime="image/png")
|
273 |
|
274 |
try:
|
275 |
os.remove(orig_path)
|