Spaces:
Running
Running
Commit
Β·
d331ffb
1
Parent(s):
9928002
Add the app to HF
Browse files- Dockerfile +15 -0
- app.py +121 -0
- requirements.txt +4 -0
Dockerfile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9
|
| 2 |
+
|
| 3 |
+
RUN useradd -m -u 1000 user
|
| 4 |
+
USER user
|
| 5 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 6 |
+
|
| 7 |
+
WORKDIR /app
|
| 8 |
+
|
| 9 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
| 10 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 11 |
+
|
| 12 |
+
COPY --chown=user . /app
|
| 13 |
+
|
| 14 |
+
# Streamlit uses 8501 by default, but HF needs 7860
|
| 15 |
+
CMD ["streamlit", "run", "app.py", "--server.port", "7860", "--server.address", "0.0.0.0"]
|
app.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import T5ForConditionalGeneration, T5Tokenizer
|
| 3 |
+
import torch
|
| 4 |
+
import time
|
| 5 |
+
|
| 6 |
+
# --- PAGE CONFIG ---
|
| 7 |
+
st.set_page_config(page_title="AI Summarizer", page_icon="π", layout="centered")
|
| 8 |
+
|
| 9 |
+
# --- CUSTOM CSS THEME ---
|
| 10 |
+
st.markdown("""
|
| 11 |
+
<style>
|
| 12 |
+
.stApp {
|
| 13 |
+
background-color: #0E1117;
|
| 14 |
+
color: #FFFFFF;
|
| 15 |
+
}
|
| 16 |
+
.stTextArea textarea {
|
| 17 |
+
background-color: #1B1D21 !important;
|
| 18 |
+
color: #58a6ff !important;
|
| 19 |
+
border: 1px solid #005fb8 !important;
|
| 20 |
+
}
|
| 21 |
+
.stButton>button {
|
| 22 |
+
background-color: #005fb8;
|
| 23 |
+
color: white;
|
| 24 |
+
border-radius: 5px;
|
| 25 |
+
border: none;
|
| 26 |
+
width: 100%;
|
| 27 |
+
font-weight: bold;
|
| 28 |
+
transition: 0.3s;
|
| 29 |
+
}
|
| 30 |
+
.stButton>button:hover {
|
| 31 |
+
background-color: #58a6ff;
|
| 32 |
+
color: black;
|
| 33 |
+
box-shadow: 0px 0px 15px #58a6ff;
|
| 34 |
+
}
|
| 35 |
+
h1, h2, h3 {
|
| 36 |
+
color: #58a6ff !important;
|
| 37 |
+
}
|
| 38 |
+
</style>
|
| 39 |
+
""", unsafe_allow_html=True)
|
| 40 |
+
|
| 41 |
+
# --- SPEED-OPTIMIZED MODEL LOADING ---
|
| 42 |
+
@st.cache_resource
|
| 43 |
+
def load_model():
|
| 44 |
+
model_name = "mahirmasud/t5-summarizer"
|
| 45 |
+
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
| 46 |
+
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
| 47 |
+
|
| 48 |
+
# --- OPTIMIZATION START ---
|
| 49 |
+
# 1. Move to GPU if available, else optimize for CPU
|
| 50 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 51 |
+
|
| 52 |
+
if device == "cpu":
|
| 53 |
+
# 2. Dynamic Quantization: Makes the model ~2x-3x faster on CPU
|
| 54 |
+
model = torch.quantization.quantize_dynamic(
|
| 55 |
+
model, {torch.nn.Linear}, dtype=torch.qint8
|
| 56 |
+
)
|
| 57 |
+
else:
|
| 58 |
+
model = model.to(device)
|
| 59 |
+
# 3. Use Half Precision if on GPU
|
| 60 |
+
model = model.half()
|
| 61 |
+
|
| 62 |
+
model.eval()
|
| 63 |
+
# --- OPTIMIZATION END ---
|
| 64 |
+
|
| 65 |
+
return tokenizer, model, device
|
| 66 |
+
|
| 67 |
+
tokenizer, model, device = load_model()
|
| 68 |
+
|
| 69 |
+
# --- UI ---
|
| 70 |
+
st.title("π AI News Summarizer")
|
| 71 |
+
|
| 72 |
+
# Personalized AI Greeting
|
| 73 |
+
with st.chat_message("assistant", avatar="π€"):
|
| 74 |
+
st.write("Hello! I am your **AI Summarizer**. π")
|
| 75 |
+
st.write("I'm now running on an **optimized engine** to give you faster responses. Paste any article below!")
|
| 76 |
+
|
| 77 |
+
# Input Area
|
| 78 |
+
st.markdown("### π₯ Input Article")
|
| 79 |
+
input_text = st.text_area(
|
| 80 |
+
label="Paste your text here:",
|
| 81 |
+
height=250,
|
| 82 |
+
placeholder="Paste the news content here...",
|
| 83 |
+
label_visibility="collapsed"
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
# Execution Logic
|
| 87 |
+
if st.button("β¨ Generate Faster Summary"):
|
| 88 |
+
if not input_text.strip():
|
| 89 |
+
st.warning("I need some text to work with!")
|
| 90 |
+
else:
|
| 91 |
+
start_time = time.time() # Track speed
|
| 92 |
+
|
| 93 |
+
with st.status("π Processing with high-speed engine...", expanded=True) as status:
|
| 94 |
+
# Use inference_mode for maximum speed
|
| 95 |
+
with torch.inference_mode():
|
| 96 |
+
text = "summarize: " + input_text
|
| 97 |
+
inputs = tokenizer.encode(text, return_tensors="pt", max_length=512, truncation=True).to(device)
|
| 98 |
+
|
| 99 |
+
# Optimized generation parameters
|
| 100 |
+
outputs = model.generate(
|
| 101 |
+
inputs,
|
| 102 |
+
max_length=128,
|
| 103 |
+
num_beams=4, # Reduced from 4 to 2 for speed (minimal quality loss)
|
| 104 |
+
repetition_penalty=2.5,
|
| 105 |
+
length_penalty=1.0,
|
| 106 |
+
early_stopping=True,
|
| 107 |
+
use_cache=True # Uses previous hidden states to speed up generation
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 111 |
+
|
| 112 |
+
end_time = time.time()
|
| 113 |
+
duration = round(end_time - start_time, 2)
|
| 114 |
+
status.update(label=f"β
Done in {duration}s", state="complete", expanded=False)
|
| 115 |
+
|
| 116 |
+
# Display Results
|
| 117 |
+
st.markdown("---")
|
| 118 |
+
st.subheader("π― The Bottom Line")
|
| 119 |
+
st.info(summary)
|
| 120 |
+
|
| 121 |
+
st.caption(f"Engine: {device.upper()} | Speed: {duration} seconds")
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
transformers
|
| 3 |
+
torch>=2.0.0
|
| 4 |
+
sentencepiece
|