Spaces:
Paused
Paused
Upload 3 files
Browse files- README.md +35 -6
- app.py +115 -0
- requirements-1.txt +8 -0
README.md
CHANGED
|
@@ -1,10 +1,39 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
-
sdk:
|
|
|
|
|
|
|
| 7 |
pinned: false
|
|
|
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: ModiTrans
|
| 3 |
+
emoji: 📜
|
| 4 |
+
colorFrom: yellow
|
| 5 |
+
colorTo: red
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 5.0.0
|
| 8 |
+
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
+
short_description: Transliterate historic Modi script images to Devanagari
|
| 12 |
---
|
| 13 |
|
| 14 |
+
# ModiTrans — Modi Script to Devanagari
|
| 15 |
+
|
| 16 |
+
Gradio demo for [`historyHulk/ModiTrans-12B-Gemma-Teacher`](https://huggingface.co/historyHulk/ModiTrans-12B-Gemma-Teacher),
|
| 17 |
+
a LoRA adapter on `google/gemma-3-12b-it` that transliterates scanned images
|
| 18 |
+
of historic Modi script into modern Devanagari text.
|
| 19 |
+
|
| 20 |
+
## Setup notes before deploying
|
| 21 |
+
|
| 22 |
+
1. **Accept the gated model license.** Visit the
|
| 23 |
+
[model page](https://huggingface.co/historyHulk/ModiTrans-12B-Gemma-Teacher)
|
| 24 |
+
while logged in and accept the access conditions. You'll also need to
|
| 25 |
+
accept the license for the base model
|
| 26 |
+
[`google/gemma-3-12b-it`](https://huggingface.co/google/gemma-3-12b-it).
|
| 27 |
+
2. **Add an `HF_TOKEN` secret** to this Space (Settings → Variables and
|
| 28 |
+
secrets) using a token from an account that has been granted access to
|
| 29 |
+
both gated repos.
|
| 30 |
+
3. **Hardware:** this Space is configured for **ZeroGPU** (`@spaces.GPU` in
|
| 31 |
+
`app.py`). ZeroGPU requires a PRO or verified Hugging Face account. If you
|
| 32 |
+
don't have ZeroGPU access, remove the `spaces` import/decorator in
|
| 33 |
+
`app.py` and instead select a dedicated GPU hardware tier (T4 or A10G) in
|
| 34 |
+
the Space settings.
|
| 35 |
+
|
| 36 |
+
## Files
|
| 37 |
+
|
| 38 |
+
- `app.py` — Gradio app + inference logic
|
| 39 |
+
- `requirements.txt` — Python dependencies
|
app.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
+
import torch
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from transformers import AutoProcessor, AutoModelForImageTextToText
|
| 6 |
+
from peft import PeftModel
|
| 7 |
+
|
| 8 |
+
BASE_MODEL_ID = "google/gemma-3-12b-it"
|
| 9 |
+
ADAPTER_ID = "historyHulk/ModiTrans-12B-Gemma-Teacher"
|
| 10 |
+
PROMPT = "Translitrate the following Modi script to Devnagri script."
|
| 11 |
+
MAX_NEW_TOKENS = 350
|
| 12 |
+
|
| 13 |
+
# Model + processor are loaded once at startup and kept on CPU.
|
| 14 |
+
# They are moved to GPU inside the @spaces.GPU-decorated function,
|
| 15 |
+
# which is how ZeroGPU spaces work (GPU only attached per-call).
|
| 16 |
+
print("Loading processor...")
|
| 17 |
+
processor = AutoProcessor.from_pretrained(BASE_MODEL_ID)
|
| 18 |
+
|
| 19 |
+
print("Loading base model...")
|
| 20 |
+
base_model = AutoModelForImageTextToText.from_pretrained(
|
| 21 |
+
BASE_MODEL_ID,
|
| 22 |
+
torch_dtype=torch.bfloat16,
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
print("Loading LoRA adapter...")
|
| 26 |
+
model = PeftModel.from_pretrained(
|
| 27 |
+
base_model,
|
| 28 |
+
ADAPTER_ID,
|
| 29 |
+
torch_dtype=torch.bfloat16,
|
| 30 |
+
)
|
| 31 |
+
model.eval()
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
@spaces.GPU
|
| 35 |
+
def transliterate(image: Image.Image) -> str:
|
| 36 |
+
if image is None:
|
| 37 |
+
return "Please upload an image of Modi script."
|
| 38 |
+
|
| 39 |
+
device = "cuda"
|
| 40 |
+
model.to(device)
|
| 41 |
+
|
| 42 |
+
image = image.convert("RGB").resize((1024, 512))
|
| 43 |
+
|
| 44 |
+
messages = [
|
| 45 |
+
{
|
| 46 |
+
"role": "user",
|
| 47 |
+
"content": [
|
| 48 |
+
{"type": "image", "image": image},
|
| 49 |
+
{"type": "text", "text": PROMPT},
|
| 50 |
+
],
|
| 51 |
+
},
|
| 52 |
+
]
|
| 53 |
+
|
| 54 |
+
inputs = processor.apply_chat_template(
|
| 55 |
+
messages,
|
| 56 |
+
add_generation_prompt=True,
|
| 57 |
+
tokenize=True,
|
| 58 |
+
return_dict=True,
|
| 59 |
+
return_tensors="pt",
|
| 60 |
+
).to(device, dtype=torch.bfloat16)
|
| 61 |
+
|
| 62 |
+
input_len = inputs["input_ids"].shape[-1]
|
| 63 |
+
|
| 64 |
+
with torch.no_grad():
|
| 65 |
+
output_ids = model.generate(
|
| 66 |
+
**inputs,
|
| 67 |
+
max_new_tokens=MAX_NEW_TOKENS,
|
| 68 |
+
do_sample=False,
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
generated = output_ids[0][input_len:]
|
| 72 |
+
text = processor.decode(generated, skip_special_tokens=True)
|
| 73 |
+
return text.strip()
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
with gr.Blocks(title="ModiTrans: Modi Script to Devanagari") as demo:
|
| 77 |
+
gr.Markdown(
|
| 78 |
+
"""
|
| 79 |
+
# ModiTrans — Modi Script to Devanagari Transliteration
|
| 80 |
+
|
| 81 |
+
Upload a scanned image of historic **Modi script** text and this model
|
| 82 |
+
will transliterate it into modern **Devanagari** script.
|
| 83 |
+
|
| 84 |
+
Uses [`historyHulk/ModiTrans-12B-Gemma-Teacher`](https://huggingface.co/historyHulk/ModiTrans-12B-Gemma-Teacher),
|
| 85 |
+
a LoRA adapter on `google/gemma-3-12b-it`, from the paper
|
| 86 |
+
*"Historic Scripts to Modern Vision: A Novel Dataset and A VLM Framework
|
| 87 |
+
for Transliteration of Modi Script to Devanagari"* (ICDAR 2025).
|
| 88 |
+
|
| 89 |
+
> This is a gated model — the Space owner's `HF_TOKEN` must have accepted
|
| 90 |
+
> access on the model page for inference to work.
|
| 91 |
+
"""
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
with gr.Row():
|
| 95 |
+
with gr.Column():
|
| 96 |
+
image_input = gr.Image(type="pil", label="Modi Script Image")
|
| 97 |
+
run_btn = gr.Button("Transliterate", variant="primary")
|
| 98 |
+
with gr.Column():
|
| 99 |
+
output_text = gr.Textbox(
|
| 100 |
+
label="Devanagari Transliteration", lines=8
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
run_btn.click(fn=transliterate, inputs=image_input, outputs=output_text)
|
| 104 |
+
image_input.change(fn=transliterate, inputs=image_input, outputs=output_text)
|
| 105 |
+
|
| 106 |
+
gr.Markdown(
|
| 107 |
+
"""
|
| 108 |
+
---
|
| 109 |
+
**Citation:** Kausadikar, H., Kale, T., Susladkar, O., Mittal, S.
|
| 110 |
+
*Historic Scripts to Modern Vision.* ICDAR 2025 (Springer LNCS).
|
| 111 |
+
"""
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
if __name__ == "__main__":
|
| 115 |
+
demo.launch()
|
requirements-1.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
spaces
|
| 2 |
+
torch
|
| 3 |
+
torchvision
|
| 4 |
+
transformers>=4.50.0
|
| 5 |
+
peft
|
| 6 |
+
accelerate
|
| 7 |
+
pillow
|
| 8 |
+
gradio
|