-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_script.py
207 lines (185 loc) · 6.82 KB
/
main_script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import streamlit as st
import requests
from azure_ocr import GetTextRead
from azure_translator import translate_text
from dotenv import load_dotenv
import os
import numpy as np
# Set the title of the tab
st.set_page_config(page_title="Lingual Lens")
# Custom CSS style
cus_css = """
body {
color: white; /* Change the text color to white */
font-family: Arial, sans-serif;
background-color: #f2f2f2;
background-color: #f2f2f2;
background-image: repeating-linear-gradient(45deg, transparent, transparent 20px, #c0c0c0 20px, #c0c0c0 40px);
}
.stButton {
background-color: #007BFF !important;
color: white !important;
font-weight: bold !important;
border-radius: 5px !important;
}
.stSelectbox {
background-color: #24252E !important;
color: #333 !important;
font-weight: bold !important;
border-radius: 5px !important;
}
[data-testid="stAppViewContainer"] {
background-color: #00008B;
opacity: 0.9;
background-image: linear-gradient(45deg, #000000 12%, transparent 15.5%, transparent 87%, #000000 87.5%, #000000), linear-gradient(150deg, #000000 12%, transparent 12.5%, transparent 87%, #000000 87.5%, #000000), linear-gradient(30deg, #000000 12%, transparent 12.5%, transparent 87%, #000000 87.5%, #000000), linear-gradient(150deg, #000000 12%, transparent 12.5%, transparent 87%, #000000 87.5%, #000000), linear-gradient(60deg, #00000077 25%, transparent 25.5%, transparent 75%, #00000077 75%, #00000077), linear-gradient(60deg, #00000077 25%, transparent 25.5%, transparent 75%, #00000077 75%, #00000077);
background-size: 80px 140px;
background-position: 0 0, 0 0, 40px 70px, 40px 70px, 0 0, 40px 70px;}
"""
# Load environment variables from .env file
load_dotenv()
# Apply custom CSS
st.markdown(f"<style>{cus_css}</style>", unsafe_allow_html=True)
trans_api_key = "adbba3a6ac6c4366be43766e7d8ea0aa"
trans_api_region = "eastasia"
# Streamlit app title and instructions
st.title("Lingual Lens")
st.write("Break language barriers effortlessly. Translate text from images and documents with Azure-powered precision for global communication.", size="small")
# First field for image upload
#st.subheader("Upload your files here")
uploaded_file = st.file_uploader("", type=["jpg", "jpeg", "png", "pdf"])
if uploaded_file is not None:
# Save the uploaded image to a temporary file
image_path = "temp_image.jpg"
with open(image_path, "wb") as f:
f.write(uploaded_file.read())
# Process the image if available
if "image_path" in locals():
# Call the OCR function and get the extracted text
uf = image_path
extracted_text = GetTextRead(uf)
# Delete the temporary image file
#os.remove(image_path)
# Display the extracted text
if extracted_text:
st.subheader("Scanned Text:")
#st.write(extracted_text)
lines = extracted_text.split("\n")
for line in lines:
st.write(line)
print(lines)
# Dictionary mapping language codes to full names
language_names = {
"en": "English",
"fr": "French",
"zu": "Zulu",
"es": "Spanish",
"it": "Italian",
"ar": "Arabic",
"hi": "Hindi",
"ml": "Malayalam",
"zh-Hans": "Simplified Chinese",
"de": "German",
"ja": "Japanese",
"ko": "Korean",
"ru": "Russian",
"pt": "Portuguese",
"tr": "Turkish",
"nl": "Dutch",
"th": "Thai",
"sv": "Swedish",
"fi": "Finnish",
"no": "Norwegian",
"da": "Danish",
"pl": "Polish",
"id": "Indonesian",
"uk": "Ukrainian",
"cs": "Czech",
"el": "Greek",
"ro": "Romanian",
"hu": "Hungarian",
"he": "Hebrew",
"bn": "Bengali",
"ta": "Tamil",
"te": "Telugu",
"vi": "Vietnamese",
"mr": "Marathi",
"ur": "Urdu",
"fa": "Persian",
"af": "Afrikaans",
"sw": "Swahili",
"tl": "Tagalog",
"ne": "Nepali",
"pa": "Punjabi",
"gu": "Gujarati",
"my": "Burmese",
"sd": "Sindhi",
"ha": "Hausa",
"yo": "Yoruba",
"ig": "Igbo",
"si": "Sinhala",
"km": "Khmer",
"sn": "Shona",
"so": "Somali",
"am": "Amharic",
"jv": "Javanese",
"rw": "Kinyarwanda",
"mg": "Malagasy",
"zu": "Zulu",
}
# Third field for language selection
target_languages = list(language_names.keys()) # Use the list of keys from the dictionary
target_language = st.selectbox("\tSelect target language for translation",[language_names[code] for code in target_languages])
for code in target_languages:
if language_names[code]==target_language:
print(code)
for k, v in language_names.items():
if v == target_language:
target_language_code = k
print(target_language_code)
print("Translator API Key:", trans_api_key)
print("Translator API Region:", trans_api_region)
with st.spinner("Translating..."):
translation_response = translate_text(trans_api_key, trans_api_region, extracted_text, [target_language_code])
if translation_response:
# Display the translated text
translated_text = translation_response[0]['translations'][0]['text']
st.subheader("Translation:")
#st.write(translated_text)
lines = translated_text.split("\n")
for line in lines:
st.write(line)
else:
st.error("Translation failed. Please try again later.")
else:
st.error("Text extraction failed.")
def get_st_button_a_tag(url_link, button_name):
"""
generate html a tag
:param url_link:
:param button_name:
:return:
"""
return f'''
<a href={url_link}><button style="
fontWeight: 400;
padding: 0.25rem 0.75rem;
borderRadius: 0.25rem;
margin: 0px;
lineHeight: 1.6;
width: auto;
userSelect: none;
backgroundColor: #24252E;
border: 1px solid rgba(49, 51, 63, 0.2);
color: #FFFFFF;">{button_name}</button></a>
'''
st.write("", size="small")
st.write("", size="small")
st.write("", size="small")
st.write("", size="small")
st.write("", size="small")
st.write("", size="small")
st.write("", size="small")
st.write("", size="small")
st.write("", size="small")
st.markdown(get_st_button_a_tag('https://github.com/meaviral17', 'GitHub Profile'), unsafe_allow_html=True)
st.write("Made with 🤍 by Aviral Srivastava.", size="small")