Spaces:
Runtime error
Runtime error
fracapuano
commited on
Commit
•
7a7c4d5
1
Parent(s):
0489db2
add: streaming of answers
Browse files
qa/qa.py
CHANGED
@@ -1,8 +1,6 @@
|
|
1 |
import streamlit as st
|
2 |
-
from streamlit_chat import message
|
3 |
from openai.error import OpenAIError
|
4 |
from .utils import *
|
5 |
-
from uuid import uuid4
|
6 |
from typing import Text, Union
|
7 |
|
8 |
multiple_files = False
|
@@ -13,14 +11,19 @@ def clear_submit():
|
|
13 |
"""
|
14 |
st.session_state["file_submitted"] = False
|
15 |
|
16 |
-
def set_openai_api_key(api_key:Text):
|
17 |
"""Sets the internal OpenAI API key to the given value.
|
18 |
|
19 |
Args:
|
20 |
api_key (Text): OpenAI API key
|
21 |
"""
|
|
|
|
|
|
|
|
|
22 |
st.session_state["OPENAI_API_KEY"] = api_key
|
23 |
st.session_state["api_key_configured"] = True
|
|
|
24 |
|
25 |
def file_to_doc(file:Union[PDFFile, DocxFile, TxtFile, CodeFile]) -> None:
|
26 |
"""Converts a file to a document using specialized parsers."""
|
@@ -48,6 +51,7 @@ def qa_main():
|
|
48 |
|
49 |
# OpenAI API Key - TODO: consider adding a key valid for everyone
|
50 |
st.header("Configure OpenAI API Key")
|
|
|
51 |
user_secret = st.text_input(
|
52 |
"Insert your OpenAI API key here ([get your API key](https://platform.openai.com/account/api-keys)).",
|
53 |
type="password",
|
@@ -56,8 +60,9 @@ def qa_main():
|
|
56 |
value=st.session_state.get("OPENAI_API_KEY", ""),
|
57 |
)
|
58 |
if user_secret:
|
59 |
-
set_openai_api_key(user_secret)
|
60 |
-
|
|
|
61 |
# File that needs to be queried
|
62 |
st.header("Upload a file")
|
63 |
uploaded_file = st.file_uploader(
|
@@ -101,9 +106,15 @@ def qa_main():
|
|
101 |
# retrieving the most relevant sources
|
102 |
sources = search_docs(index, prompt)
|
103 |
# producing the answer, live
|
104 |
-
|
105 |
-
|
106 |
-
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
-
st.session_state.messages.append({"role": "assistant", "content": answer["output_text"]})
|
|
|
109 |
|
|
|
1 |
import streamlit as st
|
|
|
2 |
from openai.error import OpenAIError
|
3 |
from .utils import *
|
|
|
4 |
from typing import Text, Union
|
5 |
|
6 |
multiple_files = False
|
|
|
11 |
"""
|
12 |
st.session_state["file_submitted"] = False
|
13 |
|
14 |
+
def set_openai_api_key(api_key:Text)->bool:
|
15 |
"""Sets the internal OpenAI API key to the given value.
|
16 |
|
17 |
Args:
|
18 |
api_key (Text): OpenAI API key
|
19 |
"""
|
20 |
+
if not (api_key.startswith('sk-') and len(api_key)==51):
|
21 |
+
st.error("Invalid OpenAI API key! Please provide a valid key.")
|
22 |
+
return False
|
23 |
+
|
24 |
st.session_state["OPENAI_API_KEY"] = api_key
|
25 |
st.session_state["api_key_configured"] = True
|
26 |
+
return True
|
27 |
|
28 |
def file_to_doc(file:Union[PDFFile, DocxFile, TxtFile, CodeFile]) -> None:
|
29 |
"""Converts a file to a document using specialized parsers."""
|
|
|
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='⚠️')
|
55 |
user_secret = st.text_input(
|
56 |
"Insert your OpenAI API key here ([get your API key](https://platform.openai.com/account/api-keys)).",
|
57 |
type="password",
|
|
|
60 |
value=st.session_state.get("OPENAI_API_KEY", ""),
|
61 |
)
|
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(
|
|
|
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 |
|