fracapuano commited on
Commit
d5bd88b
1 Parent(s): 60017a4

add: greenlights to showcase widgets

Browse files
Files changed (1) hide show
  1. qa/qa.py +75 -52
qa/qa.py CHANGED
@@ -49,6 +49,8 @@ def qa_main():
49
  index = None
50
  doc = None
51
 
 
 
52
  # OpenAI API Key - TODO: consider adding a key valid for everyone
53
  st.header("Configure OpenAI API Key")
54
  st.warning('Please enter your OpenAI API Key!', icon='⚠️')
@@ -62,59 +64,80 @@ def qa_main():
62
  if user_secret:
63
  if set_openai_api_key(user_secret):
64
  st.success('OpenAI API key successfully provided!', icon='✅')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
- # File that needs to be queried
67
- st.header("Upload a file")
68
- uploaded_file = st.file_uploader(
69
- "Upload a pdf, docx, or txt file (scanned documents not supported)",
70
- type=["pdf", "docx", "txt", "py", "json", "html", "css", "md"],
71
- help="Scanned documents are not supported yet 🥲",
72
- on_change=clear_submit,
73
- accept_multiple_files=multiple_files,
74
- )
75
 
76
- # reading the uploaded file
77
- if uploaded_file is not None:
78
- # toggle internal file submission state to True
79
- st.session_state["file_submitted"] = True
80
- # parse the file using custom parsers
81
- doc = file_to_doc(uploaded_file)
82
- # converts the files into a list of documents
83
- text = text_to_docs(text=tuple(doc))
84
-
85
- try:
86
- with st.spinner("Indexing the document... This might take a while!"):
87
- index = embed_docs(tuple(text))
88
- st.session_state["api_key_configured"] = True
89
- except OpenAIError as e:
90
- st.error("OpenAI error encountered: ", e._message)
91
-
92
- if "messages" not in st.session_state:
93
- st.session_state["messages"] = []
94
-
95
- for message in st.session_state.messages:
96
- with st.chat_message(message["role"]):
97
- st.markdown(message["content"])
98
-
99
- if prompt := st.chat_input("Ask the document something..."):
100
- st.session_state.messages.append({"role": "user", "content": prompt})
101
- with st.chat_message("user"):
102
- st.markdown(prompt)
103
-
104
- with st.chat_message("assistant"):
105
- message_placeholder = st.empty()
106
- # retrieving the most relevant sources
107
- sources = search_docs(index, prompt)
108
- # producing the answer, live
109
- full_response = ""
110
- for answer_bit in get_answer(sources, prompt)["output_text"]:
111
- full_response += answer_bit
112
- message_placeholder.markdown(full_response + "▌")
113
-
114
- message_placeholder.markdown(full_response)
115
- # answer = get_answer(sources, prompt)
116
- # message_placeholder.markdown(answer["output_text"])
117
 
118
- # st.session_state.messages.append({"role": "assistant", "content": answer["output_text"]})
119
- st.session_state.messages.append({"role": "assistant", "content": full_response})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
 
 
 
49
  index = None
50
  doc = None
51
 
52
+ upload_document_greenlight = False
53
+ uploaded_processed_document_greenlight = False
54
  # OpenAI API Key - TODO: consider adding a key valid for everyone
55
  st.header("Configure OpenAI API Key")
56
  st.warning('Please enter your OpenAI API Key!', icon='⚠️')
 
64
  if user_secret:
65
  if set_openai_api_key(user_secret):
66
  st.success('OpenAI API key successfully provided!', icon='✅')
67
+ upload_document_greenlight = True
68
+
69
+ if upload_document_greenlight:
70
+ # File that needs to be queried
71
+ st.header("Upload a file")
72
+ uploaded_file = st.file_uploader(
73
+ "Upload a pdf, docx, or txt file (scanned documents not supported)",
74
+ type=["pdf", "docx", "txt", "py", "json", "html", "css", "md"],
75
+ help="Scanned documents are not supported yet 🥲",
76
+ on_change=clear_submit,
77
+ accept_multiple_files=multiple_files,
78
+ )
79
+
80
+ # reading the uploaded file
81
+ if uploaded_file is not None:
82
+ # toggle internal file submission state to True
83
+ st.session_state["file_submitted"] = True
84
+ # parse the file using custom parsers
85
+ doc = file_to_doc(uploaded_file)
86
+ # converts the files into a list of documents
87
+ text = text_to_docs(text=tuple(doc))
88
 
89
+ try:
90
+ with st.spinner("Indexing the document... This might take a while!"):
91
+ index = embed_docs(tuple(text))
92
+ st.session_state["api_key_configured"] = True
93
+ except OpenAIError as e:
94
+ st.error("OpenAI error encountered: ", e._message)
 
 
 
95
 
96
+ uploaded_processed_document_greenlight = True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
+ if uploaded_processed_document_greenlight:
99
+ if "messages" not in st.session_state:
100
+ st.session_state["messages"] = []
101
+
102
+ for message in st.session_state.messages:
103
+ with st.chat_message(message["role"]):
104
+ st.markdown(message["content"])
105
+
106
+ if prompt := st.chat_input("Ask the document something..."):
107
+ st.session_state.messages.append({"role": "user", "content": prompt})
108
+ with st.chat_message("user"):
109
+ st.markdown(prompt)
110
+
111
+ with st.chat_message("assistant"):
112
+ message_placeholder = st.empty()
113
+ # retrieving the most relevant sources
114
+ sources = search_docs(index, prompt)
115
+ # producing the answer, live
116
+ full_response = ""
117
+ for answer_bit in get_answer(sources, prompt)["output_text"]:
118
+ full_response += answer_bit
119
+ message_placeholder.markdown(full_response + "▌")
120
+
121
+ message_placeholder.markdown(full_response)
122
+
123
+ # answer = get_answer(sources, prompt)
124
+ # message_placeholder.markdown(answer["output_text"])
125
+
126
+ # st.session_state.messages.append({"role": "assistant", "content": answer["output_text"]})
127
+ st.session_state.messages.append({"role": "assistant", "content": full_response})
128
+
129
+ # This might be useful to add memory to the chatbot harnessing a more low-level approach
130
+ # llm = ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo")
131
+
132
+ # memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True, output_key='answer')
133
+ # retriever = your_vector_store.as_retriever()
134
+
135
+ # # Create the multipurpose chain
136
+ # qachat = ConversationalRetrievalChain.from_llm(
137
+ # llm=ChatOpenAI(temperature=0),
138
+ # memory=memory,
139
+ # retriever=retriever,
140
+ # return_source_documents=True
141
+ # )
142
 
143
+ # qachat("Ask your question here...")