Update app.py
Browse files
app.py
CHANGED
@@ -1,39 +1,22 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
-
try:
|
7 |
-
# Initialize the RAG model and tokenizer
|
8 |
-
tokenizer = RagTokenizer.from_pretrained("facebook/rag-token-nq")
|
9 |
-
retriever = RagRetriever.from_pretrained(
|
10 |
-
"facebook/rag-token-nq",
|
11 |
-
index_name="exact",
|
12 |
-
use_dummy_dataset=True,
|
13 |
-
trust_remote_code=True # Allows loading the required dataset script
|
14 |
-
)
|
15 |
-
model = RagSequenceForGeneration.from_pretrained("facebook/rag-token-nq")
|
16 |
-
|
17 |
-
# Tokenize the input text
|
18 |
-
inputs = tokenizer(txt, return_tensors="pt")
|
19 |
-
|
20 |
-
# Retrieve relevant documents using the retriever
|
21 |
-
retrieved_docs = retriever.retrieve(inputs["input_ids"])
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
#
|
27 |
-
summary =
|
28 |
-
|
29 |
-
return summary
|
30 |
except Exception as e:
|
31 |
st.error(f"An error occurred during summarization: {str(e)}")
|
32 |
return None
|
33 |
|
34 |
# Page title and layout
|
35 |
-
st.set_page_config(page_title='π¦π
|
36 |
-
st.title('π¦π
|
37 |
|
38 |
# Text input area for user to input text
|
39 |
txt_input = st.text_area('Enter your text', '', height=200)
|
@@ -43,16 +26,16 @@ response = None
|
|
43 |
with st.form('summarize_form', clear_on_submit=True):
|
44 |
submitted = st.form_submit_button('Submit')
|
45 |
if submitted and txt_input:
|
46 |
-
with st.spinner('Summarizing
|
47 |
-
response =
|
48 |
|
49 |
# Display the response if available
|
50 |
if response:
|
51 |
st.info(response)
|
52 |
|
53 |
-
# Instructions for
|
54 |
-
st.subheader("Hugging Face
|
55 |
st.write("""
|
56 |
-
This app uses Hugging Face's
|
57 |
-
|
58 |
""")
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
|
4 |
+
# Initialize summarization pipeline
|
5 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
# Function to generate response using the summarizer
|
8 |
+
def generate_response_with_summarizer(txt):
|
9 |
+
try:
|
10 |
+
# Generate summary
|
11 |
+
summary = summarizer(txt, max_length=130, min_length=30, do_sample=False)
|
12 |
+
return summary[0]['summary_text']
|
|
|
13 |
except Exception as e:
|
14 |
st.error(f"An error occurred during summarization: {str(e)}")
|
15 |
return None
|
16 |
|
17 |
# Page title and layout
|
18 |
+
st.set_page_config(page_title='π¦π Text Summarization App')
|
19 |
+
st.title('π¦π Text Summarization App')
|
20 |
|
21 |
# Text input area for user to input text
|
22 |
txt_input = st.text_area('Enter your text', '', height=200)
|
|
|
26 |
with st.form('summarize_form', clear_on_submit=True):
|
27 |
submitted = st.form_submit_button('Submit')
|
28 |
if submitted and txt_input:
|
29 |
+
with st.spinner('Summarizing...'):
|
30 |
+
response = generate_response_with_summarizer(txt_input)
|
31 |
|
32 |
# Display the response if available
|
33 |
if response:
|
34 |
st.info(response)
|
35 |
|
36 |
+
# Instructions for using the summarization app
|
37 |
+
st.subheader("Hugging Face Summarization")
|
38 |
st.write("""
|
39 |
+
This app uses Hugging Face's `facebook/bart-large-cnn` model to summarize input text.
|
40 |
+
The model provides concise summaries by capturing the main points of the text.
|
41 |
""")
|