Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,45 @@
|
|
| 1 |
-
import
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
|
| 4 |
-
# 1.
|
| 5 |
-
st.set_page_config(page_title="AI Summarizer", page_icon="π")
|
| 6 |
-
|
| 7 |
-
st.title("π Automated Text Summarizer")
|
| 8 |
-
|
| 9 |
-
# 2. Configuration
|
| 10 |
MODEL_ID = "VoltIC/Automated-Text-Summarizer"
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
-
# 3.
|
| 23 |
-
|
| 24 |
-
if
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
st.session_state.model_ready = True
|
| 33 |
-
status.update(label="β
Model Loaded!", state="complete", expanded=False)
|
| 34 |
-
st.rerun()
|
| 35 |
-
except Exception as e:
|
| 36 |
-
st.error("Model is still transferring from Hub. Refreshing in 30 seconds...")
|
| 37 |
-
st.stop()
|
| 38 |
-
else:
|
| 39 |
-
# 4. The actual interface (only shows once model_ready is True)
|
| 40 |
-
summarizer = load_summarizer()
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
| 1 |
+
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 |
+
# 2. Load the model once at startup
|
| 9 |
+
# Gradio handles the 'waiting' state better for the user
|
| 10 |
+
print("π Loading BART model from subfolder... this may take a minute.")
|
| 11 |
+
summarizer = pipeline(
|
| 12 |
+
"summarization",
|
| 13 |
+
model=MODEL_ID,
|
| 14 |
+
subfolder="summarizer_model",
|
| 15 |
+
framework="pt"
|
| 16 |
+
)
|
| 17 |
+
print("β
Model loaded and ready!")
|
| 18 |
|
| 19 |
+
# 3. Define the Summarization Function
|
| 20 |
+
def summarize_text(text):
|
| 21 |
+
if not text or len(text.strip()) < 50:
|
| 22 |
+
return "Please enter a longer text (at least 50 characters)."
|
| 23 |
+
|
| 24 |
+
# max_length=100 generally results in 2-3 sentences
|
| 25 |
+
result = summarizer(text, max_length=100, min_length=30, do_sample=False)
|
| 26 |
+
return result[0]['summary_text']
|
| 27 |
|
| 28 |
+
# 4. Build the Gradio Interface
|
| 29 |
+
# 'article' theme is clean and professional
|
| 30 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 31 |
+
gr.Markdown("# π Automated Text Summarizer")
|
| 32 |
+
gr.Markdown("Paste a long article below to get a concise 2-3 sentence summary.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
with gr.Row():
|
| 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 |
+
# 5. Launch the app
|
| 44 |
+
if __name__ == "__main__":
|
| 45 |
+
demo.launch()
|