fadliaulawi commited on
Commit
d8661f4
·
1 Parent(s): c75efd8

Add initial code

Browse files
Files changed (3) hide show
  1. .gitignore +1 -0
  2. app.py +71 -0
  3. old-app.py +119 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .vscode
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Translator AI
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
+ uploaded_file = st.file_uploader("Upload a file to start the process")
16
+
17
+ # Language options
18
+ langs = (
19
+ 'id - Indonesian',
20
+ 'en - English',
21
+ 'es - Spanish',
22
+ 'zh - Chinese',
23
+ 'ar - Arabic',
24
+ 'fr - French',
25
+ 'ru - Russian',
26
+ 'hi - Hindi',
27
+ 'pt - Portuguese',
28
+ 'de - German',
29
+ 'ms - Malay',
30
+ 'ta - Tamil',
31
+ 'ko - Korean',
32
+ 'th - Thai',
33
+ )
34
+
35
+ lang = st.selectbox('Target language selection:', langs, key='lang')
36
+ lang_id = lang.split()[0]
37
+ lang_name = lang.split()[-1]
38
+
39
+ if uploaded_file:
40
+ submit = st.button("Get Result", key='submit')
41
+
42
+ if uploaded_file and submit:
43
+ file_name = uploaded_file.name
44
+ file_content = uploaded_file.read()
45
+
46
+ #TODO: filter which type of files that need batch translation, otherwise just use synchronous translation directly
47
+
48
+ # Define headers and files
49
+ headers = {
50
+ "Ocp-Apim-Subscription-Key": AZURE_AI_TRANSLATOR_KEY,
51
+ }
52
+
53
+ files = {
54
+ "document": (file_name, file_content, "ContentType/file-extension"),
55
+ }
56
+
57
+ # Make the POST request
58
+ url = f"{AZURE_AI_ENDPOINT_URL}/translator/document:translate?targetLanguage={lang_id}&api-version={AZURE_AI_API_VERSION}"
59
+ response = requests.post(url, headers=headers, files=files)
60
+ if response.status_code == 200:
61
+ st.success("Translation completed successfully.")
62
+ else:
63
+ st.error(f"Failed with status code {response.status_code}: {response.text}")
64
+
65
+ # Allow user to download the file
66
+ st.download_button(
67
+ label="Download the Processed File",
68
+ data=response.content,
69
+ file_name=f"{lang_name}-translated-{file_name}",
70
+ mime="application/octet-stream"
71
+ )
old-app.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ from azure.storage.blob import BlobServiceClient
4
+ from azure.core.credentials import AzureKeyCredential
5
+ from azure.ai.translation.document import DocumentTranslationClient
6
+
7
+ # Azure Blob Storage connection strings
8
+ STORAGE_CONNECTION_STRING = 'DefaultEndpointsProtocol=https;AccountName=cbdtranslation;AccountKey=V4CBDnn6UH8PlybYGAbLUxejErCnDdxJWWcbMeM+HJB8S/zYDof/8EWAtG6latsG12fR2Q9NVf4l+ASt4ohA4g==;EndpointSuffix=core.windows.net'
9
+
10
+ # Container names
11
+ FIRST_CONTAINER_NAME = "source"
12
+ SECOND_CONTAINER_NAME = "target"
13
+
14
+ # Translator AI
15
+ AZURE_AI_TRANSLATOR_KEY = "26JrNtz5HvXYJ7zTL1MkmuRjTkAJ4EXx2gsHp6rj8pV9VIb4Q6TTJQQJ99AKACqBBLyXJ3w3AAAbACOGFu5N"
16
+ AZURE_AI_ENDPOINT_URL = "https://cbd-translation.cognitiveservices.azure.com/"
17
+
18
+ # Initialize Azure Blob Service Clients
19
+ blob_service_client = BlobServiceClient.from_connection_string(STORAGE_CONNECTION_STRING)
20
+
21
+ # Function to upload file to Azure Storage
22
+ def upload_to_azure(blob_service_client, container_name, file, file_name):
23
+ container_client = blob_service_client.get_container_client(container_name)
24
+ container_client.upload_blob(name=file_name, data=file, overwrite=True)
25
+
26
+ # Function to download file from Azure Storage
27
+ def download_from_azure(blob_service_client, container_name, file_name):
28
+ container_client = blob_service_client.get_container_client(container_name)
29
+ blob_client = container_client.get_blob_client(blob=file_name)
30
+ file_content = blob_client.download_blob().readall()
31
+ return file_content
32
+
33
+ # Function to delete file from Azure Storage
34
+ def delete_from_azure(blob_service_client, container_name, file_name):
35
+ container_client = blob_service_client.get_container_client(container_name)
36
+ blob_client = container_client.get_blob_client(blob=file_name)
37
+ blob_client.delete_blob()
38
+
39
+ key = AZURE_AI_TRANSLATOR_KEY
40
+ endpoint = AZURE_AI_ENDPOINT_URL
41
+
42
+ sourceUri = "https://cbdtranslation.blob.core.windows.net/source"
43
+ targetUri = "https://cbdtranslation.blob.core.windows.net/target"
44
+
45
+ # Initialize a new instance of the DocumentTranslationClient
46
+ client = DocumentTranslationClient(endpoint, AzureKeyCredential(key))
47
+
48
+ def translate(lang_id, lang_name):
49
+ poller = client.begin_translation(sourceUri, targetUri, lang_id)
50
+ result = poller.result()
51
+ st.write(
52
+ 'Total documents: {}'.format(
53
+ poller.details.documents_total_count
54
+ )
55
+ )
56
+
57
+ for document in result:
58
+ if document.status == 'Succeeded':
59
+ st.success('Translated to language: {}\n'.format(lang_name))
60
+ else:
61
+ st.error(
62
+ 'Error Code: {}, Message: {}\n'.format(
63
+ document.error.code, document.error.message
64
+ )
65
+ )
66
+
67
+ # Streamlit UI
68
+ st.title("Azure Translation Tools")
69
+
70
+ # Step 1: Upload File
71
+ uploaded_file = st.file_uploader("Upload a file to start the process")
72
+
73
+ langs = (
74
+ 'id - Indonesian',
75
+ 'en - English',
76
+ 'es - Spanish',
77
+ 'zh - Chinese',
78
+ 'ar - Arabic',
79
+ 'fr - French',
80
+ 'ru - Russian',
81
+ 'hi - Hindi',
82
+ 'pt - Portuguese',
83
+ 'de - German',
84
+ 'ms - Malay',
85
+ 'ta - Tamil',
86
+ 'ko - Korean',
87
+ 'th - Thai',
88
+ )
89
+
90
+ lang = st.selectbox('Target language selection:', langs, key='lang')
91
+ lang_id = lang.split()[0]
92
+ lang_name = lang.split()[-1]
93
+
94
+ if uploaded_file:
95
+ submit = st.button("Get Result", key='submit')
96
+
97
+ if uploaded_file and submit:
98
+ file_name = uploaded_file.name
99
+ file_content = uploaded_file.read()
100
+
101
+ upload_to_azure(blob_service_client, FIRST_CONTAINER_NAME, file_content, file_name)
102
+
103
+ st.info("Performing translations on the file...")
104
+ translate(lang_id, lang_name)
105
+
106
+ # Upload file to second storage for simulation
107
+ downloaded_file_content = download_from_azure(blob_service_client, SECOND_CONTAINER_NAME, file_name)
108
+
109
+ # Step 5: Delete both files
110
+ delete_from_azure(blob_service_client, FIRST_CONTAINER_NAME, file_name)
111
+ delete_from_azure(blob_service_client, SECOND_CONTAINER_NAME, file_name)
112
+
113
+ # Allow user to download the file
114
+ st.download_button(
115
+ label="Download the Processed File",
116
+ data=downloaded_file_content,
117
+ file_name=f"{lang_name}-translated-{file_name}",
118
+ mime="application/octet-stream"
119
+ )