Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import os
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
+
|
| 7 |
+
# This points to the repo where you uploaded the .pt files in Phase 1
|
| 8 |
+
MODEL_REPO = "ibz18/Model_D_weights"
|
| 9 |
+
BASE_MODEL = "csebuetnlp/banglat5"
|
| 10 |
+
|
| 11 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 12 |
+
|
| 13 |
+
print("1. Downloading .pt file...")
|
| 14 |
+
abstracter_rl_path = hf_hub_download(
|
| 15 |
+
repo_id=MODEL_REPO,
|
| 16 |
+
filename="abstracter_rl.pt",
|
| 17 |
+
token=hf_token
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
print("2. Loading tokenizer and base model...")
|
| 21 |
+
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
|
| 22 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(BASE_MODEL)
|
| 23 |
+
|
| 24 |
+
print("3. Resizing embeddings...")
|
| 25 |
+
model.resize_token_embeddings(len(tokenizer))
|
| 26 |
+
|
| 27 |
+
print("4. Injecting .pt weights into memory...")
|
| 28 |
+
checkpoint = torch.load(abstracter_rl_path, map_location="cpu", weights_only=True)
|
| 29 |
+
|
| 30 |
+
if isinstance(checkpoint, dict) and "state_dict" in checkpoint:
|
| 31 |
+
state_dict = checkpoint["state_dict"]
|
| 32 |
+
else:
|
| 33 |
+
state_dict = checkpoint
|
| 34 |
+
|
| 35 |
+
model.load_state_dict(state_dict, strict=False)
|
| 36 |
+
model.eval()
|
| 37 |
+
|
| 38 |
+
def generate_summary(text):
|
| 39 |
+
if not text.strip():
|
| 40 |
+
return "Please enter Bangla text."
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
|
| 44 |
+
|
| 45 |
+
with torch.no_grad():
|
| 46 |
+
output_ids = model.generate(
|
| 47 |
+
**inputs,
|
| 48 |
+
max_new_tokens=128,
|
| 49 |
+
do_sample=False,
|
| 50 |
+
num_beams=2,
|
| 51 |
+
repetition_penalty=2.5,
|
| 52 |
+
early_stopping=True,
|
| 53 |
+
decoder_start_token_id=tokenizer.pad_token_id,
|
| 54 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 55 |
+
pad_token_id=tokenizer.pad_token_id
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
summary = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 59 |
+
return summary if summary.strip() else "ERROR: Empty string"
|
| 60 |
+
|
| 61 |
+
except Exception as e:
|
| 62 |
+
return f"CRASH ERROR: {str(e)}"
|
| 63 |
+
|
| 64 |
+
# --- INTERFACE WITH NEW NAME ---
|
| 65 |
+
demo = gr.Interface(
|
| 66 |
+
fn=generate_summary,
|
| 67 |
+
inputs=gr.Textbox(lines=8, label="Input Bangla Text", placeholder="এখানে আপনার বাংলা টেক্সট দিন..."),
|
| 68 |
+
outputs=gr.Textbox(label="Generated Summary"),
|
| 69 |
+
title="Model_D",
|
| 70 |
+
description="Live testing interface for Model_D"
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
demo.launch()
|