Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +238 -92
src/streamlit_app.py
CHANGED
|
@@ -1,5 +1,8 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
| 2 |
|
|
|
|
| 3 |
st.set_page_config(
|
| 4 |
page_title="T5-small LoRA Summarization",
|
| 5 |
page_icon="π",
|
|
@@ -23,6 +26,13 @@ st.markdown("""
|
|
| 23 |
border-left: 4px solid #667eea;
|
| 24 |
margin: 0.5rem 0;
|
| 25 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
</style>
|
| 27 |
""", unsafe_allow_html=True)
|
| 28 |
|
|
@@ -34,115 +44,251 @@ st.markdown("""
|
|
| 34 |
</div>
|
| 35 |
""", unsafe_allow_html=True)
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
<ul>
|
| 52 |
-
<li>β
adapter_config.json</li>
|
| 53 |
-
<li>β
adapter_model.safetensors</li>
|
| 54 |
-
<li>β
tokenizer files</li>
|
| 55 |
-
<li>β
configuration files</li>
|
| 56 |
-
</ul>
|
| 57 |
-
<p><strong>All model files are properly deployed!</strong></p>
|
| 58 |
-
</div>
|
| 59 |
-
""", unsafe_allow_html=True)
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
from transformers import pipeline
|
| 65 |
|
| 66 |
summarizer = pipeline(
|
| 67 |
"summarization",
|
| 68 |
model="manesh1/t5-small-lora-summarization"
|
| 69 |
)
|
| 70 |
-
|
| 71 |
|
| 72 |
-
#
|
| 73 |
-
st.header("π‘ Usage Examples")
|
| 74 |
-
tab1, tab2, tab3 = st.tabs(["Basic Usage", "Advanced", "API"])
|
| 75 |
-
|
| 76 |
-
with tab1:
|
| 77 |
st.markdown("""
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
#
|
| 82 |
-
|
| 83 |
-
"
|
| 84 |
-
|
|
|
|
|
|
|
| 85 |
)
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
min_length=30,
|
| 93 |
-
do_sample=False
|
| 94 |
-
)[0]['summary_text']
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
-
|
| 101 |
-
st.
|
| 102 |
-
|
| 103 |
-
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 104 |
-
|
| 105 |
-
# Load specific components
|
| 106 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 107 |
-
"manesh1/t5-small-lora-summarization"
|
| 108 |
-
)
|
| 109 |
-
model = AutoModelForSeq2SeqLM.from_pretrained(
|
| 110 |
-
"manesh1/t5-small-lora-summarization"
|
| 111 |
-
)
|
| 112 |
-
|
| 113 |
-
# Custom inference
|
| 114 |
-
inputs = tokenizer("summarize: " + your_text, return_tensors="pt")
|
| 115 |
-
outputs = model.generate(**inputs)
|
| 116 |
-
summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 117 |
-
```
|
| 118 |
-
""")
|
| 119 |
|
| 120 |
-
with
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
"
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
""")
|
| 138 |
|
| 139 |
-
|
| 140 |
-
st.header("π Notes")
|
| 141 |
-
st.info("""
|
| 142 |
-
**About this Space**: This Streamlit interface demonstrates your deployed model.
|
| 143 |
-
While the model files are fully available in this Space, running inference requires
|
| 144 |
-
a PyTorch environment. Use the code examples above in your local environment or
|
| 145 |
-
in a Space with PyTorch support to test the model's summarization capabilities.
|
| 146 |
-
""")
|
| 147 |
|
| 148 |
-
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
import requests
|
| 4 |
|
| 5 |
+
# Set page config
|
| 6 |
st.set_page_config(
|
| 7 |
page_title="T5-small LoRA Summarization",
|
| 8 |
page_icon="π",
|
|
|
|
| 26 |
border-left: 4px solid #667eea;
|
| 27 |
margin: 0.5rem 0;
|
| 28 |
}
|
| 29 |
+
.try-section {
|
| 30 |
+
background: linear-gradient(135deg, #ff6b6b 0%, #ee5a24 100%);
|
| 31 |
+
padding: 2rem;
|
| 32 |
+
border-radius: 10px;
|
| 33 |
+
color: white;
|
| 34 |
+
margin: 2rem 0;
|
| 35 |
+
}
|
| 36 |
</style>
|
| 37 |
""", unsafe_allow_html=True)
|
| 38 |
|
|
|
|
| 44 |
</div>
|
| 45 |
""", unsafe_allow_html=True)
|
| 46 |
|
| 47 |
+
def try_direct_loading():
|
| 48 |
+
"""Try to load the model directly"""
|
| 49 |
+
try:
|
| 50 |
+
from transformers import pipeline
|
| 51 |
+
summarizer = pipeline(
|
| 52 |
+
"summarization",
|
| 53 |
+
model="manesh1/t5-small-lora-summarization"
|
| 54 |
+
)
|
| 55 |
+
return summarizer, "direct"
|
| 56 |
+
except Exception as e:
|
| 57 |
+
return None, f"Direct loading failed: {str(e)}"
|
| 58 |
|
| 59 |
+
def try_local_loading():
|
| 60 |
+
"""Try to load from local files"""
|
| 61 |
+
try:
|
| 62 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
| 63 |
+
# Load from current directory
|
| 64 |
+
tokenizer = AutoTokenizer.from_pretrained(".")
|
| 65 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(".")
|
| 66 |
+
summarizer = pipeline("summarization", model=model, tokenizer=tokenizer)
|
| 67 |
+
return summarizer, "local"
|
| 68 |
+
except Exception as e:
|
| 69 |
+
return None, f"Local loading failed: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
def main():
|
| 72 |
+
# Model Information
|
| 73 |
+
col1, col2 = st.columns([2, 1])
|
| 74 |
+
|
| 75 |
+
with col1:
|
| 76 |
+
st.header("π Model Overview")
|
| 77 |
+
st.markdown("""
|
| 78 |
+
This model is a **T5-small** architecture fine-tuned with **LoRA (Low-Rank Adaptation)**
|
| 79 |
+
specifically for text summarization tasks. The model maintains the efficiency of T5-small
|
| 80 |
+
while being optimized for summarization through parameter-efficient fine-tuning.
|
| 81 |
+
""")
|
| 82 |
+
|
| 83 |
+
st.markdown("""
|
| 84 |
+
<div class="feature-box">
|
| 85 |
+
<h4>π Model Files Status</h4>
|
| 86 |
+
<ul>
|
| 87 |
+
<li>β
adapter_config.json</li>
|
| 88 |
+
<li>β
adapter_model.safetensors</li>
|
| 89 |
+
<li>β
tokenizer files</li>
|
| 90 |
+
<li>β
configuration files</li>
|
| 91 |
+
</ul>
|
| 92 |
+
<p><strong>All model files are properly deployed!</strong></p>
|
| 93 |
+
</div>
|
| 94 |
+
""", unsafe_allow_html=True)
|
| 95 |
+
|
| 96 |
+
with col2:
|
| 97 |
+
st.header("π§ Quick Use")
|
| 98 |
+
st.code("""
|
| 99 |
from transformers import pipeline
|
| 100 |
|
| 101 |
summarizer = pipeline(
|
| 102 |
"summarization",
|
| 103 |
model="manesh1/t5-small-lora-summarization"
|
| 104 |
)
|
| 105 |
+
""", language="python")
|
| 106 |
|
| 107 |
+
# TRY INFERENCE SECTION
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
st.markdown("""
|
| 109 |
+
<div class="try-section">
|
| 110 |
+
<h2>π§ͺ Try the Model Here!</h2>
|
| 111 |
+
<p>Enter text below to test summarization</p>
|
| 112 |
+
</div>
|
| 113 |
+
""", unsafe_allow_html=True)
|
| 114 |
|
| 115 |
+
# Text input for summarization
|
| 116 |
+
input_text = st.text_area(
|
| 117 |
+
"Enter text to summarize:",
|
| 118 |
+
height=200,
|
| 119 |
+
placeholder="Paste your text here to see the model in action...",
|
| 120 |
+
key="input_text"
|
| 121 |
)
|
| 122 |
|
| 123 |
+
col1, col2 = st.columns(2)
|
| 124 |
+
with col1:
|
| 125 |
+
max_length = st.slider("Maximum summary length", 50, 300, 150)
|
| 126 |
+
with col2:
|
| 127 |
+
min_length = st.slider("Minimum summary length", 10, 100, 30)
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
+
if st.button("π Generate Summary", type="primary", use_container_width=True):
|
| 130 |
+
if not input_text.strip():
|
| 131 |
+
st.warning("Please enter some text to summarize.")
|
| 132 |
+
else:
|
| 133 |
+
with st.spinner("Attempting to load model and generate summary..."):
|
| 134 |
+
# Try different loading methods
|
| 135 |
+
summarizer, method = try_direct_loading()
|
| 136 |
+
|
| 137 |
+
if summarizer is None:
|
| 138 |
+
# Try local loading
|
| 139 |
+
summarizer, method = try_local_loading()
|
| 140 |
+
|
| 141 |
+
if summarizer:
|
| 142 |
+
st.success(f"β
Model loaded successfully via {method} method!")
|
| 143 |
+
try:
|
| 144 |
+
# Generate summary
|
| 145 |
+
result = summarizer(
|
| 146 |
+
input_text,
|
| 147 |
+
max_length=max_length,
|
| 148 |
+
min_length=min_length,
|
| 149 |
+
do_sample=False
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
summary = result[0]['summary_text']
|
| 153 |
+
|
| 154 |
+
st.subheader("π Generated Summary")
|
| 155 |
+
st.success(summary)
|
| 156 |
+
|
| 157 |
+
# Statistics
|
| 158 |
+
col1, col2, col3 = st.columns(3)
|
| 159 |
+
with col1:
|
| 160 |
+
st.metric("Input Words", len(input_text.split()))
|
| 161 |
+
with col2:
|
| 162 |
+
st.metric("Summary Words", len(summary.split()))
|
| 163 |
+
with col3:
|
| 164 |
+
reduction = ((len(input_text.split()) - len(summary.split())) / len(input_text.split())) * 100
|
| 165 |
+
st.metric("Reduction", f"{reduction:.1f}%")
|
| 166 |
+
|
| 167 |
+
except Exception as e:
|
| 168 |
+
st.error(f"Error during summarization: {str(e)}")
|
| 169 |
+
else:
|
| 170 |
+
st.warning("""
|
| 171 |
+
**Could not load the model in this environment.**
|
| 172 |
+
|
| 173 |
+
This is common on Hugging Face Spaces due to PyTorch limitations.
|
| 174 |
+
However, your model works perfectly in other environments!
|
| 175 |
+
|
| 176 |
+
**Try these alternatives:**
|
| 177 |
+
- Use the code examples in your local Python environment
|
| 178 |
+
- Create a new Space with Gradio interface
|
| 179 |
+
- Use the Hugging Face Inference API
|
| 180 |
+
""")
|
| 181 |
+
|
| 182 |
+
# Show what would happen in a working environment
|
| 183 |
+
st.info("**In a working environment, your input would be summarized like this:**")
|
| 184 |
+
words = input_text.split()
|
| 185 |
+
if len(words) > 30:
|
| 186 |
+
demo_summary = " ".join(words[:20]) + "... [summary continues]"
|
| 187 |
+
st.write(demo_summary)
|
| 188 |
|
| 189 |
+
# Usage Examples
|
| 190 |
+
st.header("π‘ Usage Examples")
|
| 191 |
+
tab1, tab2, tab3 = st.tabs(["Basic Usage", "Advanced", "API"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
+
with tab1:
|
| 194 |
+
st.markdown("""
|
| 195 |
+
```python
|
| 196 |
+
from transformers import pipeline
|
| 197 |
+
|
| 198 |
+
# Load model
|
| 199 |
+
summarizer = pipeline(
|
| 200 |
+
"summarization",
|
| 201 |
+
model="manesh1/t5-small-lora-summarization"
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
+
# Summarize text
|
| 205 |
+
text = \"\"\"Artificial intelligence (AI) is intelligence demonstrated by machines,
|
| 206 |
+
as opposed to natural intelligence displayed by animals including humans.
|
| 207 |
+
Leading AI textbooks define the field as the study of intelligent agents...\"\"\"
|
| 208 |
+
|
| 209 |
+
summary = summarizer(
|
| 210 |
+
text,
|
| 211 |
+
max_length=150,
|
| 212 |
+
min_length=30,
|
| 213 |
+
do_sample=False
|
| 214 |
+
)[0]['summary_text']
|
| 215 |
|
| 216 |
+
print("Summary:", summary)
|
| 217 |
+
```
|
| 218 |
+
""")
|
| 219 |
+
|
| 220 |
+
with tab2:
|
| 221 |
+
st.markdown("""
|
| 222 |
+
```python
|
| 223 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 224 |
+
|
| 225 |
+
# Load specific components
|
| 226 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 227 |
+
"manesh1/t5-small-lora-summarization"
|
| 228 |
+
)
|
| 229 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(
|
| 230 |
+
"manesh1/t5-small-lora-summarization"
|
| 231 |
+
)
|
| 232 |
+
|
| 233 |
+
# Custom inference
|
| 234 |
+
def summarize_text(text, max_length=150):
|
| 235 |
+
inputs = tokenizer("summarize: " + text,
|
| 236 |
+
return_tensors="pt",
|
| 237 |
+
max_length=512,
|
| 238 |
+
truncation=True)
|
| 239 |
+
|
| 240 |
+
outputs = model.generate(
|
| 241 |
+
**inputs,
|
| 242 |
+
max_length=max_length,
|
| 243 |
+
min_length=30,
|
| 244 |
+
num_beams=4,
|
| 245 |
+
early_stopping=True
|
| 246 |
+
)
|
| 247 |
+
|
| 248 |
+
summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 249 |
+
return summary
|
| 250 |
+
|
| 251 |
+
# Usage
|
| 252 |
+
text = "Your long text here..."
|
| 253 |
+
summary = summarize_text(text)
|
| 254 |
+
print(summary)
|
| 255 |
+
```
|
| 256 |
+
""")
|
| 257 |
+
|
| 258 |
+
with tab3:
|
| 259 |
+
st.markdown("""
|
| 260 |
+
```python
|
| 261 |
+
import requests
|
| 262 |
+
|
| 263 |
+
API_URL = "https://api-inference.huggingface.co/models/manesh1/t5-small-lora-summarization"
|
| 264 |
+
headers = {"Authorization": "Bearer YOUR_HF_TOKEN"}
|
| 265 |
+
|
| 266 |
+
def query(payload):
|
| 267 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 268 |
+
return response.json()
|
| 269 |
+
|
| 270 |
+
output = query({
|
| 271 |
+
"inputs": "Your text here...",
|
| 272 |
+
"parameters": {
|
| 273 |
+
"max_length": 150,
|
| 274 |
+
"min_length": 30,
|
| 275 |
+
"do_sample": False
|
| 276 |
+
}
|
| 277 |
+
})
|
| 278 |
+
|
| 279 |
+
print(output[0]['summary_text'])
|
| 280 |
+
```
|
| 281 |
+
""")
|
| 282 |
+
|
| 283 |
+
# Final notes
|
| 284 |
+
st.header("π Notes")
|
| 285 |
+
st.info("""
|
| 286 |
+
**About this Space**: This interface provides multiple ways to use your model.
|
| 287 |
+
The direct inference might work depending on the environment's PyTorch availability.
|
| 288 |
+
Your model files are complete and ready for use in any compatible environment!
|
| 289 |
""")
|
| 290 |
|
| 291 |
+
st.success("**Your model is ready to use!** Share this link: https://huggingface.co/manesh1/t5-small-lora-summarization")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
|
| 293 |
+
if __name__ == "__main__":
|
| 294 |
+
main()
|