|
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 |
|
|
|
def generate_response(txt, openai_api_key): |
|
try: |
|
|
|
openai.api_key = openai_api_key |
|
|
|
|
|
text_splitter = CharacterTextSplitter() |
|
texts = text_splitter.split_text(txt) |
|
|
|
|
|
docs = [Document(page_content=t) for t in texts] |
|
|
|
|
|
chain = load_summarize_chain(llm="openai", chain_type='map_reduce') |
|
return chain.run(docs) |
|
except Exception as e: |
|
st.error(f"An error occurred during summarization: {str(e)}") |
|
return None |
|
|
|
|
|
st.set_page_config(page_title='π¦π Text Summarization App') |
|
st.title('π¦π Text Summarization App') |
|
|
|
|
|
txt_input = st.text_area('Enter your text', '', height=200) |
|
|
|
|
|
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('Calculating...'): |
|
response = generate_response(txt_input, openai_api_key) |
|
|
|
if response: |
|
st.info(response) |
|
|
|
|
|
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. Next, enter an identifier name (optional) and click on the `Create secret key` button. |
|
""") |
|
1 |