Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +148 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,150 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
st.
|
| 34 |
-
.
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import textwrap
|
|
|
|
| 3 |
import streamlit as st
|
| 4 |
+
from typing import Optional
|
| 5 |
+
from utils import (
|
| 6 |
+
load_bitnet_model,
|
| 7 |
+
map_reduce_summarize,
|
| 8 |
+
)
|
| 9 |
|
| 10 |
+
# ---------- Page Config ----------
|
| 11 |
+
st.set_page_config(page_title="BitNet Summarizer", page_icon="📝", layout="wide")
|
| 12 |
+
|
| 13 |
+
st.title("📝 Text Summarizer — BitNet on Hugging Face Spaces")
|
| 14 |
+
st.caption(
|
| 15 |
+
"Open-source summarizer powered by **microsoft/bitnet-b1.58-2B-4T** with a map‑reduce strategy for long documents."
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
# ---------- Sidebar Controls ----------
|
| 19 |
+
with st.sidebar:
|
| 20 |
+
st.header("Engine")
|
| 21 |
+
engine = st.radio(
|
| 22 |
+
"Choose inference engine:",
|
| 23 |
+
options=["BitNet (local)", "HF Inference API (fallback)"],
|
| 24 |
+
index=0,
|
| 25 |
+
help="Local BitNet loads inside your Space. Fallback uses a hosted summarization model via HF Inference API.",
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
st.header("Generation Settings")
|
| 29 |
+
temperature = st.slider("temperature", 0.0, 1.5, 0.3, 0.05)
|
| 30 |
+
top_p = st.slider("top_p", 0.5, 1.0, 0.95, 0.01)
|
| 31 |
+
chunk_tokens = st.slider("chunk size (tokens)", 400, 1600, 900, 50)
|
| 32 |
+
chunk_overlap = st.slider("overlap (tokens)", 0, 200, 60, 5)
|
| 33 |
+
chunk_max_new = st.slider("chunk max_new_tokens", 32, 256, 128, 8)
|
| 34 |
+
final_max_new = st.slider("final max_new_tokens", 64, 512, 220, 8)
|
| 35 |
+
|
| 36 |
+
st.markdown("---")
|
| 37 |
+
st.subheader("HF Inference API Settings")
|
| 38 |
+
hf_token = st.text_input(
|
| 39 |
+
"HF_TOKEN (optional)",
|
| 40 |
+
type="password",
|
| 41 |
+
help="Personal access token with Inference API scope if you want to use the fallback engine.",
|
| 42 |
+
value=os.environ.get("HF_TOKEN", ""),
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# ---------- Input Area ----------
|
| 46 |
+
DEFAULT_TEXT = (
|
| 47 |
+
"The Hugging Face Spaces platform makes it simple to build and share machine learning apps. "
|
| 48 |
+
"This example demonstrates a map‑reduce summarization approach using an efficient BitNet model. "
|
| 49 |
+
"For longer documents, we split text into token chunks, summarize each piece, and merge the summaries "
|
| 50 |
+
"into a coherent final summary."
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
text = st.text_area(
|
| 54 |
+
"Paste your text here:",
|
| 55 |
+
value=DEFAULT_TEXT,
|
| 56 |
+
height=260,
|
| 57 |
+
help="Works with long documents via chunking. You can also try the sample text to see the pipeline.",
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
colA, colB = st.columns([1, 2])
|
| 61 |
+
with colA:
|
| 62 |
+
run = st.button("Summarize", type="primary")
|
| 63 |
+
with colB:
|
| 64 |
+
st.write("")
|
| 65 |
+
|
| 66 |
+
# ---------- Inference API Fallback ----------
|
| 67 |
+
# Lightweight helper using huggingface_hub's InferenceClient
|
| 68 |
+
from huggingface_hub import InferenceClient
|
| 69 |
+
|
| 70 |
+
def summarize_via_hf_api(text: str, token: str) -> Optional[str]:
|
| 71 |
+
try:
|
| 72 |
+
client = InferenceClient(token=token)
|
| 73 |
+
# A small, instruction‑tuned summarizer works well as fallback
|
| 74 |
+
# DistilBART CNN is common; switch to any hosted summarization model you prefer
|
| 75 |
+
model = "sshleifer/distilbart-cnn-12-6"
|
| 76 |
+
out = client.text_generation(
|
| 77 |
+
model=model,
|
| 78 |
+
prompt=(
|
| 79 |
+
"Summarize the following text in 3-6 concise sentences, preserving key facts and avoiding hallucinations.\n\n" + text
|
| 80 |
+
),
|
| 81 |
+
max_new_tokens=220,
|
| 82 |
+
temperature=0.3,
|
| 83 |
+
top_p=0.95,
|
| 84 |
+
)
|
| 85 |
+
return out
|
| 86 |
+
except Exception as e:
|
| 87 |
+
st.error(f"HF Inference API error: {e}")
|
| 88 |
+
return None
|
| 89 |
+
|
| 90 |
+
# ---------- Main Action ----------
|
| 91 |
+
if run:
|
| 92 |
+
if not text.strip():
|
| 93 |
+
st.warning("Please paste some text to summarize.")
|
| 94 |
+
st.stop()
|
| 95 |
+
|
| 96 |
+
if engine.startswith("HF Inference API"):
|
| 97 |
+
if not hf_token.strip():
|
| 98 |
+
st.error("Please provide an HF_TOKEN to use the Inference API fallback.")
|
| 99 |
+
st.stop()
|
| 100 |
+
with st.spinner("Calling HF Inference API…"):
|
| 101 |
+
summary = summarize_via_hf_api(text, hf_token)
|
| 102 |
+
if summary:
|
| 103 |
+
st.success("Done!")
|
| 104 |
+
st.markdown("### Summary")
|
| 105 |
+
st.write(summary)
|
| 106 |
+
st.stop()
|
| 107 |
+
|
| 108 |
+
# Local BitNet path
|
| 109 |
+
info_box = st.empty()
|
| 110 |
+
info_box.info(
|
| 111 |
+
"Loading BitNet model. On CPU this can take several minutes on first run; subsequent runs are cached."
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
@st.cache_resource(show_spinner=False)
|
| 115 |
+
def _load():
|
| 116 |
+
return load_bitnet_model()
|
| 117 |
+
|
| 118 |
+
tok, model = _load()
|
| 119 |
+
info_box.empty()
|
| 120 |
+
|
| 121 |
+
with st.spinner("Summarizing with BitNet (map‑reduce)…"):
|
| 122 |
+
summary = map_reduce_summarize(
|
| 123 |
+
text=text,
|
| 124 |
+
tokenizer=tok,
|
| 125 |
+
model=model,
|
| 126 |
+
max_chunk_tokens=chunk_tokens,
|
| 127 |
+
overlap=chunk_overlap,
|
| 128 |
+
chunk_max_new_tokens=chunk_max_new,
|
| 129 |
+
final_max_new_tokens=final_max_new,
|
| 130 |
+
temperature=temperature,
|
| 131 |
+
top_p=top_p,
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
st.success("Done!")
|
| 135 |
+
st.markdown("### Summary")
|
| 136 |
+
st.write(summary)
|
| 137 |
+
|
| 138 |
+
with st.expander("Debug / details"):
|
| 139 |
+
st.markdown(
|
| 140 |
+
"- **Engine:** BitNet (local) \n"
|
| 141 |
+
f"- **chunk size:** {chunk_tokens} tokens, **overlap:** {chunk_overlap} tokens \n"
|
| 142 |
+
f"- **temperature:** {temperature}, **top_p:** {top_p} \n"
|
| 143 |
+
f"- **chunk max_new_tokens:** {chunk_max_new}, **final max_new_tokens:** {final_max_new}"
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
st.markdown("---")
|
| 147 |
+
st.caption(
|
| 148 |
+
"Built with Streamlit + Transformers + Hugging Face Hub. Model: microsoft/bitnet-b1.58-2B-4T.\n"
|
| 149 |
+
"Tip: Select a GPU in Space settings for faster startup."
|
| 150 |
+
)
|