Spaces:
Running
Running
File size: 4,007 Bytes
895c980 0a59081 9dcbee4 0a59081 f7aa0f6 0a59081 f7aa0f6 0a59081 f7aa0f6 476dd48 0a59081 da9c52f 0a59081 da9c52f 0a59081 798c21f da9c52f 8aae362 0a59081 b113724 0a59081 798c21f 0a59081 f7aa0f6 0a59081 f7aa0f6 0a59081 da9c52f 0a59081 b113724 0a59081 da9c52f 0a59081 b113724 0a59081 f7aa0f6 0a59081 f7aa0f6 0a59081 f7aa0f6 0a59081 9dcbee4 8aae362 0a59081 d2bdf71 f7aa0f6 8aae362 da9c52f 0a59081 895c980 f7aa0f6 8aae362 0a59081 8aae362 c37794f 0a59081 8aae362 c37794f 476dd48 9dcbee4 0a59081 8aae362 c37794f |
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 |
import gradio as gr
import os
import tempfile
from deep_translator import GoogleTranslator
from langdetect import detect
from fpdf import FPDF
import docx2txt
# Dictionary of language codes for translation
LANGUAGES = {
"English": "en",
"Spanish": "es",
"French": "fr",
"German": "de",
"Italian": "it",
"Portuguese": "pt",
"Russian": "ru",
"Chinese": "zh",
"Japanese": "ja",
"Korean": "ko",
"Arabic": "ar",
"Hindi": "hi"
}
def detect_language(text):
try:
return detect(text)
except:
return "en" # Default to English if detection fails
def translate_text(text, target_language):
if not text or not target_language:
return "No text to translate"
source_lang = detect_language(text)
if source_lang == LANGUAGES[target_language]:
return text
try:
translator = GoogleTranslator(source=source_lang, target=LANGUAGES[target_language])
translation = translator.translate(text)
return translation
except Exception as e:
return f"Translation failed: {str(e)}"
def extract_text_from_document(file):
if not file:
return ""
file_path = file.name # Gradio file object provides a 'name' attribute
_, file_extension = os.path.splitext(file_path)
if file_extension.lower() == '.txt':
with open(file_path, 'r', encoding='utf-8', errors='replace') as f:
return f.read()
elif file_extension.lower() in ['.docx', '.doc']:
return docx2txt.process(file_path)
else:
return "Unsupported file format. Please upload a .txt or .docx file."
def text_to_pdf(text, output_path):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
lines = text.split('\n')
for line in lines:
try:
pdf.multi_cell(0, 10, line.encode('latin-1', 'replace').decode('latin-1'))
except Exception:
pdf.multi_cell(0, 10, "[Text contains unsupported characters]")
pdf.output(output_path)
return output_path
def translate_and_save(input_text, input_file, target_language):
if input_text:
text_to_translate = input_text
elif input_file is not None:
text_to_translate = extract_text_from_document(input_file)
else:
return None, "Please provide either text or a document for translation."
translated_text = translate_text(text_to_translate, target_language)
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as temp_pdf:
pdf_path = temp_pdf.name
text_to_pdf(translated_text, pdf_path)
return pdf_path, translated_text
# Create Gradio interface with explicit types
with gr.Blocks(title="Context-Aware Translation Tool") as demo:
gr.Markdown("# Context-Aware Language Translation")
gr.Markdown("This tool translates text while preserving context, idioms, and phrases.")
with gr.Row():
with gr.Column():
input_text = gr.Textbox(label="Enter text to translate", lines=5, placeholder="Type your text here")
input_file = gr.File(label="Or upload a document (.txt, .docx)", file_types=[".txt", ".docx"])
target_language = gr.Dropdown(
label="Target Language",
choices=list(LANGUAGES.keys()),
value="English"
)
translate_button = gr.Button("Translate")
with gr.Column():
output_text = gr.Textbox(label="Translation", lines=5, interactive=False)
output_pdf = gr.File(label="Download as PDF")
# Define the event without _js
translate_button.click(
fn=translate_and_save,
inputs=[
input_text,
input_file,
target_language
],
outputs=[
output_pdf,
output_text
]
)
if __name__ == "__main__":
print(f"Gradio version: {gr.__version__}")
demo.launch(debug=True) |