ashirhashmi commited on
Commit
f6b9930
·
verified ·
1 Parent(s): b44731b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -39
app.py CHANGED
@@ -1,40 +1,48 @@
1
  import streamlit as st
2
- from transformers import pipeline, BartForConditionalGeneration, BartTokenizer
3
-
4
- # Load pre-trained BART model and tokenizer
5
- model_name = "facebook/bart-large-cnn" # BART large model for summarization
6
- model = BartForConditionalGeneration.from_pretrained(model_name)
7
- tokenizer = BartTokenizer.from_pretrained(model_name)
8
-
9
- # Define function to generate summary
10
- def generate_summary(text):
11
- inputs = tokenizer([text], max_length=1024, return_tensors='pt')
12
-
13
- # Generate summary
14
- summary_ids = model.generate(
15
- inputs['input_ids'],
16
- max_length=150,
17
- num_beams=4,
18
- length_penalty=2.0,
19
- early_stopping=True
20
- )
21
-
22
- # Decode and return summary
23
- generated_summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
24
- return generated_summary
25
-
26
- # Streamlit app
27
- def main():
28
- st.title("Summarization App")
29
-
30
- # Sidebar input for text
31
- text = st.sidebar.text_area("Enter text to summarize", "Enter your text here...")
32
-
33
- # Generate button
34
- if st.sidebar.button("Generate Summary"):
35
- summary = generate_summary(text)
36
- st.subheader("Generated Summary:")
37
- st.write(summary)
38
-
39
- if __name__ == "__main__":
40
- main()
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import torch
3
+ from transformers import pipeline
4
+ from transformers import BartTokenizer, BartForConditionalGeneration
5
+
6
+ # Replace with your Hugging Face model repository path
7
+ model_repo_path = 'ASaboor/Bart_samsum'
8
+
9
+ # Load the model and tokenizer
10
+ model = BartForConditionalGeneration.from_pretrained(model_repo_path)
11
+ tokenizer = BartTokenizer.from_pretrained(model_repo_path)
12
+
13
+ # Initialize the summarization pipeline
14
+ summarizer = pipeline('summarization', model=model, tokenizer=tokenizer)
15
+
16
+ # Streamlit app layout
17
+ st.set_page_config(page_title="Text Summarization App", page_icon=":memo:", layout="wide")
18
+
19
+ st.title("Text Summarization App")
20
+ st.write("""
21
+ This app uses a fine-tuned BART model to generate summaries of your input text.
22
+ Enter the text you want to summarize in the box below and click "Summarize" to see the result.
23
+ """)
24
+
25
+ # User input
26
+ text_input = st.text_area("Enter text to summarize", height=300, placeholder="Paste your text here...")
27
+
28
+ # Summarize the text
29
+ if st.button("Summarize"):
30
+ if text_input:
31
+ with st.spinner("Generating summary..."):
32
+ try:
33
+ # Generate summary
34
+ summary = summarizer(text_input, max_length=150, min_length=30, do_sample=False)
35
+
36
+ # Display summary
37
+ st.subheader("Summary")
38
+ st.write(summary[0]['summary_text'])
39
+ except Exception as e:
40
+ st.error(f"An error occurred during summarization: {e}")
41
+ else:
42
+ st.warning("Please enter some text to summarize.")
43
+
44
+ # Optional: Add a footer or additional information
45
+ st.markdown("""
46
+ ---
47
+ Made with ❤️ using [Streamlit](https://streamlit.io) and [Hugging Face Transformers](https://huggingface.co/transformers/).
48
+ """)