Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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(
|
| 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 |
-
|
| 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 |
-
)
|
| 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()
|