Spaces:
Sleeping
Sleeping
Upload app.py
Browse filesuploading app file
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
+
|
| 5 |
+
# Load from Hugging Face Hub
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained("Adarsh921/indicbart-hindi-summarizer")
|
| 7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("Adarsh921/indicbart-hindi-summarizer")
|
| 8 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 9 |
+
model = model.to(device)
|
| 10 |
+
|
| 11 |
+
# Inference function
|
| 12 |
+
def generate_summary(text):
|
| 13 |
+
inputs = tokenizer(
|
| 14 |
+
text,
|
| 15 |
+
return_tensors="pt",
|
| 16 |
+
max_length=512,
|
| 17 |
+
truncation=True,
|
| 18 |
+
padding="max_length"
|
| 19 |
+
)
|
| 20 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 21 |
+
|
| 22 |
+
summary_ids = model.generate(
|
| 23 |
+
inputs["input_ids"],
|
| 24 |
+
num_beams=4,
|
| 25 |
+
max_length = 128,
|
| 26 |
+
min_length=30,
|
| 27 |
+
no_repeat_ngram_size=3,
|
| 28 |
+
early_stopping=True
|
| 29 |
+
)
|
| 30 |
+
return tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 31 |
+
|
| 32 |
+
# Gradio UI
|
| 33 |
+
gr.Interface(
|
| 34 |
+
fn=generate_summary,
|
| 35 |
+
inputs=gr.Textbox(lines=10, label="Paste Hindi Article"),
|
| 36 |
+
outputs=gr.Textbox(label="Generated Summary"),
|
| 37 |
+
title="Hindi Article Summarizer",
|
| 38 |
+
description="Summarizer fine-tuned on ILSUM 2024 using IndicBART"
|
| 39 |
+
).launch(share=True)
|