bakrianoo commited on
Commit
61057b2
·
1 Parent(s): 49b1cdf

download data for different formats

Browse files
Files changed (1) hide show
  1. app.py +92 -1
app.py CHANGED
@@ -4,6 +4,8 @@ from utils import (extract_wiki_id, get_wiki_details,
4
  get_translate_prompt)
5
  import json
6
  import json_repair
 
 
7
 
8
  # Define language options for translation
9
  LANGUAGES = {
@@ -162,6 +164,73 @@ def format_debug_info(debug_info):
162
 
163
  return markdown
164
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
  # Add this function to update UI with sections from Wikipedia content
166
  def update_ui_with_sections(sections):
167
  """
@@ -428,8 +497,18 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
428
  outputs=[sidebar_expanded, sidebar, main_content, sidebar_show_btn]
429
  )
430
 
431
- # Add debug info display to the bottom of the sidebar
432
  with sidebar:
 
 
 
 
 
 
 
 
 
 
433
  debug_header = gr.Markdown("### Debug Information", visible=False)
434
  debug_display = gr.Markdown(visible=False)
435
 
@@ -446,6 +525,18 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
446
  fn=lambda: (gr.update(visible=True), gr.update(visible=True)),
447
  outputs=[debug_header, debug_display]
448
  )
 
 
 
 
 
 
 
 
 
 
 
 
449
 
450
  # Launch the app
451
  if __name__ == "__main__":
 
4
  get_translate_prompt)
5
  import json
6
  import json_repair
7
+ import os
8
+ import tempfile
9
 
10
  # Define language options for translation
11
  LANGUAGES = {
 
164
 
165
  return markdown
166
 
167
+ # Functions to generate downloadable content for original and translated articles
168
+ def generate_download_original(download_format, article_title, article_summary, article_xml, sections):
169
+ """
170
+ Generate a downloadable original content file in the specified format.
171
+ """
172
+ # Prepare content and filename
173
+ if download_format == "Wikipedia XML":
174
+ content = article_xml or ""
175
+ filename = f"{article_title or 'article'}.xml"
176
+ elif download_format == "HTML":
177
+ parts = [f"<h1>{article_title}</h1>", f"<p>{article_summary}</p>"]
178
+ for title, text in sections.items():
179
+ parts.append(f"<h2>{title}</h2>")
180
+ parts.append(f"<p>{text}</p>")
181
+ content = "\n".join(parts)
182
+ filename = f"{article_title or 'article'}.html"
183
+ elif download_format == "JSON":
184
+ obj = {"title": article_title, "summary": article_summary, "sections": sections}
185
+ content = json.dumps(obj, ensure_ascii=False, indent=2)
186
+ filename = f"{article_title or 'article'}.json"
187
+ else: # Plain Text
188
+ parts = [article_title or 'Article', article_summary or '']
189
+ for title, text in sections.items():
190
+ parts.append(f"## {title}\n{text}")
191
+ content = "\n\n".join(parts)
192
+ filename = f"{article_title or 'article'}.txt"
193
+ # Write to a temp file and return its path
194
+ temp_path = os.path.join(tempfile.gettempdir(), filename)
195
+ with open(temp_path, 'w', encoding='utf-8') as f:
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():
207
+ parts.append(f" <section title=\"{title}\">{text}</section>")
208
+ parts.append("</article>")
209
+ content = "\n".join(parts)
210
+ filename = f"{article_title or 'article'}_translated.xml"
211
+ elif download_format == "HTML":
212
+ parts = [f"<h1>{article_title}</h1>", f"<p>{article_summary}</p>"]
213
+ for title, text in translations.items():
214
+ parts.append(f"<h2>{title}</h2>")
215
+ parts.append(f"<p>{text}</p>")
216
+ content = "\n".join(parts)
217
+ filename = f"{article_title or 'article'}_translated.html"
218
+ elif download_format == "JSON":
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)
232
+ return temp_path
233
+
234
  # Add this function to update UI with sections from Wikipedia content
235
  def update_ui_with_sections(sections):
236
  """
 
497
  outputs=[sidebar_expanded, sidebar, main_content, sidebar_show_btn]
498
  )
499
 
500
+ # Add download options to the bottom of the sidebar
501
  with sidebar:
502
+ download_format = gr.Dropdown(
503
+ choices=["Wikipedia XML", "HTML", "JSON", "Plain Text"],
504
+ value="Wikipedia XML",
505
+ label="Download Format"
506
+ )
507
+ download_original_btn = gr.Button("Download Original")
508
+ download_original_file = gr.File(label="Original Article")
509
+ download_translated_btn = gr.Button("Download Translated")
510
+ download_translated_file = gr.File(label="Translated Article")
511
+ # Debug info display
512
  debug_header = gr.Markdown("### Debug Information", visible=False)
513
  debug_display = gr.Markdown(visible=False)
514
 
 
525
  fn=lambda: (gr.update(visible=True), gr.update(visible=True)),
526
  outputs=[debug_header, debug_display]
527
  )
528
+
529
+ # Connect download buttons
530
+ download_original_btn.click(
531
+ fn=generate_download_original,
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
 
541
  # Launch the app
542
  if __name__ == "__main__":