Update app.py
Browse files
app.py
CHANGED
@@ -1,35 +1,34 @@
|
|
1 |
import streamlit as st
|
2 |
-
import
|
3 |
-
from langchain.docstore.document import Document
|
4 |
-
from langchain.text_splitter import CharacterTextSplitter
|
5 |
-
from langchain.chains.summarize import load_summarize_chain
|
6 |
-
from langchain.llms import OpenAI
|
7 |
|
8 |
-
|
|
|
9 |
try:
|
10 |
-
#
|
11 |
-
|
|
|
|
|
12 |
|
13 |
-
#
|
14 |
-
|
15 |
-
texts = text_splitter.split_text(txt)
|
16 |
|
17 |
-
#
|
18 |
-
|
19 |
|
20 |
-
#
|
21 |
-
|
22 |
|
23 |
-
#
|
24 |
-
|
25 |
-
|
|
|
26 |
except Exception as e:
|
27 |
st.error(f"An error occurred during summarization: {str(e)}")
|
28 |
return None
|
29 |
|
30 |
# Page title and layout
|
31 |
-
st.set_page_config(page_title='π¦π Text Summarization App')
|
32 |
-
st.title('π¦π Text Summarization App')
|
33 |
|
34 |
# Text input area for user to input text
|
35 |
txt_input = st.text_area('Enter your text', '', height=200)
|
@@ -37,21 +36,18 @@ txt_input = st.text_area('Enter your text', '', height=200)
|
|
37 |
# Form to accept the user's text input for summarization
|
38 |
response = None
|
39 |
with st.form('summarize_form', clear_on_submit=True):
|
40 |
-
openai_api_key = st.text_input('OpenAI API Key', type='password', disabled=not txt_input)
|
41 |
submitted = st.form_submit_button('Submit')
|
42 |
-
if submitted and
|
43 |
-
with st.spinner('Summarizing...'):
|
44 |
-
response =
|
45 |
|
46 |
# Display the response if available
|
47 |
if response:
|
48 |
st.info(response)
|
49 |
|
50 |
-
# Instructions for getting
|
51 |
-
st.subheader("
|
52 |
-
st.write("You can get your own OpenAI API key by following the instructions:")
|
53 |
st.write("""
|
54 |
-
|
55 |
-
|
56 |
-
3. Enter an identifier name (optional) and click on the `Create secret key` button.
|
57 |
""")
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import RagTokenizer, RagRetriever, RagSequenceForGeneration, pipeline
|
|
|
|
|
|
|
|
|
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("facebook/rag-token-nq", index_name="exact", use_dummy_dataset=True)
|
10 |
+
model = RagSequenceForGeneration.from_pretrained("facebook/rag-token-nq")
|
11 |
|
12 |
+
# Tokenize the input text
|
13 |
+
inputs = tokenizer(txt, return_tensors="pt")
|
|
|
14 |
|
15 |
+
# Retrieve relevant documents using the retriever
|
16 |
+
retrieved_docs = retriever.retrieve(inputs["input_ids"])
|
17 |
|
18 |
+
# Generate the output using RAG
|
19 |
+
generated = model.generate(input_ids=inputs["input_ids"], context_input_ids=retrieved_docs['context_input_ids'])
|
20 |
|
21 |
+
# Decode the generated text
|
22 |
+
summary = tokenizer.decode(generated[0], skip_special_tokens=True)
|
23 |
+
|
24 |
+
return summary
|
25 |
except Exception as e:
|
26 |
st.error(f"An error occurred during summarization: {str(e)}")
|
27 |
return None
|
28 |
|
29 |
# Page title and layout
|
30 |
+
st.set_page_config(page_title='π¦π RAG Text Summarization App')
|
31 |
+
st.title('π¦π RAG Text Summarization App')
|
32 |
|
33 |
# Text input area for user to input text
|
34 |
txt_input = st.text_area('Enter your text', '', height=200)
|
|
|
36 |
# Form to accept the user's text input for summarization
|
37 |
response = None
|
38 |
with st.form('summarize_form', clear_on_submit=True):
|
|
|
39 |
submitted = st.form_submit_button('Submit')
|
40 |
+
if submitted and txt_input:
|
41 |
+
with st.spinner('Summarizing with RAG...'):
|
42 |
+
response = generate_response_with_rag(txt_input)
|
43 |
|
44 |
# Display the response if available
|
45 |
if response:
|
46 |
st.info(response)
|
47 |
|
48 |
+
# Instructions for getting started with Hugging Face models
|
49 |
+
st.subheader("Hugging Face RAG Summarization")
|
|
|
50 |
st.write("""
|
51 |
+
This app uses Hugging Face's RAG model (Retrieval-Augmented Generation) to generate summaries with relevant external context.
|
52 |
+
RAG retrieves information from a set of documents and combines that with a generative model to produce more accurate summaries.
|
|
|
53 |
""")
|