fadliaulawi's picture
Add initial code
d8661f4
raw
history blame
2.17 kB
import streamlit as st
import requests
from azure.storage.blob import BlobServiceClient
from azure.core.credentials import AzureKeyCredential
from azure.ai.translation.document import DocumentTranslationClient
# Translator AI
AZURE_AI_TRANSLATOR_KEY = "26JrNtz5HvXYJ7zTL1MkmuRjTkAJ4EXx2gsHp6rj8pV9VIb4Q6TTJQQJ99AKACqBBLyXJ3w3AAAbACOGFu5N"
AZURE_AI_ENDPOINT_URL = "https://cbd-translation.cognitiveservices.azure.com/"
AZURE_AI_API_VERSION = "2024-05-01"
# Streamlit UI
st.title("Azure Translation Tools")
uploaded_file = st.file_uploader("Upload a file to start the process")
# Language options
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()
#TODO: filter which type of files that need batch translation, otherwise just use synchronous translation directly
# Define headers and files
headers = {
"Ocp-Apim-Subscription-Key": AZURE_AI_TRANSLATOR_KEY,
}
files = {
"document": (file_name, file_content, "ContentType/file-extension"),
}
# Make the POST request
url = f"{AZURE_AI_ENDPOINT_URL}/translator/document:translate?targetLanguage={lang_id}&api-version={AZURE_AI_API_VERSION}"
response = requests.post(url, headers=headers, files=files)
if response.status_code == 200:
st.success("Translation completed successfully.")
else:
st.error(f"Failed with status code {response.status_code}: {response.text}")
# Allow user to download the file
st.download_button(
label="Download the Processed File",
data=response.content,
file_name=f"{lang_name}-translated-{file_name}",
mime="application/octet-stream"
)