indictrans2-3 / app.py
kasimali's picture
Upload folder using huggingface_hub
cf315f4 verified
Raw
History Blame Contribute Delete
5.37 kB
# indictrans2-3
# --- 1. CLEAN UP AND CLONE THE REPOSITORY ---
print("Cleaning up old directories and cloning the repository...")
print("✅ Repository cloned successfully.")
# --- 2. INSTALL CORE LIBRARIES ---
print("Installing core libraries...")
print("✅ Core libraries installed.")
# --- 3. SET UP THE SYSTEM PATH (THE OFFICIAL METHOD) ---
# This is the crucial step from the official notebook.
# It tells Python where to find the IndicTransToolkit module without installation.
import sys
sys.path.insert(0, '/content/IndicTrans2/src')
print("✅ System path configured for IndicTransToolkit.")
# --- 4. IMPORT ALL PACKAGES ---
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
from IndicTransToolkit.processor import IndicProcessor
import torch
print("✅ All packages imported successfully.")
# --- 5. LOAD THE MODEL, TOKENIZER, AND PROCESSOR ---
model_name = "ai4bharat/indictrans2-indic-en-dist-200M" # Using the CPU-friendly model
print("Loading the model and other components...")
device = torch.device("cpu")
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name, trust_remote_code=True).to(device)
ip = IndicProcessor(inference=True)
print("✅ Model, Tokenizer, and IndicProcessor are ready!")
# --- 6. DEFINE THE TRANSLATION FUNCTION (Using the correct workflow) ---
SUPPORTED_LANGUAGES = {
"Hindi": "hin_Deva", "Bengali": "ben_Beng", "Tamil": "tam_Taml",
"Telugu": "tel_Telu", "Kannada": "kan_Knda", "Malayalam": "mal_Mlym",
"Gujarati": "guj_Gujr", "Punjabi": "pan_Guru", "Marathi": "mar_Deva",
"Urdu": "urd_Arab", "Assamese": "asm_Beng", "Oriya": "ory_Orya",
"Nepali": "npi_Deva"
}
def translate_correctly(native_text, source_language_name):
try:
if not native_text or not native_text.strip():
return "Please enter text to translate."
src_lang = SUPPORTED_LANGUAGES[source_language_name]
tgt_lang = "eng_Latn"
# 1. Preprocess the text using IndicProcessor
processed_text = ip.preprocess_batch([native_text], src_lang=src_lang, tgt_lang=tgt_lang)
# 2. Tokenize the preprocessed text
inputs = tokenizer(processed_text, return_tensors="pt", padding=True).to(device)
# 3. Generate translation
with torch.no_grad():
translated_tokens = model.generate(**inputs, num_beams=5, max_length=256)
# 4. Decode the tokens
decoded_translation = tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)
# 5. Postprocess the translation
final_translation = ip.postprocess_batch(decoded_translation, lang=src_lang)
return final_translation[0]
except Exception as e:
return f"An error occurred: {str(e)}"
print("✅ Correct translation function is ready.")
# --- 7. CREATE AND LAUNCH THE GRADIO APP ---
iface_final = gr.Interface(
fn=translate_correctly,
inputs=[
gr.Textbox(lines=5, label="Native Indian Language Text", placeholder="यहाँ अपना पाठ दर्ज करें..."),
gr.Dropdown(choices=list(SUPPORTED_LANGUAGES.keys()), label="Select Source Language", value="Hindi")
],
outputs=gr.Textbox(label="English Translation"),
title="IndicTrans2 Translator (Official Workflow)",
description="Translate from 13 Indian languages to English using the official AI4Bharat workflow and IndicProcessor.",
examples=[
["नमस्ते, आप कैसे हैं?", "Hindi"],
["வணக்கம், நீங்கள் எப்படி இருக்கிறீர்கள்?", "Tamil"],
["হ্যালো, আপনি কেমন আছেন?", "Bengali"]
]
)
print("🚀 Launching the final, corrected Gradio app...")
iface_final.launch(share=True)
# --- Step 1: Clean up and clone the repository ---
# This ensures we have a fresh and correct copy.
import os
# --- Step 2: Verify that the 'src' directory exists ---
src_path = '/content/IndicTrans2/src'
if os.path.isdir(src_path):
print(f"✅ SUCCESS: The directory '{src_path}' exists.")
# --- Step 3: Check for the 'IndicTransToolkit' within 'src' ---
toolkit_path = os.path.join(src_path, 'IndicTransToolkit')
if os.path.isdir(toolkit_path):
print(f"✅ SUCCESS: The 'IndicTransToolkit' directory was found inside 'src'.")
else:
print(f"⚠️ WARNING: The 'IndicTransToolkit' directory was NOT found directly inside 'src'. The structure might have changed.")
# --- Step 4: Attempt to import the processor ---
import sys
# Add the src directory to Python's path
sys.path.insert(0, src_path)
try:
from IndicTransToolkit.processor import IndicProcessor
print("✅ SUCCESS: Successfully imported 'IndicProcessor' from 'IndicTransToolkit'.")
print("\n🎉 Your environment is set up correctly!")
except ImportError as e:
print(f"❌ ERROR: Failed to import 'IndicProcessor'. Python returned the following error: {e}")
print("This means the Python path is likely correct, but the module name or structure is wrong.")
else:
print(f"❌ ERROR: The directory '{src_path}' does not exist.")
print("This means the 'git clone' command likely failed or cloned to a different location.")