Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
os.system('pip install streamlit transformers torch')
|
| 3 |
+
|
| 4 |
+
import streamlit as st
|
| 5 |
+
from transformers import BartTokenizer, BartForConditionalGeneration
|
| 6 |
+
|
| 7 |
+
# Load the model and tokenizer
|
| 8 |
+
model_name = 'ahmadrocks/facebook_bart_base_new'
|
| 9 |
+
|
| 10 |
+
tokenizer = BartTokenizer.from_pretrained(model_name)
|
| 11 |
+
model = BartForConditionalGeneration.from_pretrained(model_name)
|
| 12 |
+
|
| 13 |
+
def summarize_text(text):
|
| 14 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding="longest")
|
| 15 |
+
summary_ids = model.generate(
|
| 16 |
+
inputs["input_ids"],
|
| 17 |
+
max_length=150,
|
| 18 |
+
min_length=30,
|
| 19 |
+
length_penalty=2.0,
|
| 20 |
+
num_beams=4,
|
| 21 |
+
early_stopping=True
|
| 22 |
+
)
|
| 23 |
+
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 24 |
+
return summary
|
| 25 |
+
|
| 26 |
+
st.title("Text Summarization with Fine-Tuned Model")
|
| 27 |
+
st.write("Enter text to generate a summary using the fine-tuned summarization model.")
|
| 28 |
+
|
| 29 |
+
text = st.text_area("Input Text", height=200)
|
| 30 |
+
if st.button("Summarize"):
|
| 31 |
+
if text:
|
| 32 |
+
with st.spinner("Summarizing..."):
|
| 33 |
+
summary = summarize_text(text)
|
| 34 |
+
st.success("Summary Generated")
|
| 35 |
+
st.write(summary)
|
| 36 |
+
else:
|
| 37 |
+
st.warning("Please enter some text to summarize.")
|
| 38 |
+
|
| 39 |
+
if _name_ == "_main_":
|
| 40 |
+
st.set_option('deprecation.showfileUploaderEncoding', False)
|
| 41 |
+
st.markdown(
|
| 42 |
+
"""
|
| 43 |
+
<style>
|
| 44 |
+
.reportview-container {
|
| 45 |
+
flex-direction: row;
|
| 46 |
+
justify-content: center.
|
| 47 |
+
}
|
| 48 |
+
</style>
|
| 49 |
+
""",
|
| 50 |
+
unsafe_allow_html=True
|
| 51 |
+
)
|