bakrianoo commited on
Commit
d7a887c
·
1 Parent(s): 0baabbd

download translated contents

Browse files
Files changed (1) hide show
  1. app.py +11 -6
app.py CHANGED
@@ -196,11 +196,14 @@ def generate_download_original(download_format, article_title, article_summary,
196
  f.write(content)
197
  return temp_path
198
 
199
- def generate_download_translated(download_format, article_title, article_summary, translations):
200
  """
201
- Generate a downloadable translated content file in the specified format.
202
  """
203
- # Prepare content and filename
 
 
 
204
  if download_format == "Wikipedia XML":
205
  parts = [f"<article title=\"{article_title}\">"]
206
  for title, text in translations.items():
@@ -219,13 +222,13 @@ def generate_download_translated(download_format, article_title, article_summary
219
  obj = {"title": article_title, "summary": article_summary, "sections": translations}
220
  content = json.dumps(obj, ensure_ascii=False, indent=2)
221
  filename = f"{article_title or 'article'}_translated.json"
222
- else: # Plain Text
223
  parts = [article_title or 'Article', article_summary or '']
224
  for title, text in translations.items():
225
  parts.append(f"## {title}\n{text}")
226
  content = "\n\n".join(parts)
227
  filename = f"{article_title or 'article'}_translated.txt"
228
- # Write to a temp file and return its path
229
  temp_path = os.path.join(tempfile.gettempdir(), filename)
230
  with open(temp_path, 'w', encoding='utf-8') as f:
231
  f.write(content)
@@ -532,9 +535,11 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
532
  inputs=[download_format, article_title, aticle_summary, article_xml, sections_state],
533
  outputs=[download_original_file]
534
  )
 
 
535
  download_translated_btn.click(
536
  fn=generate_download_translated,
537
- inputs=[download_format, article_title, aticle_summary, sections_state],
538
  outputs=[download_translated_file]
539
  )
540
 
 
196
  f.write(content)
197
  return temp_path
198
 
199
+ def generate_download_translated(download_format, article_title, article_summary, sections, *translations_values):
200
  """
201
+ Generate downloadable translated content file from existing translations.
202
  """
203
+ # Build translations dict from provided UI values
204
+ section_titles = list(sections.keys())
205
+ translations = {section_titles[i]: translations_values[i] for i in range(min(len(section_titles), len(translations_values)))}
206
+ # Build downloadable content
207
  if download_format == "Wikipedia XML":
208
  parts = [f"<article title=\"{article_title}\">"]
209
  for title, text in translations.items():
 
222
  obj = {"title": article_title, "summary": article_summary, "sections": translations}
223
  content = json.dumps(obj, ensure_ascii=False, indent=2)
224
  filename = f"{article_title or 'article'}_translated.json"
225
+ else:
226
  parts = [article_title or 'Article', article_summary or '']
227
  for title, text in translations.items():
228
  parts.append(f"## {title}\n{text}")
229
  content = "\n\n".join(parts)
230
  filename = f"{article_title or 'article'}_translated.txt"
231
+ # Write to temp file
232
  temp_path = os.path.join(tempfile.gettempdir(), filename)
233
  with open(temp_path, 'w', encoding='utf-8') as f:
234
  f.write(content)
 
535
  inputs=[download_format, article_title, aticle_summary, article_xml, sections_state],
536
  outputs=[download_original_file]
537
  )
538
+ # Prepare existing translation outputs for download
539
+ translation_outputs = [section_components[i+2] for i in range(0, len(section_components), 4)]
540
  download_translated_btn.click(
541
  fn=generate_download_translated,
542
+ inputs=[download_format, article_title, aticle_summary, sections_state] + translation_outputs,
543
  outputs=[download_translated_file]
544
  )
545