Spaces:
Sleeping
Sleeping
fadliaulawi
commited on
Commit
·
1f48a9f
1
Parent(s):
d8661f4
Complete synchronous approach
Browse files- .gitignore +2 -1
- app.py +40 -29
.gitignore
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
-
.vscode
|
|
|
|
|
|
| 1 |
+
.vscode
|
| 2 |
+
.env
|
app.py
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
|
|
|
| 3 |
|
| 4 |
from azure.storage.blob import BlobServiceClient
|
| 5 |
from azure.core.credentials import AzureKeyCredential
|
| 6 |
from azure.ai.translation.document import DocumentTranslationClient
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
AZURE_AI_TRANSLATOR_KEY = "26JrNtz5HvXYJ7zTL1MkmuRjTkAJ4EXx2gsHp6rj8pV9VIb4Q6TTJQQJ99AKACqBBLyXJ3w3AAAbACOGFu5N"
|
| 10 |
-
AZURE_AI_ENDPOINT_URL = "https://cbd-translation.cognitiveservices.azure.com/"
|
| 11 |
-
AZURE_AI_API_VERSION = "2024-05-01"
|
| 12 |
|
| 13 |
# Streamlit UI
|
| 14 |
st.title("Azure Translation Tools")
|
| 15 |
-
|
| 16 |
|
| 17 |
# Language options
|
| 18 |
langs = (
|
|
@@ -36,36 +37,46 @@ lang = st.selectbox('Target language selection:', langs, key='lang')
|
|
| 36 |
lang_id = lang.split()[0]
|
| 37 |
lang_name = lang.split()[-1]
|
| 38 |
|
| 39 |
-
if
|
| 40 |
submit = st.button("Get Result", key='submit')
|
| 41 |
|
| 42 |
-
if
|
| 43 |
-
file_name = uploaded_file.name
|
| 44 |
-
file_content = uploaded_file.read()
|
| 45 |
|
| 46 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
}
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
# Allow user to download
|
| 66 |
st.download_button(
|
| 67 |
-
label="Download
|
| 68 |
-
data=
|
| 69 |
-
file_name=f"{lang_name}-translated-
|
| 70 |
-
mime="application/
|
| 71 |
)
|
|
|
|
| 1 |
+
import io
|
| 2 |
+
import os
|
| 3 |
import streamlit as st
|
| 4 |
import requests
|
| 5 |
+
import zipfile
|
| 6 |
|
| 7 |
from azure.storage.blob import BlobServiceClient
|
| 8 |
from azure.core.credentials import AzureKeyCredential
|
| 9 |
from azure.ai.translation.document import DocumentTranslationClient
|
| 10 |
+
from dotenv import load_dotenv
|
| 11 |
|
| 12 |
+
load_dotenv()
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# Streamlit UI
|
| 15 |
st.title("Azure Translation Tools")
|
| 16 |
+
uploaded_files = st.file_uploader("Upload files to start the process", accept_multiple_files=True)
|
| 17 |
|
| 18 |
# Language options
|
| 19 |
langs = (
|
|
|
|
| 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 |
+
# Allow user to download all files as zip
|
| 77 |
st.download_button(
|
| 78 |
+
label="Download All Translated Files",
|
| 79 |
+
data=zip_buffer.getvalue(),
|
| 80 |
+
file_name=f"{lang_name}-translated-files.zip",
|
| 81 |
+
mime="application/zip"
|
| 82 |
)
|