saima730 commited on
Commit
267005e
Β·
verified Β·
1 Parent(s): 5e2e02c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -10
app.py CHANGED
@@ -3,42 +3,47 @@ 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
 
7
  def generate_response(txt, openai_api_key):
8
  try:
9
  # Set up OpenAI API key
10
  openai.api_key = openai_api_key
11
 
12
- # Split text
13
  text_splitter = CharacterTextSplitter()
14
  texts = text_splitter.split_text(txt)
15
 
16
- # Create multiple documents
17
  docs = [Document(page_content=t) for t in texts]
18
 
19
- # Text summarization using langchain's summarization chain
20
- chain = load_summarize_chain(llm="openai", chain_type='map_reduce')
 
 
 
21
  return chain.run(docs)
22
  except Exception as e:
23
  st.error(f"An error occurred during summarization: {str(e)}")
24
  return None
25
 
26
- # Page title
27
  st.set_page_config(page_title='πŸ¦œπŸ”— Text Summarization App')
28
  st.title('πŸ¦œπŸ”— Text Summarization App')
29
 
30
- # Text input
31
  txt_input = st.text_area('Enter your text', '', height=200)
32
 
33
- # Form to accept user's text input for summarization
34
  response = None
35
  with st.form('summarize_form', clear_on_submit=True):
36
  openai_api_key = st.text_input('OpenAI API Key', type='password', disabled=not txt_input)
37
  submitted = st.form_submit_button('Submit')
38
  if submitted and openai_api_key.startswith('sk-'):
39
- with st.spinner('Calculating...'):
40
  response = generate_response(txt_input, openai_api_key)
41
 
 
42
  if response:
43
  st.info(response)
44
 
@@ -48,6 +53,5 @@ st.write("You can get your own OpenAI API key by following the instructions:")
48
  st.write("""
49
  1. Go to [OpenAI API Keys](https://platform.openai.com/account/api-keys).
50
  2. Click on the `+ Create new secret key` button.
51
- 3. Next, enter an identifier name (optional) and click on the `Create secret key` button.
52
  """)
53
- 1
 
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)
36
 
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
 
 
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
  """)