Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,45 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline, AutoModelForSeq2SeqLM, AutoTokenizer
|
|
|
|
| 3 |
|
| 4 |
-
# 1.
|
| 5 |
MODEL_ID = "VoltIC/Automated-Text-Summarizer"
|
| 6 |
SUBFOLDER = "summarizer_model"
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, subfolder=SUBFOLDER)
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
| 23 |
|
|
|
|
| 24 |
def summarize_text(text):
|
| 25 |
-
if not text
|
| 26 |
-
return "
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
max_length=100,
|
| 32 |
-
min_length=30,
|
| 33 |
-
do_sample=False,
|
| 34 |
-
truncation=True
|
| 35 |
-
)
|
| 36 |
-
return result[0]['summary_text']
|
| 37 |
-
except Exception as e:
|
| 38 |
-
return f"Runtime Error: {str(e)}"
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
btn.click(fn=summarize_text, inputs=input_box, outputs=output_box)
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline, AutoModelForSeq2SeqLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
# 1. Setup
|
| 6 |
MODEL_ID = "VoltIC/Automated-Text-Summarizer"
|
| 7 |
SUBFOLDER = "summarizer_model"
|
| 8 |
|
| 9 |
+
# 2. Initialization (No 'try' block here so we can see the real error in logs)
|
| 10 |
+
print("π Phase 1: Loading Tokenizer...")
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, subfolder=SUBFOLDER)
|
| 12 |
|
| 13 |
+
print("π Phase 2: Loading Model Weights (1.6GB)...")
|
| 14 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_ID, subfolder=SUBFOLDER)
|
|
|
|
| 15 |
|
| 16 |
+
print("π Phase 3: Building Pipeline...")
|
| 17 |
+
# This defines the 'summarizer' variable globally
|
| 18 |
+
summarizer = pipeline(
|
| 19 |
+
"summarization",
|
| 20 |
+
model=model,
|
| 21 |
+
tokenizer=tokenizer,
|
| 22 |
+
framework="pt",
|
| 23 |
+
device=-1 # Forces CPU
|
| 24 |
+
)
|
| 25 |
+
print("β
SUCCESS: Summarizer is now defined!")
|
| 26 |
|
| 27 |
+
# 3. Logic
|
| 28 |
def summarize_text(text):
|
| 29 |
+
if not text:
|
| 30 |
+
return "Please enter text."
|
| 31 |
|
| 32 |
+
# We use the global 'summarizer' defined above
|
| 33 |
+
results = summarizer(text, max_length=100, min_length=30, truncation=True)
|
| 34 |
+
return results[0]['summary_text']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
# 4. Interface
|
| 37 |
+
demo = gr.Interface(
|
| 38 |
+
fn=summarize_text,
|
| 39 |
+
inputs=gr.Textbox(lines=10, label="Input Text"),
|
| 40 |
+
outputs=gr.Textbox(label="Summary"),
|
| 41 |
+
title="AI Text Summarizer"
|
| 42 |
+
)
|
|
|
|
| 43 |
|
| 44 |
+
if __name__ == "__main__":
|
| 45 |
+
demo.launch()
|