app.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import gradio as gr
  2. from transformers import T5ForConditionalGeneration, T5Tokenizer
  3. def summarize_text(text):
  4. # Preprocess the text
  5. inputs = tokenizer.encode(
  6. "summarize: " + text,
  7. return_tensors='pt',
  8. max_length=512,
  9. truncation=True,
  10. padding='max_length'
  11. )
  12. # Generate the summary
  13. summary_ids = model.generate(
  14. inputs,
  15. max_length=50,
  16. num_beams=5,
  17. # early_stopping=True
  18. )
  19. # Decode and return the summary
  20. return tokenizer.decode(summary_ids[0], skip_special_tokens=True)
  21. model_path = 'results_t5base/checkpoint-4450' # the path where you saved your model
  22. model = T5ForConditionalGeneration.from_pretrained(model_path)
  23. tokenizer = T5Tokenizer.from_pretrained('results_t5base')
  24. interface = gr.Interface(
  25. fn=summarize_text,
  26. inputs=gr.Textbox(lines=10, placeholder='Enter Text Here...', label='Input text'),
  27. outputs=gr.Textbox(label='Summarized Text'),
  28. title='Text Summarizer using T5'
  29. )
  30. interface.launch()