Mahrukhh commited on
Commit
ebafdf2
Β·
verified Β·
1 Parent(s): 88af9f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -13
app.py CHANGED
@@ -17,7 +17,7 @@ translation_history = []
17
  # 🌐 Translation function
18
  def translate_text(text, target_lang):
19
  if not text.strip() or not target_lang.strip():
20
- return "⚠️ Please enter both English text and target language.", "", ""
21
 
22
  prompt = f"Translate the following English text to {target_lang}:\n\n{text}"
23
 
@@ -30,16 +30,16 @@ def translate_text(text, target_lang):
30
  translated = response.choices[0].message.content.strip()
31
 
32
  # Save history
33
- translation_history.append((text, translated))
34
 
35
  # Write to file for download
36
  filename = "/mnt/data/translated_text.txt"
37
  with open(filename, "w", encoding="utf-8") as f:
38
  f.write(translated)
39
 
40
- return translated, gr.update(visible=True), filename
41
  except Exception as e:
42
- return f"❌ Error: {e}", gr.update(visible=False), ""
43
 
44
  # ✨ Gradio UI
45
  with gr.Blocks(theme=gr.themes.Soft()) as app:
@@ -55,19 +55,14 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
55
  download_btn = gr.File(label="Download Translated Text", visible=False)
56
 
57
  with gr.Accordion("πŸ“œ Translation History", open=False):
58
- history_display = gr.HighlightedText(label="Past Translations")
59
-
60
- def update_history():
61
- if translation_history:
62
- return {"value": [{"text": f"{src} ➜ {tgt}"} for src, tgt in translation_history]}
63
- else:
64
- return {"value": []}
65
 
 
66
  translate_btn.click(
67
  translate_text,
68
  inputs=[input_text, target_lang],
69
- outputs=[translated_output, download_btn, download_btn]
70
- ).then(update_history, outputs=[history_display])
71
 
72
  # πŸš€ Launch
73
  app.launch()
 
17
  # 🌐 Translation function
18
  def translate_text(text, target_lang):
19
  if not text.strip() or not target_lang.strip():
20
+ return "⚠️ Please enter both English text and target language.", "", "", ""
21
 
22
  prompt = f"Translate the following English text to {target_lang}:\n\n{text}"
23
 
 
30
  translated = response.choices[0].message.content.strip()
31
 
32
  # Save history
33
+ translation_history.append(f"ENGLISH: {text}\nTRANSLATED ({target_lang}): {translated}\n")
34
 
35
  # Write to file for download
36
  filename = "/mnt/data/translated_text.txt"
37
  with open(filename, "w", encoding="utf-8") as f:
38
  f.write(translated)
39
 
40
+ return translated, gr.update(visible=True), filename, "\n---\n".join(translation_history)
41
  except Exception as e:
42
+ return f"❌ Error: {e}", gr.update(visible=False), "", "\n".join(translation_history)
43
 
44
  # ✨ Gradio UI
45
  with gr.Blocks(theme=gr.themes.Soft()) as app:
 
55
  download_btn = gr.File(label="Download Translated Text", visible=False)
56
 
57
  with gr.Accordion("πŸ“œ Translation History", open=False):
58
+ history_box = gr.Textbox(label="", lines=10, interactive=False)
 
 
 
 
 
 
59
 
60
+ # Bind the translate button
61
  translate_btn.click(
62
  translate_text,
63
  inputs=[input_text, target_lang],
64
+ outputs=[translated_output, download_btn, download_btn, history_box]
65
+ )
66
 
67
  # πŸš€ Launch
68
  app.launch()