File size: 2,031 Bytes
5eaccf4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

# Your exact Hugging Face repository name from the screenshot
MODEL_ID = "ibz18/sft"

print("Downloading and loading the SFT model...")
# Because it's a standard safetensors repo, Hugging Face does all the work automatically!
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_ID)

model.eval()

def generate_summary(text):
    if not text.strip():
        return "Please enter some Bangla text."
    
    try:
        # If your SFT model used a prefix during training, uncomment and use it here:
        # text = "summarize: " + text
        
        inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
        
        with torch.no_grad():
            output_ids = model.generate(
                **inputs, 
                max_new_tokens=128,
                do_sample=False,
                num_beams=4,              # SFT models are usually more stable with beam search
                repetition_penalty=2.5,
                early_stopping=True,
                decoder_start_token_id=tokenizer.pad_token_id,
                eos_token_id=tokenizer.eos_token_id,
                pad_token_id=tokenizer.pad_token_id
            )
            
        summary = tokenizer.decode(output_ids[0], skip_special_tokens=True)
        
        if not summary or summary.isspace():
            return "ERROR: Model generated an empty string."
            
        return summary

    except Exception as e:
        return f"CRASH ERROR: {str(e)}"

# Create the Web Interface
demo = gr.Interface(
    fn=generate_summary,
    inputs=gr.Textbox(lines=8, label="Input Bangla Text", placeholder="এখানে আপনার বাংলা টেক্সট দিন..."),
    outputs=gr.Textbox(label="Generated Summary"),
    title="SFT Baseline Model",
    description="Live testing interface for the Supervised Fine-Tuned (SFT) model."
)

demo.launch()