mynkchaudhry commited on
Commit
ddfce1f
·
verified ·
1 Parent(s): cb4dd2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -20
app.py CHANGED
@@ -1,36 +1,34 @@
1
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
2
  import gradio as gr
3
 
4
- # Load model and tokenizer
5
- tokenizer = AutoTokenizer.from_pretrained("mynkchaudhry/Summarization-Pro",force_download=True,cache_dir=None)
6
- model = AutoModelForSeq2SeqLM.from_pretrained("mynkchaudhry/Summarization-Pro",force_download=True,cache_dir=None)
7
 
8
- def summarize(text):
9
- inputs = tokenizer(text, return_tensors="pt", max_length=1024, truncation=True)
10
- summary_ids = model.generate(
11
- inputs.input_ids,
12
- max_length=1500, # Increased max_length for longer summaries
13
- min_length=100, # Ensures summaries are sufficiently long
14
- length_penalty=0.9, # Encourages longer summaries but balances precision
15
- num_beams=6, # More beams for better exploration of possible summaries
16
- early_stopping=True
17
- )
18
  summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
19
  return summary
20
 
21
  # Gradio interface
22
  interface = gr.Interface(
23
  fn=summarize,
24
- inputs=gr.Textbox(lines=10, label="Input Text"),
 
 
 
25
  outputs=gr.Textbox(label="Summary"),
26
  examples=[
27
- ["The quick brown fox jumps over the lazy dog. This sentence is an English-language pangram—a sentence that contains all the letters of the English alphabet. It is often used for testing typewriters, keyboards, and computer fonts."],
28
- ["Artificial intelligence (AI) is intelligence demonstrated by machines, in contrast to the natural intelligence displayed by humans and animals. Leading AI textbooks define the field as the study of intelligent agents: any device that perceives its environment and takes actions that maximize its chance of successfully achieving its goals."],
29
- ["In data science, an anomaly is an observation that deviates significantly from other observations. Anomalies are also known as outliers, novelties, noise, deviations, and exceptions. Anomaly detection finds application in many domains, including fraud detection, network security, and fault detection."]
30
  ],
31
  title="Text Summarization",
32
- description="Generate a summary from the input text using a pre-trained summarization model.",
33
- theme="freddyaboulton/dracula_revamped" # Ensure this theme exists or use a default theme
34
  )
35
 
36
  interface.launch()
 
1
+ from transformers import PegasusTokenizer, PegasusForConditionalGeneration
2
  import gradio as gr
3
 
4
+ # Load model and tokenizer based on the provided configuration
5
+ tokenizer = PegasusTokenizer.from_pretrained("google/pegasus-cnn_dailymail")
6
+ model = PegasusForConditionalGeneration.from_pretrained("google/pegasus-cnn_dailymail")
7
 
8
+ def summarize(text, prompt):
9
+ # Prepend the prompt to the input text
10
+ inputs = tokenizer(prompt + " " + text, return_tensors="pt", max_length=1024, truncation=True)
11
+ # Generate the summary
12
+ summary_ids = model.generate(inputs.input_ids, max_length=128, min_length=32, length_penalty=0.8, num_beams=8, early_stopping=True)
 
 
 
 
 
13
  summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
14
  return summary
15
 
16
  # Gradio interface
17
  interface = gr.Interface(
18
  fn=summarize,
19
+ inputs=[
20
+ gr.Textbox(lines=10, label="Input Text"),
21
+ gr.Textbox(lines=2, label="Prompt", placeholder="Enter a prompt for the summary here...")
22
+ ],
23
  outputs=gr.Textbox(label="Summary"),
24
  examples=[
25
+ ["The quick brown fox jumps over the lazy dog. This sentence is an English-language pangram—a sentence that contains all the letters of the English alphabet. It is often used for testing typewriters, keyboards, and computer fonts.", "Summarize this text:"],
26
+ ["Artificial intelligence (AI) is intelligence demonstrated by machines, in contrast to the natural intelligence displayed by humans and animals. Leading AI textbooks define the field as the study of intelligent agents: any device that perceives its environment and takes actions that maximize its chance of successfully achieving its goals.", "Summarize this text:"],
27
+ ["In data science, an anomaly is an observation that deviates significantly from other observations. Anomalies are also known as outliers, novelties, noise, deviations, and exceptions. Anomaly detection finds application in many domains, including fraud detection, network security, and fault detection.", "Summarize this text:"]
28
  ],
29
  title="Text Summarization",
30
+ description="Generate a summary from the input text using a pre-trained PEGASUS model. Optionally, provide a prompt to guide the summarization.",
31
+ theme="freddyaboulton/dracula_revamped"
32
  )
33
 
34
  interface.launch()