ar08 commited on
Commit
3c0c86a
·
verified ·
1 Parent(s): ceb3d78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -14
app.py CHANGED
@@ -1,28 +1,38 @@
 
 
1
  import gradio as gr
2
- from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
 
 
3
 
4
- # Load the model and tokenizer
5
- model_id = "Rahmat82/t5-small-finetuned-summarization-xsum"
6
- model = AutoModelForSeq2SeqLM.from_pretrained(model_id)
7
- tokenizer = AutoTokenizer.from_pretrained(model_id, use_fast=True)
8
 
9
- # Create the summarization pipeline
10
- summarizer = pipeline("summarization", model=model, tokenizer=tokenizer)
11
 
12
- # Define the summarization function
13
  def summarize_text(text):
14
- if len(text.strip()) == 0:
15
  return "Please enter some text."
16
- summary = summarizer(text)[0]["summary_text"]
17
- return summary
 
 
 
 
 
 
18
 
19
- # Gradio Interface
20
  app = gr.Interface(
21
  fn=summarize_text,
22
  inputs=gr.Textbox(lines=15, placeholder="Paste your text here...", label="Input Text"),
23
  outputs=gr.Textbox(label="Summary"),
24
- title="Custom T5 Summarizer 📝",
25
- description="Summarize long texts using a fine-tuned T5-small model from Hugging Face"
26
  )
27
 
28
  # Launch the app
 
1
+ # Required: pip install gradio transformers accelerate optimum onnxruntime onnx
2
+
3
  import gradio as gr
4
+ from transformers import AutoTokenizer
5
+ from optimum.onnxruntime import ORTModelForSeq2SeqLM
6
+ from optimum.pipelines import pipeline
7
 
8
+ # Load ONNX model and tokenizer
9
+ model_name = "Rahmat82/t5-small-finetuned-summarization-xsum"
10
+ model = ORTModelForSeq2SeqLM.from_pretrained(model_name, export=True)
11
+ tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
12
 
13
+ # Create summarizer pipeline with Optimum
14
+ summarizer = pipeline("summarization", model=model, tokenizer=tokenizer, device_map="auto", batch_size=12)
15
 
16
+ # Define summarization function with 1024 token cap
17
  def summarize_text(text):
18
+ if not text.strip():
19
  return "Please enter some text."
20
+
21
+ # Tokenize and truncate to max 1024 tokens
22
+ inputs = tokenizer(text, return_tensors="pt", max_length=1024, truncation=True)
23
+ input_text = tokenizer.decode(inputs["input_ids"][0], skip_special_tokens=True)
24
+
25
+ # Summarize the truncated text
26
+ result = summarizer(input_text)
27
+ return result[0]["summary_text"]
28
 
29
+ # Gradio app
30
  app = gr.Interface(
31
  fn=summarize_text,
32
  inputs=gr.Textbox(lines=15, placeholder="Paste your text here...", label="Input Text"),
33
  outputs=gr.Textbox(label="Summary"),
34
+ title="🚀 ONNX-Powered T5 Summarizer (1024 tokens)",
35
+ description="Summarize long text using a fine-tuned ONNX-accelerated T5-small model (max input: 1024 tokens)"
36
  )
37
 
38
  # Launch the app