Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +123 -115
src/streamlit_app.py
CHANGED
|
@@ -1,140 +1,148 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import os
|
| 3 |
|
| 4 |
-
# Set page config
|
| 5 |
st.set_page_config(
|
| 6 |
page_title="T5-small LoRA Summarization",
|
| 7 |
page_icon="π",
|
| 8 |
layout="wide"
|
| 9 |
)
|
| 10 |
|
| 11 |
-
|
| 12 |
-
st.markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
]
|
| 25 |
-
|
| 26 |
-
for file in files:
|
| 27 |
-
if os.path.exists(file):
|
| 28 |
-
st.write(f"β
`{file}`")
|
| 29 |
-
else:
|
| 30 |
-
st.write(f"β `{file}`")
|
| 31 |
-
|
| 32 |
-
st.info(f"**Status:** {sum(1 for f in files if os.path.exists(f))}/{len(files)} files present")
|
| 33 |
|
| 34 |
-
|
| 35 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
# Load your model
|
| 46 |
-
summarizer = pipeline(
|
| 47 |
-
"summarization",
|
| 48 |
-
model="manesh1/t5-small-lora-summarization"
|
| 49 |
-
)
|
| 50 |
-
|
| 51 |
-
# Generate summary
|
| 52 |
-
text = \"\"\"Your long text here...\"\"\"
|
| 53 |
-
summary = summarizer(text, max_length=150, min_length=30)[0]['summary_text']
|
| 54 |
-
print(summary)
|
| 55 |
-
```
|
| 56 |
-
""")
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
def query(payload):
|
| 68 |
-
response = requests.post(API_URL, headers=headers, json=payload)
|
| 69 |
-
return response.json()
|
| 70 |
-
|
| 71 |
-
output = query({
|
| 72 |
-
"inputs": "Your text to summarize...",
|
| 73 |
-
"parameters": {"max_length": 150, "min_length": 30}
|
| 74 |
-
})
|
| 75 |
-
```
|
| 76 |
-
""")
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
```python
|
| 82 |
-
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 83 |
-
|
| 84 |
-
# Load from local files
|
| 85 |
-
tokenizer = AutoTokenizer.from_pretrained(".")
|
| 86 |
-
model = AutoModelForSeq2SeqLM.from_pretrained(".")
|
| 87 |
-
|
| 88 |
-
# Your inference code here
|
| 89 |
-
```
|
| 90 |
-
""")
|
| 91 |
|
| 92 |
-
|
| 93 |
-
st.
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
| 99 |
)
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
with col3:
|
| 108 |
-
st.metric("Paragraphs", len([p for p in demo_text.split('\n') if p.strip()]))
|
| 109 |
-
|
| 110 |
-
# Show a preview
|
| 111 |
-
words = demo_text.split()
|
| 112 |
-
if len(words) > 30:
|
| 113 |
-
preview = " ".join(words[:30]) + "..."
|
| 114 |
-
st.write("**First 30 words:**", preview)
|
| 115 |
|
| 116 |
-
|
| 117 |
-
st.subheader("π§ Next Steps")
|
| 118 |
st.markdown("""
|
| 119 |
-
|
|
|
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
3. **Test the model** in a Python environment with PyTorch installed
|
| 124 |
-
4. **Create a separate inference Space** using Gradio or another framework
|
| 125 |
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
""")
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
""")
|
| 138 |
|
| 139 |
-
|
| 140 |
-
main()
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
|
|
|
|
| 3 |
st.set_page_config(
|
| 4 |
page_title="T5-small LoRA Summarization",
|
| 5 |
page_icon="π",
|
| 6 |
layout="wide"
|
| 7 |
)
|
| 8 |
|
| 9 |
+
# Custom CSS
|
| 10 |
+
st.markdown("""
|
| 11 |
+
<style>
|
| 12 |
+
.model-card {
|
| 13 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 14 |
+
padding: 2rem;
|
| 15 |
+
border-radius: 10px;
|
| 16 |
+
color: white;
|
| 17 |
+
margin-bottom: 2rem;
|
| 18 |
+
}
|
| 19 |
+
.feature-box {
|
| 20 |
+
background-color: #f8f9fa;
|
| 21 |
+
padding: 1rem;
|
| 22 |
+
border-radius: 8px;
|
| 23 |
+
border-left: 4px solid #667eea;
|
| 24 |
+
margin: 0.5rem 0;
|
| 25 |
+
}
|
| 26 |
+
</style>
|
| 27 |
+
""", unsafe_allow_html=True)
|
| 28 |
|
| 29 |
+
# Header
|
| 30 |
+
st.markdown("""
|
| 31 |
+
<div class="model-card">
|
| 32 |
+
<h1>π T5-small LoRA Summarization Model</h1>
|
| 33 |
+
<p>Fine-tuned for efficient text summarization using LoRA adapters</p>
|
| 34 |
+
</div>
|
| 35 |
+
""", unsafe_allow_html=True)
|
| 36 |
+
|
| 37 |
+
# Model Information
|
| 38 |
+
col1, col2 = st.columns([2, 1])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
with col1:
|
| 41 |
+
st.header("π Model Overview")
|
| 42 |
+
st.markdown("""
|
| 43 |
+
This model is a **T5-small** architecture fine-tuned with **LoRA (Low-Rank Adaptation)**
|
| 44 |
+
specifically for text summarization tasks. The model maintains the efficiency of T5-small
|
| 45 |
+
while being optimized for summarization through parameter-efficient fine-tuning.
|
| 46 |
+
""")
|
| 47 |
|
| 48 |
+
st.markdown("""
|
| 49 |
+
<div class="feature-box">
|
| 50 |
+
<h4>π Model Files Status</h4>
|
| 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 |
+
with col2:
|
| 62 |
+
st.header("π§ Quick Use")
|
| 63 |
+
st.code("""
|
| 64 |
+
from transformers import pipeline
|
| 65 |
+
|
| 66 |
+
summarizer = pipeline(
|
| 67 |
+
"summarization",
|
| 68 |
+
model="manesh1/t5-small-lora-summarization"
|
| 69 |
+
)
|
| 70 |
+
""", language="python")
|
| 71 |
+
|
| 72 |
+
# Usage Examples
|
| 73 |
+
st.header("π‘ Usage Examples")
|
| 74 |
+
tab1, tab2, tab3 = st.tabs(["Basic Usage", "Advanced", "API"])
|
| 75 |
+
|
| 76 |
+
with tab1:
|
| 77 |
+
st.markdown("""
|
| 78 |
+
```python
|
| 79 |
+
from transformers import pipeline
|
| 80 |
|
| 81 |
+
# Load model
|
| 82 |
+
summarizer = pipeline(
|
| 83 |
+
"summarization",
|
| 84 |
+
model="manesh1/t5-small-lora-summarization"
|
| 85 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
+
# Summarize text
|
| 88 |
+
text = \"\"\"Long document text here...\"\"\"
|
| 89 |
+
summary = summarizer(
|
| 90 |
+
text,
|
| 91 |
+
max_length=150,
|
| 92 |
+
min_length=30,
|
| 93 |
+
do_sample=False
|
| 94 |
+
)[0]['summary_text']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
+
print(summary)
|
| 97 |
+
```
|
| 98 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
+
with tab2:
|
| 101 |
+
st.markdown("""
|
| 102 |
+
```python
|
| 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 tab3:
|
|
|
|
| 121 |
st.markdown("""
|
| 122 |
+
```python
|
| 123 |
+
import requests
|
| 124 |
|
| 125 |
+
API_URL = "https://api-inference.huggingface.co/models/manesh1/t5-small-lora-summarization"
|
| 126 |
+
headers = {"Authorization": "Bearer YOUR_TOKEN"}
|
|
|
|
|
|
|
| 127 |
|
| 128 |
+
def query(payload):
|
| 129 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 130 |
+
return response.json()
|
| 131 |
+
|
| 132 |
+
output = query({
|
| 133 |
+
"inputs": "Your text here...",
|
| 134 |
+
"parameters": {"max_length": 150, "min_length": 30}
|
| 135 |
+
})
|
| 136 |
+
```
|
| 137 |
""")
|
| 138 |
|
| 139 |
+
# Final notes
|
| 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 |
+
st.success("**Your model is ready to use!** Share this link: https://huggingface.co/manesh1/t5-small-lora-summarization")
|
|
|