Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
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 |
+
|
45 |
+
# Instructions for getting an OpenAI API key
|
46 |
+
st.subheader("Get an OpenAI API key")
|
47 |
+
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
|