Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
# Warning: Pipeline type "summarization" is no longer supported in transformers v5.
|
| 5 |
+
# You must load the model directly or downgrade to v4.x with:
|
| 6 |
+
# 'pip install "transformers<5.0.0'
|
| 7 |
+
from transformers import pipeline
|
| 8 |
+
|
| 9 |
+
text_summary = pipeline("summarization", model="Falconsai/text_summarization", dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32)
|
| 10 |
+
|
| 11 |
+
def summary(input_text):
|
| 12 |
+
if not input_text.strip():
|
| 13 |
+
return "Please enter document text to summarize."
|
| 14 |
+
|
| 15 |
+
# Approximate token count using words
|
| 16 |
+
input_tokens = len(input_text.split())
|
| 17 |
+
|
| 18 |
+
# Check if word count is less than 5
|
| 19 |
+
if input_tokens < 5:
|
| 20 |
+
return "Insufficient text to summarize."
|
| 21 |
+
|
| 22 |
+
# Proceed with summarization
|
| 23 |
+
output = text_summary(input_text, truncation=True)
|
| 24 |
+
summary_text = output[0]['summary_text']
|
| 25 |
+
|
| 26 |
+
# Calculate output tokens (approximate using word count)
|
| 27 |
+
output_tokens = len(summary_text.split())
|
| 28 |
+
|
| 29 |
+
# Debug print statement
|
| 30 |
+
print(f"Input tokens: {input_tokens}, Output tokens: {output_tokens}")
|
| 31 |
+
|
| 32 |
+
return summary_text
|
| 33 |
+
|
| 34 |
+
gr.close_all()
|
| 35 |
+
|
| 36 |
+
demo = gr.Interface(fn=summary,
|
| 37 |
+
inputs=[gr.Textbox(label="Input text to summarize", lines=8, placeholder="Paste document text here..." )],
|
| 38 |
+
outputs=[gr.Textbox(label="Summarized text", lines=4)],
|
| 39 |
+
title="AI Text summarization",
|
| 40 |
+
description="Powered by FalconsAI T5 Sequence-to-Sequence model.",
|
| 41 |
+
flagging_mode="never")
|
| 42 |
+
|
| 43 |
+
demo.launch()
|
| 44 |
+
# demo.launch(share=True) # Creates a public link
|