LukaVidakovic commited on
Commit
e0df169
·
verified ·
1 Parent(s): e0dfb45

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -34
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
- # 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'
@@ -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("""# Labsoft Study Buddy""")
 
 
14
 
15
- # Preuzmi OpenAI API ključ od korisnika
16
- openai_api_key = st.text_input(label="OpenAI API Ključ ", 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", type=['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", 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}")
 
 
 
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
+