File size: 1,979 Bytes
0d3a311 b0fd48b 1a152ce b0fd48b 0d3a311 b0fd48b 0d3a311 1a152ce 0d3a311 3b1f266 0d3a311 671de3c b0fd48b 0d3a311 b0fd48b 4be7028 b0fd48b 0d3a311 b0fd48b 0d3a311 | 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 59 60 61 62 63 64 65 66 | # app.py for Gradio with PEFT
import gradio as gr
import torch
import os
from huggingface_hub import login
from transformers import T5Tokenizer, T5ForConditionalGeneration
from peft import PeftModel, PeftConfig
# Load model once at startup
model = None
tokenizer = None
def load_model_once():
global model, tokenizer
if model is None:
hf_token = os.environ.get('HF_TOKEN')
login(token=hf_token)
# Load base model
base_model_name = "cahya/t5-base-indonesian-summarization-cased"
tokenizer = T5Tokenizer.from_pretrained(base_model_name)
base_model = T5ForConditionalGeneration.from_pretrained(
base_model_name,
load_in_8bit=True, # Quantize for CPU efficiency
device_map="auto"
)
model = PeftModel.from_pretrained(
base_model,
"reydeuss/trustify-t5-adapter",
)
return model, tokenizer
def summarize_text(text):
if not text.strip():
return "Please enter text to summarize."
model, tokenizer = load_model_once()
# Add T5 prefix
input_text = f"summarize: {text}"
inputs = tokenizer(input_text, return_tensors="pt", max_length=1024, truncation=True)
with torch.no_grad():
outputs = model.generate(
**inputs,
max_length=512,
num_beams=4,
length_penalty=2.0,
early_stopping=True,
no_repeat_ngram_size=2
)
summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
return summary
# Create Gradio interface
interface = gr.Interface(
fn=summarize_text,
inputs=gr.Textbox(lines=10, placeholder="Enter Indonesian text here...", label="Input Text"),
outputs=gr.Textbox(lines=5, label="Generated Summary"),
title="Indonesian Text Summarization",
description="Enter Indonesian text to generate a summary using T5 model with PEFT adapters",
)
interface.launch() |