Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +97 -78
src/streamlit_app.py
CHANGED
|
@@ -1,78 +1,97 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
st.
|
| 63 |
-
|
| 64 |
-
"
|
| 65 |
-
|
| 66 |
-
"
|
| 67 |
-
|
| 68 |
-
"
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForCausalLM, pipeline
|
| 5 |
+
from huggingface_hub import InferenceClient
|
| 6 |
+
|
| 7 |
+
# Cache model loading to avoid re-download on every run
|
| 8 |
+
@st.cache_resource
|
| 9 |
+
def load_model(model_name):
|
| 10 |
+
"""
|
| 11 |
+
Load the specified model and tokenizer. Returns a transformers pipeline for summarization or text generation.
|
| 12 |
+
"""
|
| 13 |
+
if model_name == "microsoft/bitnet-b1.58-2B-4T":
|
| 14 |
+
# Load BitNet model (causal LM) and tokenizer
|
| 15 |
+
dtype = torch.float32 # use float32 on CPU
|
| 16 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
|
| 17 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=dtype, device_map="auto")
|
| 18 |
+
# Create a text-generation pipeline for BitNet
|
| 19 |
+
gen_pipeline = pipeline(
|
| 20 |
+
"text-generation",
|
| 21 |
+
model=model, tokenizer=tokenizer,
|
| 22 |
+
max_new_tokens=256, # default max summary length
|
| 23 |
+
temperature=0.2, # a low temperature for more focused output
|
| 24 |
+
pad_token_id=tokenizer.eos_token_id
|
| 25 |
+
)
|
| 26 |
+
return gen_pipeline, tokenizer # return tokenizer as well for prompt preparation
|
| 27 |
+
else:
|
| 28 |
+
# For seq2seq models like T5 or BART, use summarization pipeline
|
| 29 |
+
summarizer = pipeline("summarization", model=model_name, tokenizer=model_name, device=-1)
|
| 30 |
+
return summarizer, None
|
| 31 |
+
|
| 32 |
+
# Set page configuration
|
| 33 |
+
st.set_page_config(page_title="Text Summarizer", page_icon="🤖")
|
| 34 |
+
st.title("📃 Text Summarizer")
|
| 35 |
+
|
| 36 |
+
# Model selection: local models and an option for Hugging Face API
|
| 37 |
+
model_options = [
|
| 38 |
+
"t5-small",
|
| 39 |
+
"facebook/bart-large-cnn",
|
| 40 |
+
"microsoft/bitnet-b1.58-2B-4T",
|
| 41 |
+
"Use Hugging Face Inference API (bart-large-cnn)"
|
| 42 |
+
]
|
| 43 |
+
model_choice = st.selectbox("Choose a summarization model:", model_options,
|
| 44 |
+
help="Select a model to use for generating the summary. 'Inference API' will call a hosted model via Hugging Face.")
|
| 45 |
+
|
| 46 |
+
# Input methods: Text area and File uploader
|
| 47 |
+
text_input = st.text_area("Enter text to summarize (English only):", height=200)
|
| 48 |
+
uploaded_file = st.file_uploader("...or upload a text file", type=["txt"])
|
| 49 |
+
if uploaded_file is not None:
|
| 50 |
+
# If a file is uploaded, read it (assuming UTF-8 text file)
|
| 51 |
+
try:
|
| 52 |
+
file_content = uploaded_file.read().decode("utf-8")
|
| 53 |
+
except Exception:
|
| 54 |
+
file_content = uploaded_file.read().decode("latin-1") # fallback decoding
|
| 55 |
+
text_to_summarize = file_content
|
| 56 |
+
else:
|
| 57 |
+
text_to_summarize = text_input
|
| 58 |
+
|
| 59 |
+
# Button to generate summary
|
| 60 |
+
if st.button("Summarize"):
|
| 61 |
+
if not text_to_summarize or text_to_summarize.strip() == "":
|
| 62 |
+
st.warning("Please provide some text (or upload a file) to summarize.")
|
| 63 |
+
else:
|
| 64 |
+
st.write("Generating summary...")
|
| 65 |
+
# Local model inference
|
| 66 |
+
if model_choice != "Use Hugging Face Inference API (bart-large-cnn)":
|
| 67 |
+
summarizer_pipeline, tok = load_model(model_choice)
|
| 68 |
+
if model_choice == "microsoft/bitnet-b1.58-2B-4T":
|
| 69 |
+
# Prepare BitNet prompt for summarization
|
| 70 |
+
prompt = (
|
| 71 |
+
"Summarize the text below in 2-3 concise sentences focusing on key facts and implications.\n"
|
| 72 |
+
f"Text:\n{ {text_to_summarize} }\nSummary:"
|
| 73 |
+
)
|
| 74 |
+
# Use the text-generation pipeline to complete the prompt
|
| 75 |
+
result = summarizer_pipeline(prompt, max_new_tokens=200, do_sample=False)[0]["generated_text"]
|
| 76 |
+
# The pipeline returns the full prompt + completion; extract only after 'Summary:'
|
| 77 |
+
summary = result.split("Summary:")[-1].strip()
|
| 78 |
+
else:
|
| 79 |
+
# For T5 or BART, use the summarization pipeline directly
|
| 80 |
+
summary = summarizer_pipeline(text_to_summarize, max_length=150, min_length=30, do_sample=False)[0]["summary_text"]
|
| 81 |
+
st.subheader("Summary")
|
| 82 |
+
st.write(summary)
|
| 83 |
+
else:
|
| 84 |
+
# Use Hugging Face Inference API with a hosted model (bart-large-cnn)
|
| 85 |
+
hf_token = os.getenv("HF_TOKEN") or (st.secrets["HF_TOKEN"] if "HF_TOKEN" in st.secrets else None)
|
| 86 |
+
client = InferenceClient(model="facebook/bart-large-cnn", token=hf_token)
|
| 87 |
+
# Call the summarization API
|
| 88 |
+
try:
|
| 89 |
+
result = client.summarization(text_to_summarize)
|
| 90 |
+
# The result is an object with `summary_text` attribute (or dict with 'summary_text')
|
| 91 |
+
summary_text = result.summary_text if hasattr(result, "summary_text") else result["summary_text"]
|
| 92 |
+
except Exception as e:
|
| 93 |
+
st.error(f"Error using Inference API: {e}")
|
| 94 |
+
summary_text = None
|
| 95 |
+
if summary_text:
|
| 96 |
+
st.subheader("Summary")
|
| 97 |
+
st.write(summary_text)
|