sft / app.py
ibz18's picture
Create app.py
5eaccf4 verified
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()