Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- README.md +5 -8
- UPLOAD_INSTRUCTIONS.txt +20 -0
- app.py +124 -0
- requirements.txt +4 -0
README.md
CHANGED
|
@@ -1,12 +1,9 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom: purple
|
| 5 |
-
colorTo: gray
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 5.49.0
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: indictrans2-3
|
| 3 |
+
emoji: 🚀
|
|
|
|
|
|
|
| 4 |
sdk: gradio
|
|
|
|
|
|
|
|
|
|
| 5 |
---
|
| 6 |
|
| 7 |
+
# indictrans2-3
|
| 8 |
+
|
| 9 |
+
Gradio application
|
UPLOAD_INSTRUCTIONS.txt
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Upload this Space to Hugging Face
|
| 2 |
+
# Run this in a new Colab cell tomorrow:
|
| 3 |
+
|
| 4 |
+
from huggingface_hub import HfApi, create_repo, login
|
| 5 |
+
|
| 6 |
+
login()
|
| 7 |
+
api = HfApi()
|
| 8 |
+
|
| 9 |
+
USERNAME = "kasimali"
|
| 10 |
+
SPACE_NAME = "indictrans2-3"
|
| 11 |
+
|
| 12 |
+
create_repo(repo_id=f"{USERNAME}/{SPACE_NAME}", repo_type="space", space_sdk="gradio", exist_ok=True)
|
| 13 |
+
|
| 14 |
+
api.upload_folder(
|
| 15 |
+
folder_path="./indictrans2-3",
|
| 16 |
+
repo_id=f"{USERNAME}/{SPACE_NAME}",
|
| 17 |
+
repo_type="space"
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
print(f"Uploaded: https://huggingface.co/spaces/{USERNAME}/{SPACE_NAME}")
|
app.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# indictrans2-3
|
| 2 |
+
|
| 3 |
+
# --- 1. CLEAN UP AND CLONE THE REPOSITORY ---
|
| 4 |
+
print("Cleaning up old directories and cloning the repository...")
|
| 5 |
+
print("✅ Repository cloned successfully.")
|
| 6 |
+
|
| 7 |
+
# --- 2. INSTALL CORE LIBRARIES ---
|
| 8 |
+
print("Installing core libraries...")
|
| 9 |
+
print("✅ Core libraries installed.")
|
| 10 |
+
|
| 11 |
+
# --- 3. SET UP THE SYSTEM PATH (THE OFFICIAL METHOD) ---
|
| 12 |
+
# This is the crucial step from the official notebook.
|
| 13 |
+
# It tells Python where to find the IndicTransToolkit module without installation.
|
| 14 |
+
import sys
|
| 15 |
+
sys.path.insert(0, '/content/IndicTrans2/src')
|
| 16 |
+
print("✅ System path configured for IndicTransToolkit.")
|
| 17 |
+
|
| 18 |
+
# --- 4. IMPORT ALL PACKAGES ---
|
| 19 |
+
import gradio as gr
|
| 20 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 21 |
+
from IndicTransToolkit.processor import IndicProcessor
|
| 22 |
+
import torch
|
| 23 |
+
print("✅ All packages imported successfully.")
|
| 24 |
+
|
| 25 |
+
# --- 5. LOAD THE MODEL, TOKENIZER, AND PROCESSOR ---
|
| 26 |
+
model_name = "ai4bharat/indictrans2-indic-en-dist-200M" # Using the CPU-friendly model
|
| 27 |
+
print("Loading the model and other components...")
|
| 28 |
+
device = torch.device("cpu")
|
| 29 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 30 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name, trust_remote_code=True).to(device)
|
| 31 |
+
ip = IndicProcessor(inference=True)
|
| 32 |
+
print("✅ Model, Tokenizer, and IndicProcessor are ready!")
|
| 33 |
+
|
| 34 |
+
# --- 6. DEFINE THE TRANSLATION FUNCTION (Using the correct workflow) ---
|
| 35 |
+
SUPPORTED_LANGUAGES = {
|
| 36 |
+
"Hindi": "hin_Deva", "Bengali": "ben_Beng", "Tamil": "tam_Taml",
|
| 37 |
+
"Telugu": "tel_Telu", "Kannada": "kan_Knda", "Malayalam": "mal_Mlym",
|
| 38 |
+
"Gujarati": "guj_Gujr", "Punjabi": "pan_Guru", "Marathi": "mar_Deva",
|
| 39 |
+
"Urdu": "urd_Arab", "Assamese": "asm_Beng", "Oriya": "ory_Orya",
|
| 40 |
+
"Nepali": "npi_Deva"
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
def translate_correctly(native_text, source_language_name):
|
| 44 |
+
try:
|
| 45 |
+
if not native_text or not native_text.strip():
|
| 46 |
+
return "Please enter text to translate."
|
| 47 |
+
|
| 48 |
+
src_lang = SUPPORTED_LANGUAGES[source_language_name]
|
| 49 |
+
tgt_lang = "eng_Latn"
|
| 50 |
+
|
| 51 |
+
# 1. Preprocess the text using IndicProcessor
|
| 52 |
+
processed_text = ip.preprocess_batch([native_text], src_lang=src_lang, tgt_lang=tgt_lang)
|
| 53 |
+
# 2. Tokenize the preprocessed text
|
| 54 |
+
inputs = tokenizer(processed_text, return_tensors="pt", padding=True).to(device)
|
| 55 |
+
# 3. Generate translation
|
| 56 |
+
with torch.no_grad():
|
| 57 |
+
translated_tokens = model.generate(**inputs, num_beams=5, max_length=256)
|
| 58 |
+
# 4. Decode the tokens
|
| 59 |
+
decoded_translation = tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)
|
| 60 |
+
# 5. Postprocess the translation
|
| 61 |
+
final_translation = ip.postprocess_batch(decoded_translation, lang=src_lang)
|
| 62 |
+
|
| 63 |
+
return final_translation[0]
|
| 64 |
+
except Exception as e:
|
| 65 |
+
return f"An error occurred: {str(e)}"
|
| 66 |
+
|
| 67 |
+
print("✅ Correct translation function is ready.")
|
| 68 |
+
|
| 69 |
+
# --- 7. CREATE AND LAUNCH THE GRADIO APP ---
|
| 70 |
+
iface_final = gr.Interface(
|
| 71 |
+
fn=translate_correctly,
|
| 72 |
+
inputs=[
|
| 73 |
+
gr.Textbox(lines=5, label="Native Indian Language Text", placeholder="यहाँ अपना पाठ दर्ज करें..."),
|
| 74 |
+
gr.Dropdown(choices=list(SUPPORTED_LANGUAGES.keys()), label="Select Source Language", value="Hindi")
|
| 75 |
+
],
|
| 76 |
+
outputs=gr.Textbox(label="English Translation"),
|
| 77 |
+
title="IndicTrans2 Translator (Official Workflow)",
|
| 78 |
+
description="Translate from 13 Indian languages to English using the official AI4Bharat workflow and IndicProcessor.",
|
| 79 |
+
examples=[
|
| 80 |
+
["नमस्ते, आप कैसे हैं?", "Hindi"],
|
| 81 |
+
["வணக்கம், நீங்கள் எப்படி இருக்கிறீர்கள்?", "Tamil"],
|
| 82 |
+
["হ্যালো, আপনি কেমন আছেন?", "Bengali"]
|
| 83 |
+
]
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
print("🚀 Launching the final, corrected Gradio app...")
|
| 87 |
+
iface_final.launch(share=True)
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
# --- Step 1: Clean up and clone the repository ---
|
| 92 |
+
# This ensures we have a fresh and correct copy.
|
| 93 |
+
|
| 94 |
+
import os
|
| 95 |
+
|
| 96 |
+
# --- Step 2: Verify that the 'src' directory exists ---
|
| 97 |
+
src_path = '/content/IndicTrans2/src'
|
| 98 |
+
if os.path.isdir(src_path):
|
| 99 |
+
print(f"✅ SUCCESS: The directory '{src_path}' exists.")
|
| 100 |
+
|
| 101 |
+
# --- Step 3: Check for the 'IndicTransToolkit' within 'src' ---
|
| 102 |
+
toolkit_path = os.path.join(src_path, 'IndicTransToolkit')
|
| 103 |
+
if os.path.isdir(toolkit_path):
|
| 104 |
+
print(f"✅ SUCCESS: The 'IndicTransToolkit' directory was found inside 'src'.")
|
| 105 |
+
else:
|
| 106 |
+
print(f"⚠️ WARNING: The 'IndicTransToolkit' directory was NOT found directly inside 'src'. The structure might have changed.")
|
| 107 |
+
|
| 108 |
+
# --- Step 4: Attempt to import the processor ---
|
| 109 |
+
import sys
|
| 110 |
+
# Add the src directory to Python's path
|
| 111 |
+
sys.path.insert(0, src_path)
|
| 112 |
+
|
| 113 |
+
try:
|
| 114 |
+
from IndicTransToolkit.processor import IndicProcessor
|
| 115 |
+
print("✅ SUCCESS: Successfully imported 'IndicProcessor' from 'IndicTransToolkit'.")
|
| 116 |
+
print("\n🎉 Your environment is set up correctly!")
|
| 117 |
+
except ImportError as e:
|
| 118 |
+
print(f"❌ ERROR: Failed to import 'IndicProcessor'. Python returned the following error: {e}")
|
| 119 |
+
print("This means the Python path is likely correct, but the module name or structure is wrong.")
|
| 120 |
+
|
| 121 |
+
else:
|
| 122 |
+
print(f"❌ ERROR: The directory '{src_path}' does not exist.")
|
| 123 |
+
print("This means the 'git clone' command likely failed or cloned to a different location.")
|
| 124 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
IndicTransToolkit
|
| 2 |
+
gradio
|
| 3 |
+
torch
|
| 4 |
+
transformers
|