Spaces:
Sleeping
Sleeping
fadliaulawi
commited on
Commit
·
1f7f38a
1
Parent(s):
1f48a9f
Add explanation
Browse files
app.py
CHANGED
@@ -15,7 +15,7 @@ load_dotenv()
|
|
15 |
st.title("Azure Translation Tools")
|
16 |
uploaded_files = st.file_uploader("Upload files to start the process", accept_multiple_files=True)
|
17 |
|
18 |
-
#
|
19 |
langs = (
|
20 |
'id - Indonesian',
|
21 |
'en - English',
|
@@ -33,47 +33,53 @@ langs = (
|
|
33 |
'th - Thai',
|
34 |
)
|
35 |
|
|
|
36 |
lang = st.selectbox('Target language selection:', langs, key='lang')
|
37 |
-
lang_id = lang.split()[0]
|
38 |
-
lang_name = lang.split()[-1]
|
39 |
|
40 |
if uploaded_files:
|
41 |
submit = st.button("Get Result", key='submit')
|
42 |
|
43 |
if uploaded_files and submit:
|
44 |
-
|
45 |
-
# Create a zip file in memory
|
46 |
zip_buffer = io.BytesIO()
|
47 |
with zipfile.ZipFile(zip_buffer, 'w') as zip_file:
|
48 |
-
# Add progress bar
|
49 |
progress_bar = st.progress(0)
|
50 |
for idx, uploaded_file in enumerate(uploaded_files):
|
51 |
file_name = uploaded_file.name
|
52 |
file_content = uploaded_file.read()
|
53 |
|
|
|
54 |
headers = {
|
55 |
"Ocp-Apim-Subscription-Key": os.environ["AZURE_AI_TRANSLATOR_KEY"],
|
56 |
}
|
57 |
|
|
|
58 |
files = {
|
59 |
"document": (file_name, file_content, "ContentType/file-extension"),
|
60 |
}
|
61 |
|
|
|
62 |
url = f"{os.environ["AZURE_AI_ENDPOINT_URL"]}/translator/document:translate?targetLanguage={lang_id}&api-version={os.environ["AZURE_AI_API_VERSION"]}"
|
|
|
|
|
63 |
response = requests.post(url, headers=headers, files=files)
|
64 |
|
|
|
65 |
if response.status_code == 200:
|
66 |
-
# Add translated file to zip
|
67 |
zip_file.writestr(f"{lang_name}-translated-{file_name}", response.content)
|
68 |
st.success(f"Successfully translated: {file_name}")
|
69 |
else:
|
70 |
st.error(f"Failed to translate {file_name} with status code {response.status_code}: {response.text}")
|
71 |
|
72 |
-
# Update progress bar
|
73 |
progress = (idx + 1) / len(uploaded_files)
|
74 |
progress_bar.progress(progress)
|
75 |
|
76 |
-
#
|
77 |
st.download_button(
|
78 |
label="Download All Translated Files",
|
79 |
data=zip_buffer.getvalue(),
|
|
|
15 |
st.title("Azure Translation Tools")
|
16 |
uploaded_files = st.file_uploader("Upload files to start the process", accept_multiple_files=True)
|
17 |
|
18 |
+
# Define available language options with their codes and names
|
19 |
langs = (
|
20 |
'id - Indonesian',
|
21 |
'en - English',
|
|
|
33 |
'th - Thai',
|
34 |
)
|
35 |
|
36 |
+
# Get user's language selection and extract language code and name
|
37 |
lang = st.selectbox('Target language selection:', langs, key='lang')
|
38 |
+
lang_id = lang.split()[0] # Get language code (e.g., 'en')
|
39 |
+
lang_name = lang.split()[-1] # Get language name (e.g., 'English')
|
40 |
|
41 |
if uploaded_files:
|
42 |
submit = st.button("Get Result", key='submit')
|
43 |
|
44 |
if uploaded_files and submit:
|
45 |
+
# Create an in-memory zip file to store translated documents
|
|
|
46 |
zip_buffer = io.BytesIO()
|
47 |
with zipfile.ZipFile(zip_buffer, 'w') as zip_file:
|
48 |
+
# Add progress bar for translation status
|
49 |
progress_bar = st.progress(0)
|
50 |
for idx, uploaded_file in enumerate(uploaded_files):
|
51 |
file_name = uploaded_file.name
|
52 |
file_content = uploaded_file.read()
|
53 |
|
54 |
+
# Set up Azure Translator API headers
|
55 |
headers = {
|
56 |
"Ocp-Apim-Subscription-Key": os.environ["AZURE_AI_TRANSLATOR_KEY"],
|
57 |
}
|
58 |
|
59 |
+
# Prepare file for translation
|
60 |
files = {
|
61 |
"document": (file_name, file_content, "ContentType/file-extension"),
|
62 |
}
|
63 |
|
64 |
+
# Construct API URL with target language and version
|
65 |
url = f"{os.environ["AZURE_AI_ENDPOINT_URL"]}/translator/document:translate?targetLanguage={lang_id}&api-version={os.environ["AZURE_AI_API_VERSION"]}"
|
66 |
+
|
67 |
+
# Send translation request to Azure
|
68 |
response = requests.post(url, headers=headers, files=files)
|
69 |
|
70 |
+
# Handle translation response
|
71 |
if response.status_code == 200:
|
72 |
+
# Add successfully translated file to zip archive
|
73 |
zip_file.writestr(f"{lang_name}-translated-{file_name}", response.content)
|
74 |
st.success(f"Successfully translated: {file_name}")
|
75 |
else:
|
76 |
st.error(f"Failed to translate {file_name} with status code {response.status_code}: {response.text}")
|
77 |
|
78 |
+
# Update progress bar based on completed translations
|
79 |
progress = (idx + 1) / len(uploaded_files)
|
80 |
progress_bar.progress(progress)
|
81 |
|
82 |
+
# Create download button for the zip file containing all translations
|
83 |
st.download_button(
|
84 |
label="Download All Translated Files",
|
85 |
data=zip_buffer.getvalue(),
|