|
import streamlit as st |
|
|
|
from azure.storage.blob import BlobServiceClient |
|
from azure.core.credentials import AzureKeyCredential |
|
from azure.ai.translation.document import DocumentTranslationClient |
|
|
|
|
|
STORAGE_CONNECTION_STRING = 'DefaultEndpointsProtocol=https;AccountName=cbdtranslation;AccountKey=V4CBDnn6UH8PlybYGAbLUxejErCnDdxJWWcbMeM+HJB8S/zYDof/8EWAtG6latsG12fR2Q9NVf4l+ASt4ohA4g==;EndpointSuffix=core.windows.net' |
|
|
|
|
|
FIRST_CONTAINER_NAME = "source" |
|
SECOND_CONTAINER_NAME = "target" |
|
|
|
|
|
AZURE_AI_TRANSLATOR_KEY = "26JrNtz5HvXYJ7zTL1MkmuRjTkAJ4EXx2gsHp6rj8pV9VIb4Q6TTJQQJ99AKACqBBLyXJ3w3AAAbACOGFu5N" |
|
AZURE_AI_ENDPOINT_URL = "https://cbd-translation.cognitiveservices.azure.com/" |
|
|
|
|
|
blob_service_client = BlobServiceClient.from_connection_string(STORAGE_CONNECTION_STRING) |
|
|
|
|
|
def upload_to_azure(blob_service_client, container_name, file, file_name): |
|
container_client = blob_service_client.get_container_client(container_name) |
|
container_client.upload_blob(name=file_name, data=file, overwrite=True) |
|
|
|
|
|
def download_from_azure(blob_service_client, container_name, file_name): |
|
container_client = blob_service_client.get_container_client(container_name) |
|
blob_client = container_client.get_blob_client(blob=file_name) |
|
file_content = blob_client.download_blob().readall() |
|
return file_content |
|
|
|
|
|
def delete_from_azure(blob_service_client, container_name, file_name): |
|
container_client = blob_service_client.get_container_client(container_name) |
|
blob_client = container_client.get_blob_client(blob=file_name) |
|
blob_client.delete_blob() |
|
|
|
key = AZURE_AI_TRANSLATOR_KEY |
|
endpoint = AZURE_AI_ENDPOINT_URL |
|
|
|
sourceUri = "https://cbdtranslation.blob.core.windows.net/source" |
|
targetUri = "https://cbdtranslation.blob.core.windows.net/target" |
|
|
|
|
|
client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) |
|
|
|
def translate(lang_id, lang_name): |
|
poller = client.begin_translation(sourceUri, targetUri, lang_id) |
|
result = poller.result() |
|
st.write( |
|
'Total documents: {}'.format( |
|
poller.details.documents_total_count |
|
) |
|
) |
|
|
|
for document in result: |
|
if document.status == 'Succeeded': |
|
st.success('Translated to language: {}\n'.format(lang_name)) |
|
else: |
|
st.error( |
|
'Error Code: {}, Message: {}\n'.format( |
|
document.error.code, document.error.message |
|
) |
|
) |
|
|
|
|
|
st.title("Azure Translation Tools") |
|
|
|
|
|
uploaded_file = st.file_uploader("Upload a file to start the process") |
|
|
|
langs = ( |
|
'id - Indonesian', |
|
'en - English', |
|
'es - Spanish', |
|
'zh - Chinese', |
|
'ar - Arabic', |
|
'fr - French', |
|
'ru - Russian', |
|
'hi - Hindi', |
|
'pt - Portuguese', |
|
'de - German', |
|
'ms - Malay', |
|
'ta - Tamil', |
|
'ko - Korean', |
|
'th - Thai', |
|
) |
|
|
|
lang = st.selectbox('Target language selection:', langs, key='lang') |
|
lang_id = lang.split()[0] |
|
lang_name = lang.split()[-1] |
|
|
|
if uploaded_file: |
|
submit = st.button("Get Result", key='submit') |
|
|
|
if uploaded_file and submit: |
|
file_name = uploaded_file.name |
|
file_content = uploaded_file.read() |
|
|
|
upload_to_azure(blob_service_client, FIRST_CONTAINER_NAME, file_content, file_name) |
|
|
|
st.info("Performing translations on the file...") |
|
translate(lang_id, lang_name) |
|
|
|
|
|
downloaded_file_content = download_from_azure(blob_service_client, SECOND_CONTAINER_NAME, file_name) |
|
|
|
|
|
delete_from_azure(blob_service_client, FIRST_CONTAINER_NAME, file_name) |
|
delete_from_azure(blob_service_client, SECOND_CONTAINER_NAME, file_name) |
|
|
|
|
|
st.download_button( |
|
label="Download the Processed File", |
|
data=downloaded_file_content, |
|
file_name=f"{lang_name}-translated-{file_name}", |
|
mime="application/octet-stream" |
|
) |
|
|