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'
|
|
@@ -12,66 +12,66 @@ if 'questions' not in st.session_state:
|
|
| 12 |
with st.container():
|
| 13 |
st.markdown("""# Smart Study Buddy""")
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
openai_api_key = st.text_input(label="OpenAI API Key ", placeholder="Ex: sk-2twmA8tfCb8un4...", key="openai_api_key_input", help="
|
| 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-16k", 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 |
+
# Inicijalizacija session states
|
| 5 |
+
# Pošto Streamlit uvek ponovo pokreće skriptu kada se promeni neki widget, potrebno je inicijalizovati session states
|
| 6 |
if 'questions' not in st.session_state:
|
| 7 |
st.session_state['questions'] = 'empty'
|
| 8 |
st.session_state['seperated_question_list'] = 'empty'
|
|
|
|
| 12 |
with st.container():
|
| 13 |
st.markdown("""# Smart Study Buddy""")
|
| 14 |
|
| 15 |
+
# Preuzmi OpenAI API ključ od korisnika
|
| 16 |
+
openai_api_key = st.text_input(label="OpenAI API Key ", placeholder="Ex: sk-2twmA8tfCb8un4...", key="openai_api_key_input", help="Kako dobiti OpenAI API ključ: https://www.howtogeek.com/885918/how-to-get-an-openai-api-key/")
|
| 17 |
|
| 18 |
+
# Kontejner za markdown tekst
|
| 19 |
with st.container():
|
| 20 |
+
st.markdown("""Uverite se da ste uneli svoj OpenAI API ključ.
|
| 21 |
+
Još uvek nemate API ključ?
|
| 22 |
+
Pročitajte [ovaj](https://medium.com/@joristechtalk/how-to-get-an-openai-api-key-for-chatgpt-fd2cf6a436c5) članak o tome kako dobiti API ključ.""")
|
| 23 |
+
|
| 24 |
+
# Dozvoli korisniku da uploaduje fajl
|
| 25 |
+
uploaded_file = st.file_uploader("Postavite vaš materijal za učenje", tip=['pdf'])
|
| 26 |
|
| 27 |
if uploaded_file is not None:
|
| 28 |
+
# Proveri da li je korisnik uneo API ključ
|
| 29 |
if not openai_api_key:
|
| 30 |
+
st.error("Molimo unesite svoj OpenAI API ključ")
|
| 31 |
else:
|
| 32 |
+
# Učitaj podatke iz PDF-a
|
| 33 |
text_from_pdf = load_data(uploaded_file)
|
| 34 |
|
| 35 |
+
# Podeli tekst za generisanje pitanja
|
| 36 |
documents_for_question_gen = split_text(text_from_pdf, chunk_size=10000, chunk_overlap=200)
|
| 37 |
|
| 38 |
+
# Podeli tekst za odgovaranje na pitanja
|
| 39 |
documents_for_question_answering = split_text(text_from_pdf, chunk_size=500, chunk_overlap=200)
|
| 40 |
|
| 41 |
+
# Inicijalizuj veliki jezički model za generisanje pitanja
|
| 42 |
llm_question_gen = initialize_llm(openai_api_key=openai_api_key, model="gpt-3.5-turbo-16k", temperature=0.4)
|
| 43 |
|
| 44 |
+
# Inicijalizuj veliki jezički model za odgovaranje na pitanja
|
| 45 |
llm_question_answering = initialize_llm(openai_api_key=openai_api_key, model="gpt-3.5-turbo", temperature=0.1)
|
| 46 |
|
| 47 |
+
# Kreiraj pitanja ako još nisu generisana
|
| 48 |
if st.session_state['questions'] == 'empty':
|
| 49 |
+
with st.spinner("Generisanje pitanja..."):
|
| 50 |
+
# Dodeli generisana pitanja session state-u. Na taj način, pitanja se generišu samo jednom.
|
| 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 |
+
# Prikaži pitanja na ekranu. Možete koristiti st.code za lako kopiranje.
|
| 55 |
st.info(st.session_state['questions'])
|
| 56 |
|
| 57 |
+
# Podeli pitanja u listu
|
| 58 |
st.session_state['questions_list'] = st.session_state['questions'].split('\n')
|
| 59 |
|
| 60 |
with st.form(key='my_form'):
|
| 61 |
+
# Kreiraj listu pitanja na koja treba odgovoriti
|
| 62 |
+
st.session_state['questions_to_answers'] = st.multiselect(label="Izaberite pitanja na koja želite odgovore", options=st.session_state['questions_list'])
|
| 63 |
+
submitted = st.form_submit_button('Generiši odgovore')
|
| 64 |
if submitted:
|
| 65 |
st.session_state['submitted'] = True
|
| 66 |
|
| 67 |
if st.session_state['submitted']:
|
| 68 |
+
# Inicijalizuj Retrieval QA Chain
|
| 69 |
+
with st.spinner("Generisanje odgovora..."):
|
| 70 |
generate_answer_chain = create_retrieval_qa_chain(openai_api_key=openai_api_key, documents=documents_for_question_answering, llm=llm_question_answering)
|
| 71 |
+
# Za svako pitanje generiši odgovor
|
| 72 |
for question in st.session_state['questions_to_answers']:
|
| 73 |
+
# Generiši odgovor
|
| 74 |
answer = generate_answer_chain.run(question)
|
| 75 |
+
# Prikaži odgovor na ekranu
|
| 76 |
+
st.write(f"Pitanje: {question}")
|
| 77 |
+
st.info(f"Odgovor: {answer}")
|