saima730 commited on
Commit
9f29f51
Β·
verified Β·
1 Parent(s): 267005e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -30
app.py CHANGED
@@ -1,35 +1,34 @@
1
  import streamlit as st
2
- import openai
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
- def generate_response(txt, openai_api_key):
 
9
  try:
10
- # Set up OpenAI API key
11
- openai.api_key = openai_api_key
 
 
12
 
13
- # Split text into chunks
14
- text_splitter = CharacterTextSplitter()
15
- texts = text_splitter.split_text(txt)
16
 
17
- # Create documents from the text chunks
18
- docs = [Document(page_content=t) for t in texts]
19
 
20
- # Instantiate the OpenAI LLM model
21
- llm = OpenAI(temperature=0, openai_api_key=openai_api_key)
22
 
23
- # Create the summarization chain and summarize the documents
24
- chain = load_summarize_chain(llm=llm, chain_type='map_reduce')
25
- return chain.run(docs)
 
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 openai_api_key.startswith('sk-'):
43
- with st.spinner('Summarizing...'):
44
- response = generate_response(txt_input, openai_api_key)
45
 
46
  # Display the response if available
47
  if response:
48
  st.info(response)
49
 
50
- # Instructions for getting an OpenAI API key
51
- st.subheader("Get an OpenAI API key")
52
- st.write("You can get your own OpenAI API key by following the instructions:")
53
  st.write("""
54
- 1. Go to [OpenAI API Keys](https://platform.openai.com/account/api-keys).
55
- 2. Click on the `+ Create new secret key` button.
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
  """)