Spaces:
Sleeping
Sleeping
fadliaulawi
commited on
Commit
·
4caa134
1
Parent(s):
4cb174a
Remove streamlit auth
Browse files- app.py +150 -168
- requirements.txt +0 -3
app.py
CHANGED
|
@@ -10,181 +10,163 @@ from azure.ai.translation.document import DocumentTranslationClient
|
|
| 10 |
from dotenv import load_dotenv
|
| 11 |
from streamlit_pdf_viewer import pdf_viewer
|
| 12 |
from utils import blob_service_client, upload_to_azure, download_from_azure, delete_from_azure
|
| 13 |
-
from streamlit_msal import Msal
|
| 14 |
-
|
| 15 |
from auth_middleware import app as auth_app
|
| 16 |
|
| 17 |
load_dotenv()
|
| 18 |
st.set_page_config(layout="wide")
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
)
|
| 62 |
|
| 63 |
-
# Get user's language selection and extract language code and name
|
| 64 |
-
lang = st.selectbox('Target language selection:', langs, key='lang')
|
| 65 |
-
lang_id = lang.split()[0] # Get language code (e.g., 'en')
|
| 66 |
-
lang_name = lang.split()[-1] # Get language name (e.g., 'English')
|
| 67 |
-
|
| 68 |
-
def process_sync(file_name, file_content):
|
| 69 |
-
|
| 70 |
-
# Set up Azure Translator API headers
|
| 71 |
-
headers = {
|
| 72 |
-
"Ocp-Apim-Subscription-Key": os.environ["AZURE_AI_TRANSLATOR_KEY"],
|
| 73 |
-
}
|
| 74 |
-
|
| 75 |
-
# Prepare file for translation
|
| 76 |
-
files = {
|
| 77 |
-
"document": (file_name, file_content, "ContentType/file-extension"),
|
| 78 |
-
}
|
| 79 |
-
|
| 80 |
-
# Construct API URL with target language and version
|
| 81 |
-
url = f"{os.environ['AZURE_AI_ENDPOINT_URL']}/translator/document:translate?targetLanguage={lang_id}&api-version={os.environ['AZURE_AI_API_VERSION']}"
|
| 82 |
-
|
| 83 |
-
# Send translation request to Azure
|
| 84 |
-
response = requests.post(url, headers=headers, files=files)
|
| 85 |
-
|
| 86 |
-
return response.status_code == 200, response.content
|
| 87 |
-
|
| 88 |
-
def process_async(file_name, file_content):
|
| 89 |
-
|
| 90 |
-
# Upload the original file to Azure Blob Storage source container
|
| 91 |
-
upload_to_azure(blob_service_client, "source", file_content, file_name)
|
| 92 |
-
|
| 93 |
-
# Initialize translation job using the DocumentTranslationClient
|
| 94 |
-
# Wait for the translation to complete and get the result
|
| 95 |
-
poller = client.begin_translation(sourceUri, targetUri, lang_id)
|
| 96 |
-
result = poller.result()
|
| 97 |
-
|
| 98 |
-
# Download the translated file from Azure Blob Storage target container
|
| 99 |
-
downloaded_file_content = download_from_azure(blob_service_client, "target", file_name)
|
| 100 |
-
|
| 101 |
-
# Clean up: Remove files from both source and target containers
|
| 102 |
-
delete_from_azure(blob_service_client, "source", file_name)
|
| 103 |
-
delete_from_azure(blob_service_client, "target", file_name)
|
| 104 |
-
|
| 105 |
-
# Return translation status and the translated content
|
| 106 |
-
for document in result:
|
| 107 |
-
return document.status == 'Succeeded', downloaded_file_content
|
| 108 |
-
|
| 109 |
-
if uploaded_files:
|
| 110 |
-
submit = st.button("Get Result", key='submit')
|
| 111 |
-
|
| 112 |
-
if uploaded_files and submit:
|
| 113 |
-
# Create an in-memory zip file to store translated documents
|
| 114 |
-
zip_buffer = io.BytesIO()
|
| 115 |
-
with zipfile.ZipFile(zip_buffer, 'w') as zip_file:
|
| 116 |
-
# Add progress bar for translation status
|
| 117 |
-
progress_bar = st.progress(0)
|
| 118 |
-
for idx, uploaded_file in enumerate(uploaded_files):
|
| 119 |
-
# Start timing
|
| 120 |
-
start_time = time.time()
|
| 121 |
-
|
| 122 |
-
file_name = uploaded_file.name
|
| 123 |
-
file_content = uploaded_file.read()
|
| 124 |
-
file_type = file_name.split('.')[-1]
|
| 125 |
-
|
| 126 |
-
# Check file extension to determine translation method
|
| 127 |
-
if file_type in ['txt', 'tsv', 'tab', 'csv', 'html', 'htm', 'mthml', 'mht', 'pptx', 'xlsx', 'docx', 'msg', 'xlf', 'xliff']:
|
| 128 |
-
result, response = process_sync(file_name, file_content)
|
| 129 |
-
elif file_type in ['pdf', 'odt', 'odp', 'ods', 'rtf']:
|
| 130 |
-
result, response = process_async(file_name, file_content)
|
| 131 |
-
|
| 132 |
-
# Calculate duration
|
| 133 |
-
duration = time.time() - start_time
|
| 134 |
-
|
| 135 |
-
# Check if translation was successful
|
| 136 |
-
if result:
|
| 137 |
-
# Add successfully translated file to zip archive
|
| 138 |
-
zip_file.writestr(f"{lang_name}-translated-{uploaded_file.name}", response)
|
| 139 |
-
st.success(f"Successfully translated: {uploaded_file.name} (Time taken: {duration:.2f} seconds)")
|
| 140 |
-
else:
|
| 141 |
-
st.error(f"Failed to translate {uploaded_file.name} with status code {response.status_code}: {response.text} (Time taken: {duration:.2f} seconds)")
|
| 142 |
-
|
| 143 |
-
if file_type == 'pdf':
|
| 144 |
-
# Display the original and translated files side by side
|
| 145 |
-
col1, col2 = st.columns(2)
|
| 146 |
-
with col1:
|
| 147 |
-
st.write(f"Original File: {uploaded_file.name}")
|
| 148 |
-
st.divider()
|
| 149 |
-
pdf_viewer(file_content)
|
| 150 |
-
with col2:
|
| 151 |
-
st.write(f"Translated File: {lang_name}-translated-{uploaded_file.name}")
|
| 152 |
-
st.divider()
|
| 153 |
-
pdf_viewer(response)
|
| 154 |
-
elif file_type == 'docx':
|
| 155 |
-
col1, col2 = st.columns(2)
|
| 156 |
-
with col1:
|
| 157 |
-
st.write(f"Original File: {uploaded_file.name}")
|
| 158 |
-
st.divider()
|
| 159 |
-
st.write("On development")
|
| 160 |
-
with col2:
|
| 161 |
-
st.write(f"Translated File: {lang_name}-translated-{uploaded_file.name}")
|
| 162 |
-
st.divider()
|
| 163 |
-
st.write("On development")
|
| 164 |
-
elif file_type == 'txt':
|
| 165 |
-
# Display the original and translated files side by side
|
| 166 |
-
col1, col2 = st.columns(2)
|
| 167 |
-
with col1:
|
| 168 |
-
st.write(f"Original File: {uploaded_file.name}")
|
| 169 |
-
st.divider()
|
| 170 |
-
st.write(file_content)
|
| 171 |
-
with col2:
|
| 172 |
-
st.write(f"Translated File: {lang_name}-translated-{uploaded_file.name}")
|
| 173 |
-
st.divider()
|
| 174 |
-
st.write(response)
|
| 175 |
-
|
| 176 |
-
# Update progress bar based on completed translations
|
| 177 |
-
progress = (idx + 1) / len(uploaded_files)
|
| 178 |
-
progress_bar.progress(progress)
|
| 179 |
-
|
| 180 |
-
# Create download button for the zip file containing all translations
|
| 181 |
-
st.download_button(
|
| 182 |
-
label="Download All Translated Files",
|
| 183 |
-
data=zip_buffer.getvalue(),
|
| 184 |
-
file_name=f"{lang_name}-translated-files.zip",
|
| 185 |
-
mime="application/zip"
|
| 186 |
-
)
|
| 187 |
-
|
| 188 |
# Proxy Streamlit through Flask
|
| 189 |
def run():
|
| 190 |
os.system("streamlit run streamlit_app.py --server.port=8501 --server.address=0.0.0.0")
|
|
|
|
| 10 |
from dotenv import load_dotenv
|
| 11 |
from streamlit_pdf_viewer import pdf_viewer
|
| 12 |
from utils import blob_service_client, upload_to_azure, download_from_azure, delete_from_azure
|
|
|
|
|
|
|
| 13 |
from auth_middleware import app as auth_app
|
| 14 |
|
| 15 |
load_dotenv()
|
| 16 |
st.set_page_config(layout="wide")
|
| 17 |
|
| 18 |
+
# Streamlit UI
|
| 19 |
+
st.title("Azure Translation Tools")
|
| 20 |
+
uploaded_files = st.file_uploader("Upload files to start the process", accept_multiple_files=True)
|
| 21 |
+
|
| 22 |
+
# Initialize a new instance of the DocumentTranslationClient
|
| 23 |
+
client = DocumentTranslationClient(os.environ["AZURE_AI_ENDPOINT_URL"], AzureKeyCredential(os.environ["AZURE_AI_TRANSLATOR_KEY"]))
|
| 24 |
+
sourceUri = "https://cbdtranslation.blob.core.windows.net/source"
|
| 25 |
+
targetUri = "https://cbdtranslation.blob.core.windows.net/target"
|
| 26 |
+
|
| 27 |
+
# Define available language options with their codes and names
|
| 28 |
+
langs = (
|
| 29 |
+
'id - Indonesian',
|
| 30 |
+
'en - English',
|
| 31 |
+
'es - Spanish',
|
| 32 |
+
'zh - Chinese',
|
| 33 |
+
'ar - Arabic',
|
| 34 |
+
'fr - French',
|
| 35 |
+
'ru - Russian',
|
| 36 |
+
'hi - Hindi',
|
| 37 |
+
'pt - Portuguese',
|
| 38 |
+
'de - German',
|
| 39 |
+
'ms - Malay',
|
| 40 |
+
'ta - Tamil',
|
| 41 |
+
'ko - Korean',
|
| 42 |
+
'th - Thai',
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Get user's language selection and extract language code and name
|
| 46 |
+
lang = st.selectbox('Target language selection:', langs, key='lang')
|
| 47 |
+
lang_id = lang.split()[0] # Get language code (e.g., 'en')
|
| 48 |
+
lang_name = lang.split()[-1] # Get language name (e.g., 'English')
|
| 49 |
+
|
| 50 |
+
def process_sync(file_name, file_content):
|
| 51 |
+
|
| 52 |
+
# Set up Azure Translator API headers
|
| 53 |
+
headers = {
|
| 54 |
+
"Ocp-Apim-Subscription-Key": os.environ["AZURE_AI_TRANSLATOR_KEY"],
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
# Prepare file for translation
|
| 58 |
+
files = {
|
| 59 |
+
"document": (file_name, file_content, "ContentType/file-extension"),
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
# Construct API URL with target language and version
|
| 63 |
+
url = f"{os.environ['AZURE_AI_ENDPOINT_URL']}/translator/document:translate?targetLanguage={lang_id}&api-version={os.environ['AZURE_AI_API_VERSION']}"
|
| 64 |
+
|
| 65 |
+
# Send translation request to Azure
|
| 66 |
+
response = requests.post(url, headers=headers, files=files)
|
| 67 |
+
|
| 68 |
+
return response.status_code == 200, response.content
|
| 69 |
+
|
| 70 |
+
def process_async(file_name, file_content):
|
| 71 |
+
|
| 72 |
+
# Upload the original file to Azure Blob Storage source container
|
| 73 |
+
upload_to_azure(blob_service_client, "source", file_content, file_name)
|
| 74 |
+
|
| 75 |
+
# Initialize translation job using the DocumentTranslationClient
|
| 76 |
+
# Wait for the translation to complete and get the result
|
| 77 |
+
poller = client.begin_translation(sourceUri, targetUri, lang_id)
|
| 78 |
+
result = poller.result()
|
| 79 |
+
|
| 80 |
+
# Download the translated file from Azure Blob Storage target container
|
| 81 |
+
downloaded_file_content = download_from_azure(blob_service_client, "target", file_name)
|
| 82 |
+
|
| 83 |
+
# Clean up: Remove files from both source and target containers
|
| 84 |
+
delete_from_azure(blob_service_client, "source", file_name)
|
| 85 |
+
delete_from_azure(blob_service_client, "target", file_name)
|
| 86 |
+
|
| 87 |
+
# Return translation status and the translated content
|
| 88 |
+
for document in result:
|
| 89 |
+
return document.status == 'Succeeded', downloaded_file_content
|
| 90 |
+
|
| 91 |
+
if uploaded_files:
|
| 92 |
+
submit = st.button("Get Result", key='submit')
|
| 93 |
+
|
| 94 |
+
if uploaded_files and submit:
|
| 95 |
+
# Create an in-memory zip file to store translated documents
|
| 96 |
+
zip_buffer = io.BytesIO()
|
| 97 |
+
with zipfile.ZipFile(zip_buffer, 'w') as zip_file:
|
| 98 |
+
# Add progress bar for translation status
|
| 99 |
+
progress_bar = st.progress(0)
|
| 100 |
+
for idx, uploaded_file in enumerate(uploaded_files):
|
| 101 |
+
# Start timing
|
| 102 |
+
start_time = time.time()
|
| 103 |
+
|
| 104 |
+
file_name = uploaded_file.name
|
| 105 |
+
file_content = uploaded_file.read()
|
| 106 |
+
file_type = file_name.split('.')[-1]
|
| 107 |
+
|
| 108 |
+
# Check file extension to determine translation method
|
| 109 |
+
if file_type in ['txt', 'tsv', 'tab', 'csv', 'html', 'htm', 'mthml', 'mht', 'pptx', 'xlsx', 'docx', 'msg', 'xlf', 'xliff']:
|
| 110 |
+
result, response = process_sync(file_name, file_content)
|
| 111 |
+
elif file_type in ['pdf', 'odt', 'odp', 'ods', 'rtf']:
|
| 112 |
+
result, response = process_async(file_name, file_content)
|
| 113 |
+
|
| 114 |
+
# Calculate duration
|
| 115 |
+
duration = time.time() - start_time
|
| 116 |
+
|
| 117 |
+
# Check if translation was successful
|
| 118 |
+
if result:
|
| 119 |
+
# Add successfully translated file to zip archive
|
| 120 |
+
zip_file.writestr(f"{lang_name}-translated-{uploaded_file.name}", response)
|
| 121 |
+
st.success(f"Successfully translated: {uploaded_file.name} (Time taken: {duration:.2f} seconds)")
|
| 122 |
+
else:
|
| 123 |
+
st.error(f"Failed to translate {uploaded_file.name} with status code {response.status_code}: {response.text} (Time taken: {duration:.2f} seconds)")
|
| 124 |
+
|
| 125 |
+
if file_type == 'pdf':
|
| 126 |
+
# Display the original and translated files side by side
|
| 127 |
+
col1, col2 = st.columns(2)
|
| 128 |
+
with col1:
|
| 129 |
+
st.write(f"Original File: {uploaded_file.name}")
|
| 130 |
+
st.divider()
|
| 131 |
+
pdf_viewer(file_content)
|
| 132 |
+
with col2:
|
| 133 |
+
st.write(f"Translated File: {lang_name}-translated-{uploaded_file.name}")
|
| 134 |
+
st.divider()
|
| 135 |
+
pdf_viewer(response)
|
| 136 |
+
elif file_type == 'docx':
|
| 137 |
+
col1, col2 = st.columns(2)
|
| 138 |
+
with col1:
|
| 139 |
+
st.write(f"Original File: {uploaded_file.name}")
|
| 140 |
+
st.divider()
|
| 141 |
+
st.write("On development")
|
| 142 |
+
with col2:
|
| 143 |
+
st.write(f"Translated File: {lang_name}-translated-{uploaded_file.name}")
|
| 144 |
+
st.divider()
|
| 145 |
+
st.write("On development")
|
| 146 |
+
elif file_type == 'txt':
|
| 147 |
+
# Display the original and translated files side by side
|
| 148 |
+
col1, col2 = st.columns(2)
|
| 149 |
+
with col1:
|
| 150 |
+
st.write(f"Original File: {uploaded_file.name}")
|
| 151 |
+
st.divider()
|
| 152 |
+
st.write(file_content)
|
| 153 |
+
with col2:
|
| 154 |
+
st.write(f"Translated File: {lang_name}-translated-{uploaded_file.name}")
|
| 155 |
+
st.divider()
|
| 156 |
+
st.write(response)
|
| 157 |
+
|
| 158 |
+
# Update progress bar based on completed translations
|
| 159 |
+
progress = (idx + 1) / len(uploaded_files)
|
| 160 |
+
progress_bar.progress(progress)
|
| 161 |
+
|
| 162 |
+
# Create download button for the zip file containing all translations
|
| 163 |
+
st.download_button(
|
| 164 |
+
label="Download All Translated Files",
|
| 165 |
+
data=zip_buffer.getvalue(),
|
| 166 |
+
file_name=f"{lang_name}-translated-files.zip",
|
| 167 |
+
mime="application/zip"
|
| 168 |
)
|
| 169 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
# Proxy Streamlit through Flask
|
| 171 |
def run():
|
| 172 |
os.system("streamlit run streamlit_app.py --server.port=8501 --server.address=0.0.0.0")
|
requirements.txt
CHANGED
|
@@ -63,9 +63,6 @@ rsa==4.9
|
|
| 63 |
six==1.17.0
|
| 64 |
smmap==5.0.1
|
| 65 |
streamlit==1.40.2
|
| 66 |
-
streamlit-auth0==1.0.5
|
| 67 |
-
streamlit-auth0-component==0.1.5
|
| 68 |
-
streamlit-msal==0.2.0
|
| 69 |
streamlit-pdf-viewer==0.0.19
|
| 70 |
tenacity==9.0.0
|
| 71 |
toml==0.10.2
|
|
|
|
| 63 |
six==1.17.0
|
| 64 |
smmap==5.0.1
|
| 65 |
streamlit==1.40.2
|
|
|
|
|
|
|
|
|
|
| 66 |
streamlit-pdf-viewer==0.0.19
|
| 67 |
tenacity==9.0.0
|
| 68 |
toml==0.10.2
|