Yashvj123 commited on
Commit
01c5184
ยท
verified ยท
1 Parent(s): fb58074

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -58
app.py CHANGED
@@ -23,19 +23,19 @@ st.set_page_config(
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,21 +166,33 @@ if uploaded_file:
166
 
167
  # Prompt LLM
168
  template = """
169
- You are a helpful medical assistant.
170
-
171
- Here is a prescription text extracted from an image:
172
-
173
- {prescription_text}
174
-
175
- Please do the following:
176
-
177
- 1. Extract only the medicine names mentioned in the prescription (ignore any other text).
178
- 2. For each medicine, provide:
179
- - When to take it (timing and dosage)
180
- - Possible side effects
181
- - Any special instructions
182
-
183
- Format your answer as bullet points, listing only medicines and their details.
 
 
 
 
 
 
 
 
 
 
 
 
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.success(response)
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
- # if response and st.button("๐ŸŒ Translate to Hindi"):
233
- # with st.spinner("Translating to Hindi..."):
234
- # target_lang = "hi"
235
- # translator = Translator(to_lang=target_lang)
236
- # chunks = split_text_into_chunks(response, max_length=450)
237
-
238
- # hindi_chunks = []
239
- # for chunk in chunks:
240
- # try:
241
- # translated = translator.translate(chunk)
242
- # hindi_chunks.append(translated)
243
- # except Exception as e:
244
- # hindi_chunks.append("[Error translating chunk]")
245
-
246
- # hindi_text = "\n\n".join(hindi_chunks)
247
-
248
- # st.markdown("#### ๐ŸŒ Hindi Translation")
249
- # st.text_area("Translated Output (Hindi)", hindi_text, height=300)
250
-
251
- # hindi_img_path = "hindi_output.png"
252
- # save_text_as_image(hindi_text, hindi_img_path)
253
-
254
- # st.markdown("#### ๐Ÿ“ฅ Download (Hindi)")
255
- # col3, col4 = st.columns(2)
256
- # with col3:
257
- # st.download_button("โฌ‡๏ธ Hindi TXT", data=hindi_text.encode(), file_name="hindi_medicine_analysis.txt")
258
- # with col4:
259
- # with open(hindi_img_path, "rb") as img_file:
260
- # st.download_button("๐Ÿ–ผ๏ธ Hindi Image", data=img_file, file_name="hindi_medicine_analysis.png", mime="image/png")
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)