LukaVidakovic commited on
Commit
ea91504
·
verified ·
1 Parent(s): 3781067

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -33
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
- # 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'
@@ -12,66 +12,66 @@ if 'questions' not in st.session_state:
12
  with st.container():
13
  st.markdown("""# Smart Study Buddy""")
14
 
15
- # Get user's OpenAI API Key
16
- 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/")
17
 
18
- # Container for markdown text
19
  with st.container():
20
- st.markdown("""Make sure you've entered your OpenAI API Key.
21
- Don't have an API key yet?
22
- Read [this](https://medium.com/@joristechtalk/how-to-get-an-openai-api-key-for-chatgpt-fd2cf6a436c5) article on how to get an API key.""")
23
-
24
- # Let user upload a file
25
- uploaded_file = st.file_uploader("Upload your study material", type=['pdf'])
26
 
27
  if uploaded_file is not None:
28
- # Check whether user entered an API key
29
  if not openai_api_key:
30
- st.error("Please enter your OpenAI API Key")
31
  else:
32
- # Load data from PDF
33
  text_from_pdf = load_data(uploaded_file)
34
 
35
- # Split text for question generation
36
  documents_for_question_gen = split_text(text_from_pdf, chunk_size=10000, chunk_overlap=200)
37
 
38
- # Split text for question answering
39
  documents_for_question_answering = split_text(text_from_pdf, chunk_size=500, chunk_overlap=200)
40
 
41
- # Initialize large language model for question generation
42
  llm_question_gen = initialize_llm(openai_api_key=openai_api_key, model="gpt-3.5-turbo-16k", temperature=0.4)
43
 
44
- # Initialize large language model for question answering
45
  llm_question_answering = initialize_llm(openai_api_key=openai_api_key, model="gpt-3.5-turbo", temperature=0.1)
46
 
47
- # Create questions if they have not yet been generated
48
  if st.session_state['questions'] == 'empty':
49
- with st.spinner("Generating questions..."):
50
- # Assign the generated questions to the session state. This way, the questions are only generated once.
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
- # Show questions on screen. You could use st.code for easy copy-pasting.
55
  st.info(st.session_state['questions'])
56
 
57
- # Split questions into a list
58
  st.session_state['questions_list'] = st.session_state['questions'].split('\n')
59
 
60
  with st.form(key='my_form'):
61
- # Create a list of questions that have to be answered
62
- st.session_state['questions_to_answers'] = st.multiselect(label="Select questions to answer", options=st.session_state['questions_list'])
63
- submitted = st.form_submit_button('Generate answers')
64
  if submitted:
65
  st.session_state['submitted'] = True
66
 
67
  if st.session_state['submitted']:
68
- # Initialize the Retrieval QA Chain
69
- with st.spinner("Generating answers..."):
70
  generate_answer_chain = create_retrieval_qa_chain(openai_api_key=openai_api_key, documents=documents_for_question_answering, llm=llm_question_answering)
71
- # For each question, generate an answer
72
  for question in st.session_state['questions_to_answers']:
73
- # Generate answer
74
  answer = generate_answer_chain.run(question)
75
- # Show answer on screen
76
- st.write(f"Question: {question}")
77
- st.info(f"Answer: {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
+ # 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}")