Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from llm_functions import load_data, split_text, initialize_llm, generate_questions, create_retrieval_qa_chain
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
#
|
| 6 |
if 'questions' not in st.session_state:
|
| 7 |
st.session_state['questions'] = 'empty'
|
| 8 |
st.session_state['seperated_question_list'] = 'empty'
|
|
@@ -10,68 +10,72 @@ if 'questions' not in st.session_state:
|
|
| 10 |
st.session_state['submitted'] = 'empty'
|
| 11 |
|
| 12 |
with st.container():
|
| 13 |
-
st.markdown("""#
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
openai_api_key = st.text_input(label="OpenAI API
|
| 17 |
|
| 18 |
-
#
|
| 19 |
with st.container():
|
| 20 |
-
st.markdown("""
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
#
|
| 25 |
-
uploaded_file = st.file_uploader("
|
| 26 |
|
| 27 |
if uploaded_file is not None:
|
| 28 |
-
#
|
| 29 |
if not openai_api_key:
|
| 30 |
-
st.error("
|
| 31 |
else:
|
| 32 |
-
#
|
| 33 |
text_from_pdf = load_data(uploaded_file)
|
| 34 |
|
| 35 |
-
#
|
| 36 |
documents_for_question_gen = split_text(text_from_pdf, chunk_size=10000, chunk_overlap=200)
|
| 37 |
|
| 38 |
-
#
|
| 39 |
documents_for_question_answering = split_text(text_from_pdf, chunk_size=500, chunk_overlap=200)
|
| 40 |
|
| 41 |
-
#
|
| 42 |
llm_question_gen = initialize_llm(openai_api_key=openai_api_key, model="gpt-3.5-turbo", temperature=0.4)
|
| 43 |
|
| 44 |
-
#
|
| 45 |
llm_question_answering = initialize_llm(openai_api_key=openai_api_key, model="gpt-3.5-turbo", temperature=0.1)
|
| 46 |
|
| 47 |
-
#
|
| 48 |
if st.session_state['questions'] == 'empty':
|
| 49 |
-
with st.spinner("
|
| 50 |
-
#
|
| 51 |
st.session_state['questions'] = generate_questions(llm=llm_question_gen, chain_type="refine", documents=documents_for_question_gen)
|
| 52 |
|
| 53 |
if st.session_state['questions'] != 'empty':
|
| 54 |
-
#
|
| 55 |
st.info(st.session_state['questions'])
|
| 56 |
|
| 57 |
-
#
|
| 58 |
st.session_state['questions_list'] = st.session_state['questions'].split('\n')
|
| 59 |
|
| 60 |
with st.form(key='my_form'):
|
| 61 |
-
#
|
| 62 |
-
st.session_state['questions_to_answers'] = st.multiselect(label="
|
| 63 |
-
submitted = st.form_submit_button('
|
| 64 |
if submitted:
|
| 65 |
st.session_state['submitted'] = True
|
| 66 |
|
| 67 |
if st.session_state['submitted']:
|
| 68 |
-
|
| 69 |
-
with st.spinner("
|
| 70 |
generate_answer_chain = create_retrieval_qa_chain(openai_api_key=openai_api_key, documents=documents_for_question_answering, llm=llm_question_answering)
|
| 71 |
-
#
|
| 72 |
for question in st.session_state['questions_to_answers']:
|
| 73 |
-
#
|
| 74 |
answer = generate_answer_chain.run(question)
|
| 75 |
-
#
|
| 76 |
-
st.write(f"
|
| 77 |
-
st.info(f"
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from llm_functions import load_data, split_text, initialize_llm, generate_questions, create_retrieval_qa_chain
|
| 3 |
|
| 4 |
+
# Initialization of session states
|
| 5 |
+
# Since Streamlit always reruns the script when a widget changes, we need to initialize the session states
|
| 6 |
if 'questions' not in st.session_state:
|
| 7 |
st.session_state['questions'] = 'empty'
|
| 8 |
st.session_state['seperated_question_list'] = 'empty'
|
|
|
|
| 10 |
st.session_state['submitted'] = 'empty'
|
| 11 |
|
| 12 |
with st.container():
|
| 13 |
+
st.markdown("""# LabSoft Study Buddy
|
| 14 |
+
""")
|
| 15 |
+
st.divider()
|
| 16 |
|
| 17 |
+
# Get user's OpenAI API Key
|
| 18 |
+
openai_api_key = st.text_input(label="OpenAI API Key ", placeholder="Ex: sk-2twmA8tfCb8un4...", key="openai_api_key_input", help="How to get an OpenAI API Key: https://www.howtogeek.com/885918/how-to-get-an-openai-api-key/")
|
| 19 |
|
| 20 |
+
# Container for markdown text
|
| 21 |
with st.container():
|
| 22 |
+
st.markdown("""Make sure you've entered your OpenAI API Key.
|
| 23 |
+
Don't have an API key yet?
|
| 24 |
+
Read [this](https://medium.com/@joristechtalk/how-to-get-an-openai-api-key-for-chatgpt-fd2cf6a436c5) article on how to get an API key.""")
|
| 25 |
+
|
| 26 |
+
# Let user upload a file
|
| 27 |
+
uploaded_file = st.file_uploader("Upload your study material", type=['pdf'])
|
| 28 |
|
| 29 |
if uploaded_file is not None:
|
| 30 |
+
# Check whether user entered an API key
|
| 31 |
if not openai_api_key:
|
| 32 |
+
st.error("Please enter your OpenAI API Key")
|
| 33 |
else:
|
| 34 |
+
# Load data from PDF
|
| 35 |
text_from_pdf = load_data(uploaded_file)
|
| 36 |
|
| 37 |
+
# Split text for question generation
|
| 38 |
documents_for_question_gen = split_text(text_from_pdf, chunk_size=10000, chunk_overlap=200)
|
| 39 |
|
| 40 |
+
# Split text for question answering
|
| 41 |
documents_for_question_answering = split_text(text_from_pdf, chunk_size=500, chunk_overlap=200)
|
| 42 |
|
| 43 |
+
# Initialize large language model for question generation
|
| 44 |
llm_question_gen = initialize_llm(openai_api_key=openai_api_key, model="gpt-3.5-turbo", temperature=0.4)
|
| 45 |
|
| 46 |
+
# Initialize large language model for question answering
|
| 47 |
llm_question_answering = initialize_llm(openai_api_key=openai_api_key, model="gpt-3.5-turbo", temperature=0.1)
|
| 48 |
|
| 49 |
+
# Create questions if they have not yet been generated
|
| 50 |
if st.session_state['questions'] == 'empty':
|
| 51 |
+
with st.spinner("Generating questions..."):
|
| 52 |
+
# Assign the generated questions to the session state. This way, the questions are only generated once.
|
| 53 |
st.session_state['questions'] = generate_questions(llm=llm_question_gen, chain_type="refine", documents=documents_for_question_gen)
|
| 54 |
|
| 55 |
if st.session_state['questions'] != 'empty':
|
| 56 |
+
# Show questions on screen. You could use st.code for easy copy-pasting.
|
| 57 |
st.info(st.session_state['questions'])
|
| 58 |
|
| 59 |
+
# Split questions into a list
|
| 60 |
st.session_state['questions_list'] = st.session_state['questions'].split('\n')
|
| 61 |
|
| 62 |
with st.form(key='my_form'):
|
| 63 |
+
# Create a list of questions that have to be answered
|
| 64 |
+
st.session_state['questions_to_answers'] = st.multiselect(label="Select questions to answer", options=st.session_state['questions_list'])
|
| 65 |
+
submitted = st.form_submit_button('Generate answers')
|
| 66 |
if submitted:
|
| 67 |
st.session_state['submitted'] = True
|
| 68 |
|
| 69 |
if st.session_state['submitted']:
|
| 70 |
+
# Initialize the Retrieval QA Chain
|
| 71 |
+
with st.spinner("Generating answers..."):
|
| 72 |
generate_answer_chain = create_retrieval_qa_chain(openai_api_key=openai_api_key, documents=documents_for_question_answering, llm=llm_question_answering)
|
| 73 |
+
# For each question, generate an answer
|
| 74 |
for question in st.session_state['questions_to_answers']:
|
| 75 |
+
# Generate answer
|
| 76 |
answer = generate_answer_chain.run(question)
|
| 77 |
+
# Show answer on screen
|
| 78 |
+
st.write(f"Question: {question}")
|
| 79 |
+
st.info(f"Answer: {answer}")
|
| 80 |
+
|
| 81 |
+
|