saima730 commited on
Commit
af547e1
Β·
verified Β·
1 Parent(s): 1a75c78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -34
app.py CHANGED
@@ -1,39 +1,22 @@
1
  import streamlit as st
2
- from transformers import RagTokenizer, RagRetriever, RagSequenceForGeneration
3
 
4
- # Function to generate response using RAG (Retrieval-Augmented Generation)
5
- def generate_response_with_rag(txt):
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
- # Generate the output using RAG
24
- generated = model.generate(input_ids=inputs["input_ids"], context_input_ids=retrieved_docs['context_input_ids'])
25
-
26
- # Decode the generated text
27
- summary = tokenizer.decode(generated[0], skip_special_tokens=True)
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='πŸ¦œπŸ”— RAG Text Summarization App')
36
- st.title('πŸ¦œπŸ”— RAG Text Summarization App')
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 with RAG...'):
47
- response = generate_response_with_rag(txt_input)
48
 
49
  # Display the response if available
50
  if response:
51
  st.info(response)
52
 
53
- # Instructions for getting started with Hugging Face models
54
- st.subheader("Hugging Face RAG Summarization")
55
  st.write("""
56
- This app uses Hugging Face's RAG model (Retrieval-Augmented Generation) to generate summaries with relevant external context.
57
- RAG retrieves information from a set of documents and combines that with a generative model to produce more accurate summaries.
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
  """)