Adarsh921 commited on
Commit
b4fa544
·
verified ·
1 Parent(s): 2feb97e

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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/flan-t5-english-summarizer")
7
+ model = AutoModelForSeq2SeqLM.from_pretrained("Adarsh921/flan-t5-english-summarizer")
8
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
+ model = model.to(device)
10
+ MAX_INPUT_LEN = 768
11
+ MAX_TARGET_LEN = 150
12
+
13
+
14
+ def summarize(text):
15
+ inputs = tokenizer(
16
+ text,
17
+ return_tensors="pt",
18
+ truncation=True,
19
+ max_length=MAX_INPUT_LEN
20
+ ).to(device)
21
+
22
+ with torch.no_grad():
23
+ output = model.generate(
24
+ **inputs,
25
+ max_length=MAX_TARGET_LEN,
26
+ min_length=40,
27
+ num_beams=6,
28
+ no_repeat_ngram_size=3,
29
+ length_penalty=1.0,
30
+ early_stopping=True
31
+ )
32
+
33
+ return tokenizer.decode(output[0], skip_special_tokens=True).strip()
34
+
35
+ # Gradio UI
36
+ gr.Interface(
37
+ fn=summarize,
38
+ inputs=gr.Textbox(lines=10, label="Paste english Article"),
39
+ outputs=gr.Textbox(label="Generated Summary"),
40
+ title="English Article Summarizer",
41
+ description="Summarizer fine-tuned on ILSUM 2024 using Flan-T5"
42
+ ).launch(share=True)