Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from deep_translator import GoogleTranslator
|
| 3 |
import json
|
|
|
|
| 4 |
|
| 5 |
# Fetch supported languages dynamically
|
| 6 |
SUPPORTED_LANGUAGES = GoogleTranslator().get_supported_languages()
|
|
@@ -17,45 +18,92 @@ target_languages = st.multiselect("π― Select target languages", SUPPORTED_LANG
|
|
| 17 |
# Input type selection
|
| 18 |
input_type = st.radio("π Choose input method", ["Text", "TXT File", "JSON File"])
|
| 19 |
|
| 20 |
-
def translate_text(text,
|
| 21 |
-
"""Translate text
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
translations[lang] = GoogleTranslator(source='auto', target=lang).translate(text)
|
| 25 |
-
return translations
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
translated_json = {}
|
| 30 |
-
|
|
|
|
| 31 |
if isinstance(value, dict):
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
else:
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
return translated_json
|
| 36 |
|
| 37 |
if input_type == "Text":
|
| 38 |
-
user_text = st.text_area("
|
| 39 |
if st.button("Translate"):
|
| 40 |
if user_text.strip():
|
| 41 |
-
|
| 42 |
-
st.json(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
else:
|
| 44 |
st.warning("Please enter text to translate.")
|
| 45 |
|
| 46 |
elif input_type == "TXT File":
|
| 47 |
uploaded_file = st.file_uploader("π Upload a TXT file", type=["txt"])
|
| 48 |
-
if uploaded_file
|
| 49 |
content = uploaded_file.read().decode("utf-8")
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
elif input_type == "JSON File":
|
| 54 |
uploaded_json = st.file_uploader("π Upload a JSON file", type=["json"])
|
| 55 |
-
if uploaded_json
|
| 56 |
content = json.load(uploaded_json)
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
st.markdown("π
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from deep_translator import GoogleTranslator
|
| 3 |
import json
|
| 4 |
+
import re
|
| 5 |
|
| 6 |
# Fetch supported languages dynamically
|
| 7 |
SUPPORTED_LANGUAGES = GoogleTranslator().get_supported_languages()
|
|
|
|
| 18 |
# Input type selection
|
| 19 |
input_type = st.radio("π Choose input method", ["Text", "TXT File", "JSON File"])
|
| 20 |
|
| 21 |
+
def translate_text(text, lang):
|
| 22 |
+
"""Translate text while preserving placeholders inside {{ }}"""
|
| 23 |
+
placeholders = re.findall(r"(\{\{.*?\}\})", text) # Extract placeholders
|
| 24 |
+
translated_text = GoogleTranslator(source="auto", target=lang).translate(text)
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# Restore placeholders to keep them unchanged
|
| 27 |
+
for placeholder in placeholders:
|
| 28 |
+
translated_text = translated_text.replace(GoogleTranslator(source="auto", target=lang).translate(placeholder), placeholder)
|
| 29 |
+
|
| 30 |
+
return translated_text
|
| 31 |
+
|
| 32 |
+
def translate_json_values(json_data, lang):
|
| 33 |
+
"""
|
| 34 |
+
Translate JSON values while preserving the original keys in quotes.
|
| 35 |
+
"""
|
| 36 |
translated_json = {}
|
| 37 |
+
|
| 38 |
+
for key, value in json_data.items():
|
| 39 |
if isinstance(value, dict):
|
| 40 |
+
# Recursive translation for nested dictionaries
|
| 41 |
+
translated_json[key] = translate_json_values(value, lang)
|
| 42 |
+
elif isinstance(value, list):
|
| 43 |
+
# Translate list elements
|
| 44 |
+
translated_json[key] = [translate_text(item, lang) if isinstance(item, str) else item for item in value]
|
| 45 |
+
elif isinstance(value, str):
|
| 46 |
+
# Translate string values only
|
| 47 |
+
translated_json[key] = translate_text(value, lang)
|
| 48 |
else:
|
| 49 |
+
# Keep numbers/booleans unchanged
|
| 50 |
+
translated_json[key] = value
|
| 51 |
+
|
| 52 |
return translated_json
|
| 53 |
|
| 54 |
if input_type == "Text":
|
| 55 |
+
user_text = st.text_area("β Enter text to translate:")
|
| 56 |
if st.button("Translate"):
|
| 57 |
if user_text.strip():
|
| 58 |
+
results = {lang: translate_text(user_text, lang) for lang in target_languages}
|
| 59 |
+
st.json(results)
|
| 60 |
+
|
| 61 |
+
# Provide download button for TXT file
|
| 62 |
+
for lang, translated_text in results.items():
|
| 63 |
+
file_name = f"translated_{lang}.txt"
|
| 64 |
+
st.download_button(
|
| 65 |
+
label=f"β¬ Download Translated TXT ({lang})",
|
| 66 |
+
data=translated_text,
|
| 67 |
+
file_name=file_name,
|
| 68 |
+
mime="text/plain"
|
| 69 |
+
)
|
| 70 |
else:
|
| 71 |
st.warning("Please enter text to translate.")
|
| 72 |
|
| 73 |
elif input_type == "TXT File":
|
| 74 |
uploaded_file = st.file_uploader("π Upload a TXT file", type=["txt"])
|
| 75 |
+
if uploaded_file:
|
| 76 |
content = uploaded_file.read().decode("utf-8")
|
| 77 |
+
if st.button("Translate"):
|
| 78 |
+
results = {lang: translate_text(content, lang) for lang in target_languages}
|
| 79 |
+
st.json(results)
|
| 80 |
+
|
| 81 |
+
# Provide download button for translated TXT files
|
| 82 |
+
for lang, translated_text in results.items():
|
| 83 |
+
file_name = f"translated_{lang}.txt"
|
| 84 |
+
st.download_button(
|
| 85 |
+
label=f"β¬ Download Translated TXT ({lang})",
|
| 86 |
+
data=translated_text,
|
| 87 |
+
file_name=file_name,
|
| 88 |
+
mime="text/plain"
|
| 89 |
+
)
|
| 90 |
|
| 91 |
elif input_type == "JSON File":
|
| 92 |
uploaded_json = st.file_uploader("π Upload a JSON file", type=["json"])
|
| 93 |
+
if uploaded_json:
|
| 94 |
content = json.load(uploaded_json)
|
| 95 |
+
translated_results = {lang: translate_json_values(content, lang) for lang in target_languages}
|
| 96 |
+
|
| 97 |
+
st.json(translated_results)
|
| 98 |
+
|
| 99 |
+
# Provide download button for translated JSON files
|
| 100 |
+
for lang, data in translated_results.items():
|
| 101 |
+
file_name = f"translated_{lang}.json"
|
| 102 |
+
st.download_button(
|
| 103 |
+
label=f"β¬ Download Translated JSON ({lang})",
|
| 104 |
+
data=json.dumps(data, indent=4, ensure_ascii=False),
|
| 105 |
+
file_name=file_name,
|
| 106 |
+
mime="application/json"
|
| 107 |
+
)
|
| 108 |
|
| 109 |
+
st.markdown("π *Supports over 100 languages!*")
|