Spaces:
Sleeping
Sleeping
fadliaulawi
commited on
Commit
·
6ce9ced
1
Parent(s):
f0efd2a
Add sync and async function
Browse files
app.py
CHANGED
|
@@ -4,10 +4,10 @@ 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 |
|
|
@@ -15,6 +15,11 @@ 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 |
# Define available language options with their codes and names
|
| 19 |
langs = (
|
| 20 |
'id - Indonesian',
|
|
@@ -38,6 +43,53 @@ 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 |
|
|
@@ -48,33 +100,21 @@ if uploaded_files and submit:
|
|
| 48 |
# Add progress bar for translation status
|
| 49 |
progress_bar = st.progress(0)
|
| 50 |
for idx, uploaded_file in enumerate(uploaded_files):
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 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-{
|
| 74 |
-
st.success(f"Successfully translated: {
|
| 75 |
else:
|
| 76 |
-
st.error(f"Failed to translate {
|
| 77 |
-
|
| 78 |
# Update progress bar based on completed translations
|
| 79 |
progress = (idx + 1) / len(uploaded_files)
|
| 80 |
progress_bar.progress(progress)
|
|
|
|
| 4 |
import requests
|
| 5 |
import zipfile
|
| 6 |
|
|
|
|
| 7 |
from azure.core.credentials import AzureKeyCredential
|
| 8 |
from azure.ai.translation.document import DocumentTranslationClient
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
+
from utils import blob_service_client, upload_to_azure, download_from_azure, delete_from_azure
|
| 11 |
|
| 12 |
load_dotenv()
|
| 13 |
|
|
|
|
| 15 |
st.title("Azure Translation Tools")
|
| 16 |
uploaded_files = st.file_uploader("Upload files to start the process", accept_multiple_files=True)
|
| 17 |
|
| 18 |
+
# Initialize a new instance of the DocumentTranslationClient
|
| 19 |
+
client = DocumentTranslationClient(os.environ["AZURE_AI_ENDPOINT_URL"], AzureKeyCredential(os.environ["AZURE_AI_TRANSLATOR_KEY"]))
|
| 20 |
+
sourceUri = "https://cbdtranslation.blob.core.windows.net/source"
|
| 21 |
+
targetUri = "https://cbdtranslation.blob.core.windows.net/target"
|
| 22 |
+
|
| 23 |
# Define available language options with their codes and names
|
| 24 |
langs = (
|
| 25 |
'id - Indonesian',
|
|
|
|
| 43 |
lang_id = lang.split()[0] # Get language code (e.g., 'en')
|
| 44 |
lang_name = lang.split()[-1] # Get language name (e.g., 'English')
|
| 45 |
|
| 46 |
+
def process_sync(uploaded_file):
|
| 47 |
+
|
| 48 |
+
file_name = uploaded_file.name
|
| 49 |
+
file_content = uploaded_file.read()
|
| 50 |
+
|
| 51 |
+
# Set up Azure Translator API headers
|
| 52 |
+
headers = {
|
| 53 |
+
"Ocp-Apim-Subscription-Key": os.environ["AZURE_AI_TRANSLATOR_KEY"],
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
# Prepare file for translation
|
| 57 |
+
files = {
|
| 58 |
+
"document": (file_name, file_content, "ContentType/file-extension"),
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
# Construct API URL with target language and version
|
| 62 |
+
url = f"{os.environ["AZURE_AI_ENDPOINT_URL"]}/translator/document:translate?targetLanguage={lang_id}&api-version={os.environ["AZURE_AI_API_VERSION"]}"
|
| 63 |
+
|
| 64 |
+
# Send translation request to Azure
|
| 65 |
+
response = requests.post(url, headers=headers, files=files)
|
| 66 |
+
|
| 67 |
+
return response.status_code == 200, response.content
|
| 68 |
+
|
| 69 |
+
def process_async(uploaded_file):
|
| 70 |
+
|
| 71 |
+
file_name = uploaded_file.name
|
| 72 |
+
file_content = uploaded_file.read()
|
| 73 |
+
|
| 74 |
+
# Upload the original file to Azure Blob Storage source container
|
| 75 |
+
upload_to_azure(blob_service_client, "source", file_content, file_name)
|
| 76 |
+
|
| 77 |
+
# Initialize translation job using the DocumentTranslationClient
|
| 78 |
+
# Wait for the translation to complete and get the result
|
| 79 |
+
poller = client.begin_translation(sourceUri, targetUri, lang_id)
|
| 80 |
+
result = poller.result()
|
| 81 |
+
|
| 82 |
+
# Download the translated file from Azure Blob Storage target container
|
| 83 |
+
downloaded_file_content = download_from_azure(blob_service_client, "target", file_name)
|
| 84 |
+
|
| 85 |
+
# Clean up: Remove files from both source and target containers
|
| 86 |
+
delete_from_azure(blob_service_client, "source", file_name)
|
| 87 |
+
delete_from_azure(blob_service_client, "target", file_name)
|
| 88 |
+
|
| 89 |
+
# Return translation status and the translated content
|
| 90 |
+
for document in result:
|
| 91 |
+
return document.status == 'Succeeded', downloaded_file_content
|
| 92 |
+
|
| 93 |
if uploaded_files:
|
| 94 |
submit = st.button("Get Result", key='submit')
|
| 95 |
|
|
|
|
| 100 |
# Add progress bar for translation status
|
| 101 |
progress_bar = st.progress(0)
|
| 102 |
for idx, uploaded_file in enumerate(uploaded_files):
|
| 103 |
+
|
| 104 |
+
# Check file extension to determine translation method
|
| 105 |
+
if uploaded_file.name.split('.')[-1] in ['txt', 'tsv', 'tab', 'csv', 'html', 'htm', 'mthml', 'mht', 'pptx', 'xlsx', 'docx', 'msg', 'xlf', 'xliff']:
|
| 106 |
+
result, response = process_sync(uploaded_file)
|
| 107 |
+
elif uploaded_file.name.split('.')[-1] in ['pdf', 'odt', 'odp', 'ods', 'rtf']:
|
| 108 |
+
result, response = process_async(uploaded_file)
|
| 109 |
+
|
| 110 |
+
# Check if translation was successful
|
| 111 |
+
if result:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
# Add successfully translated file to zip archive
|
| 113 |
+
zip_file.writestr(f"{lang_name}-translated-{uploaded_file.name}", response)
|
| 114 |
+
st.success(f"Successfully translated: {uploaded_file.name}")
|
| 115 |
else:
|
| 116 |
+
st.error(f"Failed to translate {uploaded_file.name} with status code {response.status_code}: {response.text}")
|
| 117 |
+
|
| 118 |
# Update progress bar based on completed translations
|
| 119 |
progress = (idx + 1) / len(uploaded_files)
|
| 120 |
progress_bar.progress(progress)
|
utils.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
from azure.storage.blob import BlobServiceClient
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
load_dotenv()
|
| 7 |
+
|
| 8 |
+
# Initialize Azure Blob Service Clients
|
| 9 |
+
blob_service_client = BlobServiceClient.from_connection_string(os.environ["AZURE_STORAGE_CONNECTION_STRING"])
|
| 10 |
+
|
| 11 |
+
# Function to upload file to Azure Storage
|
| 12 |
+
def upload_to_azure(blob_service_client, container_name, file, file_name):
|
| 13 |
+
container_client = blob_service_client.get_container_client(container_name)
|
| 14 |
+
container_client.upload_blob(name=file_name, data=file, overwrite=True)
|
| 15 |
+
|
| 16 |
+
# Function to download file from Azure Storage
|
| 17 |
+
def download_from_azure(blob_service_client, container_name, file_name):
|
| 18 |
+
container_client = blob_service_client.get_container_client(container_name)
|
| 19 |
+
blob_client = container_client.get_blob_client(blob=file_name)
|
| 20 |
+
file_content = blob_client.download_blob().readall()
|
| 21 |
+
return file_content
|
| 22 |
+
|
| 23 |
+
# Function to delete file from Azure Storage
|
| 24 |
+
def delete_from_azure(blob_service_client, container_name, file_name):
|
| 25 |
+
container_client = blob_service_client.get_container_client(container_name)
|
| 26 |
+
blob_client = container_client.get_blob_client(blob=file_name)
|
| 27 |
+
blob_client.delete_blob()
|