File size: 2,131 Bytes
5e2e02c
 
 
 
 
267005e
5e2e02c
 
 
 
 
 
267005e
5e2e02c
 
 
267005e
5e2e02c
 
267005e
 
 
 
 
5e2e02c
 
 
 
 
267005e
5e2e02c
 
 
267005e
5e2e02c
 
267005e
5e2e02c
 
 
 
 
267005e
5e2e02c
 
267005e
5e2e02c
 
 
 
 
 
 
 
 
267005e
5e2e02c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import streamlit as st
import openai
from langchain.docstore.document import Document
from langchain.text_splitter import CharacterTextSplitter
from langchain.chains.summarize import load_summarize_chain
from langchain.llms import OpenAI

def generate_response(txt, openai_api_key):
    try:
        # Set up OpenAI API key
        openai.api_key = openai_api_key

        # Split text into chunks
        text_splitter = CharacterTextSplitter()
        texts = text_splitter.split_text(txt)

        # Create documents from the text chunks
        docs = [Document(page_content=t) for t in texts]

        # Instantiate the OpenAI LLM model
        llm = OpenAI(temperature=0, openai_api_key=openai_api_key)

        # Create the summarization chain and summarize the documents
        chain = load_summarize_chain(llm=llm, chain_type='map_reduce')
        return chain.run(docs)
    except Exception as e:
        st.error(f"An error occurred during summarization: {str(e)}")
        return None

# Page title and layout
st.set_page_config(page_title='πŸ¦œπŸ”— Text Summarization App')
st.title('πŸ¦œπŸ”— Text Summarization App')

# Text input area for user to input text
txt_input = st.text_area('Enter your text', '', height=200)

# Form to accept the user's text input for summarization
response = None
with st.form('summarize_form', clear_on_submit=True):
    openai_api_key = st.text_input('OpenAI API Key', type='password', disabled=not txt_input)
    submitted = st.form_submit_button('Submit')
    if submitted and openai_api_key.startswith('sk-'):
        with st.spinner('Summarizing...'):
            response = generate_response(txt_input, openai_api_key)

# Display the response if available
if response:
    st.info(response)

# Instructions for getting an OpenAI API key
st.subheader("Get an OpenAI API key")
st.write("You can get your own OpenAI API key by following the instructions:")
st.write("""
1. Go to [OpenAI API Keys](https://platform.openai.com/account/api-keys).
2. Click on the `+ Create new secret key` button.
3. Enter an identifier name (optional) and click on the `Create secret key` button.
""")