File size: 1,393 Bytes
3fec680 ff636ec d339557 99ceffb d339557 ff636ec d339557 99ceffb 3fec680 ff636ec c4cfe6e 28f633d c4cfe6e 35a3d3b c4cfe6e 3fec680 ff636ec 99ceffb ff636ec |
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 |
import gradio as gr
import torch
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
# Load model and tokenizer explicitly
model_name = "facebook/mbart-large-50"
tokenizer = AutoTokenizer.from_pretrained(model_name, src_lang="ne_NP")
model = AutoModelForSeq2SeqLM.from_pretrained(
model_name,
device_map="auto",
low_cpu_mem_usage=True
)
summarizer = pipeline(
"summarization",
model=model,
tokenizer=tokenizer
)
def summarize_text(text):
try:
if not text.strip():
return "Please enter some Nepali text to summarize"
summary = summarizer(
text,
max_length=1000,
min_length=30,
truncation=True,
# Directly pass forced_bos_token_id here
forced_bos_token_id=tokenizer.lang_code_to_id["ne_NP"]
)[0]['summary_text']
return summary
except Exception as e:
return f"Error during summarization: {str(e)}"
iface = gr.Interface(
fn=summarize_text,
inputs=gr.Textbox(lines=5, label="Nepali Text to Summarize"),
outputs=gr.Textbox(lines=5, label="Summary"),
title="Nepali Text Summarizer",
description="Enter Nepali text and get a concise summary using multilingual NLP models.",
allow_flagging="never"
)
if __name__ == "__main__":
iface.launch(server_name="0.0.0.0", server_port=7860) |