Spaces:
Runtime error
Runtime error
import streamlit as st | |
from io import BytesIO | |
# import gradio as gr | |
# Def_04 Docx file to translated_Docx file | |
#from transformers import MarianMTModel, MarianTokenizer | |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM | |
import nltk | |
from nltk.tokenize import sent_tokenize | |
from nltk.tokenize import LineTokenizer | |
nltk.download('punkt') | |
import math | |
import torch | |
from docx import Document | |
from time import sleep | |
from stqdm import stqdm | |
import docx | |
def getText(filename): | |
doc = docx.Document(filename) | |
fullText = [] | |
for para in doc.paragraphs: | |
fullText.append(para.text) | |
return '\n'.join(fullText) | |
# mname = 'Helsinki-NLP/opus-mt-en-hi' | |
# tokenizer = MarianTokenizer.from_pretrained(mname) | |
# model = MarianMTModel.from_pretrained(mname) | |
# model.to(device) | |
#@st.cache | |
def btTranslator(docxfile): | |
if torch.cuda.is_available(): | |
dev = "cuda" | |
else: | |
dev = "cpu" | |
device = torch.device(dev) | |
#a=getText(docxfile) | |
a1=docxfile.split('\n') | |
bigtext=''' ''' | |
for a in a1: | |
bigtext=bigtext+'\n'+a | |
files=Document() | |
a="Helsinki-NLP/opus-mt-en-ru" | |
b="Helsinki-NLP/opus-mt-ru-fr" | |
c="Helsinki-NLP/opus-mt-fr-en" | |
# d="Helsinki-NLP/opus-mt-es-en" | |
langs=[a,b,c] | |
text=bigtext | |
for _,lang in zip(stqdm(langs),langs): | |
st.spinner('Wait for it...') | |
sleep(0.5) | |
# mname = '/content/drive/MyDrive/Transformers Models/opus-mt-en-hi-Trans Model' | |
tokenizer = AutoTokenizer.from_pretrained(lang) | |
model = AutoModelForSeq2SeqLM.from_pretrained(lang) | |
model.to(device) | |
lt = LineTokenizer() | |
batch_size = 64 | |
paragraphs = lt.tokenize(bigtext) | |
translated_paragraphs = [] | |
for _, paragraph in zip(stqdm(paragraphs),paragraphs): | |
st.spinner('Wait for it...') | |
# ###################################### | |
sleep(0.5) | |
# ###################################### | |
sentences = sent_tokenize(paragraph) | |
batches = math.ceil(len(sentences) / batch_size) | |
translated = [] | |
for i in range(batches): | |
sent_batch = sentences[i*batch_size:(i+1)*batch_size] | |
model_inputs = tokenizer(sent_batch, return_tensors="pt", padding=True, truncation=True, max_length=500).to(device) | |
with torch.no_grad(): | |
translated_batch = model.generate(**model_inputs) | |
translated += translated_batch | |
translated = [tokenizer.decode(t, skip_special_tokens=True) for t in translated] | |
translated_paragraphs += [" ".join(translated)] | |
#files.add_paragraph(translated) | |
translated_text = "\n".join(translated_paragraphs) | |
bigtext=translated_text | |
files.add_paragraph(bigtext) | |
#files2save=files.save("Translated.docx") | |
#files.save("Translated.docx") | |
#binary_output = BytesIO() | |
#f=files.save(binary_output) | |
#f2=f.getvalue() | |
return files | |
#return translated_text | |
st.title('Translator App') | |
st.markdown("Translate from Docx file") | |
st.subheader("File Upload") | |
datas=st.file_uploader("Original File") | |
name=st.text_input('Enter New File Name: ') | |
#data=getText("C:\Users\Ambresh C\Desktop\Python Files\Translators\Trail Doc of 500 words.docx") | |
#if datas : | |
#if st.button(label='Data Process'): | |
binary_output = BytesIO() | |
if st.button(label='Translate'): | |
st.spinner('Waiting...') | |
btTranslator(datas).save(binary_output) | |
binary_output.getbuffer() | |
st.success("Translated") | |
st.download_button(label='Download Translated File',file_name=(f"{name}_Translated.docx"), data=binary_output.getvalue()) | |
#files.save(f"{name}_Translated.docx") | |
#else: | |
# st.text('Upload File and Start the process') | |
#f4=binary_output(f3) | |
#st.sidebar.download_button(label='Download Translated File',file_name='Translated.docx', data=binary_output.getvalue()) | |
# st.text_area(label="",value=btTranslator(datas),height=100) | |
# Footer | |
#################################################################### | |
#Trails | |
print("Program runing.....") | |
texts=""" | |
1 | A Lonesome Bride | |
GIANITA | |
March 1933 CE | |
Most women say that they enjoy some mystery in a man. | |
Like a book, where the more you look, the more things you discover about them. The more mysterious the man, the more desirable he becomes. | |
Unfortunately, that wasn't the case for Gianita. Her husband-to-be was a mystery, but all it did for her was fill her with anxiety and trepidation. | |
With a tempestuous heart, she stepped out of her carriage onto the docks of the royal port. There she went, being sent miles across the ocean to a man she'd never known, merely a month after her nineteenth birthday. | |
Born a princess of Prajanagara, she had long become accustomed to the idea that one day she would enter a political union. So it was no surprise that her father, the King, had arranged her to marry the Emperor of Angletonia, a formidable nation in the western hemisphere. Being the first time they ever sought a consort from the east, it was an honour for them to be selected for the position; a prospect that should be triumphantly rejoiced. | |
The Emperor was twenty-seven years old, widowed, with a son whom he had appointed as heir. There were louder whispers about his looks, and how lucky she was to marry him. As she had heard many times; met only with a roll of her eyes. | |
But one thing she had failed to understand; the man who had asked for her hand in marriage made no efforts to get to know her. Unlike her sisters who enjoyed a period of courtship, there were no letters, warm greetings or even words from him; outside the formal exchanges he made with her father. | |
Wouldn't a man, let alone a master of the world, be interested in his own bride? | |
Her jaw went slack as she set eyes upon the steamship that was docked at the royal port. This was the ship that was supposed to take her abroad? This humongous thing looked like it could carry an entire village! It eclipsed even the grandest ship belonging to her family. | |
"There must be a mistake. I don't think this is it, we should go back," she skittishly told the member of staff that had come with her. | |
"This is it, Your Highness," said a government official, dispatched by the embassy to send her away. "It bears the Angletonian flag on the bulkhead, see." | |
Turning to the ship, Gigi saw the name 'Angletonia' plastered on its body, and a banner of the Great Serpent; their imperial coat of arms. It was indeed the one. | |
Her fiance could summon the most ridiculously magnificent steamer in the world to pick her up at a whim, yet he couldn't buy a spare minute to see her once before the wedding? Why couldn't he collect her at the docks himself? | |
Holding her head high, she climbed aboard, her entourage followed behind her. | |
The journey there had been emotionally exhausting to her. She had to watch her youngest brother being held back by the servants as her ship set sail. She had never thought he would chase after her, which made everything harder. | |
Angletonia, a powerful empire that laid across the west, greatly known for their advanced military strength. Ruled by the Leroys, an old noble house bearing the crest of a Serpent. Even the weather differed a lot from home; if Prajanagara had the dry and rainy seasons, Angletonia had freezing winters and windy falls. | |
Before reaching the mainland, they sailed past the Southern Isles; a large mountainous island with two smaller ones flanking its sides. Magnificent but deserted, void of human contact. The Leroy kings had ruled Angletonia for centuries before conquering them two or three generations ago, making them emperors. | |
Fourteen days passed before Gigi finally set her foot on Angletonian ground for the first time, adorned in a Kebaya; the Prajan traditional clothing. A long-sleeved white brocade bodice over a Batik skirt, a nod to the Angletonian custom for brides to wear white. | |
Expecting a carriage, an elegant Cadillac had been sent over to fetch her. | |
On the way to the palace, she could hear the blaring noises of military aircraft patrolling from above; a reminder of the power and influence the Empire had on other countries, such as her own. | |
Her mind immediately went to the Emperor. She had yet to meet him, and here she was already immersed in his massive display of power, wealth and luxury. What was he like in person? A tyrant? An overbearing ruler? | |
But he chose her, he should at least hold some affection for her. Although they never met, she was going to be his wife; she supposed he would be kind. | |
The imperial palace came into view and the car stopped before an abbey. Her handmaidens put a long embroidered veil over her head; another custom of Angletonia's to put a veil on brides. Gigi wore it like a protective cape, shielding herself from a thousand stares and cheers from the crowd. | |
Her heart pounded against her ribs as she walked down the aisle, but she kept her chin up and shoulders back; repeating the things that her mother had been telling her since she was a little girl. | |
Be regal. Be noble. Stay proud. You are born to lead these people. | |
Gigi felt with each pulse how every note sung by the choir brought her another step closer to her groom, who looked little more than a hazy silhouette. He extended his hand, and she took it; feeling warmth enveloping her skin as it was held in his bigger palm. | |
The music stopped, and he raised his hands, uncovering the veil from her face. | |
The Emperor was tall and statuesque, and, to her astonishment, looked even better than his portraits. His slicked jet-black hair contrasted his fair complexion; with striking facial features of a sharp nose, succulent lips and cheekbones that looked like they could cut glass. He exuded an air of superiority, but his eyes were a warm, copper brown; bringing the heat onto her cheeks. | |
Suddenly, there was an urge to lower her gaze, but she refused to give in, keeping her stare level with his. He took notice of it, lips curving into a regal smile. | |
""" | |
#btTranslator(texts) | |
#################################################################### | |