Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -52,10 +52,10 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as app:
|
|
| 52 |
language_dropdown = gr.Dropdown(choices=language_choices, label="Translate to", value="Urdu")
|
| 53 |
translate_btn = gr.Button("π Translate")
|
| 54 |
with gr.Column():
|
| 55 |
-
output_text = gr.Textbox(label="Translated Text", lines=4)
|
| 56 |
-
copy_btn = gr.Button("π Copy to Clipboard")
|
| 57 |
download_btn = gr.Button("β¬οΈ Download Translation", visible=False)
|
| 58 |
-
|
| 59 |
with gr.Accordion("π Translation History", open=False):
|
| 60 |
history_box = gr.Markdown("")
|
| 61 |
|
|
@@ -71,8 +71,6 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as app:
|
|
| 71 |
|
| 72 |
translate_btn.click(fn=update_history, outputs=[history_box])
|
| 73 |
|
| 74 |
-
copy_btn.click(fn=None, inputs=[], outputs=[], _js="() => navigator.clipboard.writeText(document.querySelector('textarea[aria-label=\"Translated Text\"]').value)")
|
| 75 |
-
|
| 76 |
def download_text(translated_text):
|
| 77 |
path = "/mnt/data/translation.txt"
|
| 78 |
with open(path, "w", encoding="utf-8") as f:
|
|
@@ -81,5 +79,23 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as app:
|
|
| 81 |
|
| 82 |
download_btn.click(download_text, inputs=[output_text], outputs=[download_btn])
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
# π Launch app
|
| 85 |
app.launch()
|
|
|
|
| 52 |
language_dropdown = gr.Dropdown(choices=language_choices, label="Translate to", value="Urdu")
|
| 53 |
translate_btn = gr.Button("π Translate")
|
| 54 |
with gr.Column():
|
| 55 |
+
output_text = gr.Textbox(label="Translated Text", lines=4, elem_id="output-box")
|
| 56 |
+
copy_btn = gr.Button("π Copy to Clipboard", elem_id="copy-btn")
|
| 57 |
download_btn = gr.Button("β¬οΈ Download Translation", visible=False)
|
| 58 |
+
|
| 59 |
with gr.Accordion("π Translation History", open=False):
|
| 60 |
history_box = gr.Markdown("")
|
| 61 |
|
|
|
|
| 71 |
|
| 72 |
translate_btn.click(fn=update_history, outputs=[history_box])
|
| 73 |
|
|
|
|
|
|
|
| 74 |
def download_text(translated_text):
|
| 75 |
path = "/mnt/data/translation.txt"
|
| 76 |
with open(path, "w", encoding="utf-8") as f:
|
|
|
|
| 79 |
|
| 80 |
download_btn.click(download_text, inputs=[output_text], outputs=[download_btn])
|
| 81 |
|
| 82 |
+
# π§ JavaScript for copy button
|
| 83 |
+
gr.HTML("""
|
| 84 |
+
<script>
|
| 85 |
+
document.addEventListener("DOMContentLoaded", function() {
|
| 86 |
+
const copyBtn = document.querySelector('#copy-btn button');
|
| 87 |
+
copyBtn.addEventListener("click", function() {
|
| 88 |
+
const outputBox = document.querySelector('textarea#output-box');
|
| 89 |
+
if (outputBox) {
|
| 90 |
+
navigator.clipboard.writeText(outputBox.value).then(() => {
|
| 91 |
+
copyBtn.innerText = "β
Copied!";
|
| 92 |
+
setTimeout(() => { copyBtn.innerText = "π Copy to Clipboard"; }, 1500);
|
| 93 |
+
});
|
| 94 |
+
}
|
| 95 |
+
});
|
| 96 |
+
});
|
| 97 |
+
</script>
|
| 98 |
+
""")
|
| 99 |
+
|
| 100 |
# π Launch app
|
| 101 |
app.launch()
|