Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,44 +2,47 @@ import gradio as gr
|
|
| 2 |
from transformers import pipeline
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
# 1. Configuration
|
| 6 |
MODEL_ID = "VoltIC/Automated-Text-Summarizer"
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
summarizer = pipeline(
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
# 3. Define the Summarization Function
|
| 20 |
def summarize_text(text):
|
| 21 |
-
if not text or len(text.strip()) <
|
| 22 |
-
return "
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
gr.
|
| 32 |
-
gr.
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
input_box = gr.Textbox(label="Input Text", placeholder="Paste here...", lines=10)
|
| 36 |
-
output_box = gr.Textbox(label="Summary", lines=5)
|
| 37 |
-
|
| 38 |
-
submit_btn = gr.Button("Summarize", variant="primary")
|
| 39 |
-
|
| 40 |
-
# Connect the button to the function
|
| 41 |
-
submit_btn.click(fn=summarize_text, inputs=input_box, outputs=output_box)
|
| 42 |
|
| 43 |
-
|
| 44 |
-
if __name__ == "__main__":
|
| 45 |
-
demo.launch()
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
import torch
|
| 4 |
|
|
|
|
| 5 |
MODEL_ID = "VoltIC/Automated-Text-Summarizer"
|
| 6 |
|
| 7 |
+
# Load the model with low_cpu_mem_usage to save RAM
|
| 8 |
+
print("π Initializing model...")
|
| 9 |
+
try:
|
| 10 |
+
summarizer = pipeline(
|
| 11 |
+
"summarization",
|
| 12 |
+
model=MODEL_ID,
|
| 13 |
+
subfolder="summarizer_model",
|
| 14 |
+
framework="pt",
|
| 15 |
+
device=-1 # Explicitly force CPU
|
| 16 |
+
)
|
| 17 |
+
print("β
System Ready")
|
| 18 |
+
except Exception as e:
|
| 19 |
+
print(f"β Initialization Error: {e}")
|
| 20 |
|
|
|
|
| 21 |
def summarize_text(text):
|
| 22 |
+
if not text or len(text.strip()) < 20:
|
| 23 |
+
return "Error: Text is too short."
|
| 24 |
|
| 25 |
+
try:
|
| 26 |
+
# TRUNCATION is the key.
|
| 27 |
+
# It prevents the model from trying to process 10,000 words at once.
|
| 28 |
+
result = summarizer(
|
| 29 |
+
text,
|
| 30 |
+
max_length=100,
|
| 31 |
+
min_length=30,
|
| 32 |
+
do_sample=False,
|
| 33 |
+
truncation=True # π This prevents memory crashes
|
| 34 |
+
)
|
| 35 |
+
return result[0]['summary_text']
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return f"Runtime Error: {str(e)}"
|
| 38 |
|
| 39 |
+
# Define the UI
|
| 40 |
+
with gr.Blocks() as demo:
|
| 41 |
+
gr.Markdown("# AI Summarizer")
|
| 42 |
+
input_box = gr.Textbox(label="Input", lines=5)
|
| 43 |
+
output_box = gr.Textbox(label="Result", lines=5)
|
| 44 |
+
btn = gr.Button("Summarize")
|
| 45 |
|
| 46 |
+
btn.click(fn=summarize_text, inputs=input_box, outputs=output_box)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
demo.launch()
|
|
|
|
|
|