Yashvj123 commited on
Commit
49e28e2
Β·
verified Β·
1 Parent(s): 01c5184

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -27
app.py CHANGED
@@ -22,22 +22,6 @@ st.set_page_config(
22
  )
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):
42
  with open(image_file, "rb") as image:
43
  encoded = base64.b64encode(image.read()).decode()
@@ -90,6 +74,21 @@ def set_background(image_file):
90
  unsafe_allow_html=True
91
  )
92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
94
  def save_text_as_image(text, file_path):
95
  font = ImageFont.load_default()
@@ -239,30 +238,26 @@ if uploaded_file:
239
  st.download_button("⬇️ English TXT", data=response.encode(), file_name="medicine_analysis.txt")
240
  with col2:
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:
 
22
  )
23
 
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  def set_background(image_file):
26
  with open(image_file, "rb") as image:
27
  encoded = base64.b64encode(image.read()).decode()
 
74
  unsafe_allow_html=True
75
  )
76
 
77
+ # Split large response into smaller chunks (for translation)
78
+ def split_text_into_chunks(text, max_length=450):
79
+ lines = text.split('\n')
80
+ chunks = []
81
+ current = ""
82
+ for line in lines:
83
+ if len(current) + len(line) + 1 <= max_length:
84
+ current += line + '\n'
85
+ else:
86
+ chunks.append(current.strip())
87
+ current = line + '\n'
88
+ if current:
89
+ chunks.append(current.strip())
90
+ return chunks
91
+
92
 
93
  def save_text_as_image(text, file_path):
94
  font = ImageFont.load_default()
 
238
  st.download_button("⬇️ English TXT", data=response.encode(), file_name="medicine_analysis.txt")
239
  with col2:
240
  with open(img_path, "rb") as img_file:
241
+ st.download_button("πŸ–ΌοΈ English Image", data=img_file, file_name="medicine_analysis.png", mime="image/png")\
242
+
243
  if response and st.button("🌐 Translate to Hindi"):
244
  with st.spinner("Translating to Hindi..."):
245
+ chunks = split_text_into_chunks(response, max_length=100)
 
 
 
246
  hindi_chunks = []
247
  for chunk in chunks:
248
  try:
249
+ translated = GoogleTranslator(source='auto', target='hi').translate(chunk)
250
  hindi_chunks.append(translated)
251
  except Exception as e:
252
  hindi_chunks.append("[Error translating chunk]")
 
253
  hindi_text = "\n\n".join(hindi_chunks)
254
+
255
  st.markdown("#### 🌐 Hindi Translation")
256
  st.text_area("Translated Output (Hindi)", hindi_text, height=600)
257
+
258
  hindi_img_path = "hindi_output.png"
259
  save_text_as_image(hindi_text, hindi_img_path)
260
+
261
  st.markdown("#### πŸ“₯ Download (Hindi)")
262
  col3, col4 = st.columns(2)
263
  with col3: