Spaces:
Sleeping
Sleeping
Commit
·
d8f4e8d
1
Parent(s):
16e4fc4
oai key
Browse files- app.py +11 -11
- ttyd_functions.py +13 -1
- vecstore/c87f6fd4-541b-11ee-842a-acde48001122/c2ef6a64-23e9-4547-8d53-32e82b52f85f/data_level0.bin +3 -0
- vecstore/c87f6fd4-541b-11ee-842a-acde48001122/c2ef6a64-23e9-4547-8d53-32e82b52f85f/header.bin +3 -0
- vecstore/c87f6fd4-541b-11ee-842a-acde48001122/c2ef6a64-23e9-4547-8d53-32e82b52f85f/length.bin +3 -0
- vecstore/c87f6fd4-541b-11ee-842a-acde48001122/c2ef6a64-23e9-4547-8d53-32e82b52f85f/link_lists.bin +0 -0
- vecstore/c87f6fd4-541b-11ee-842a-acde48001122/chroma.sqlite3 +0 -0
- vecstore/ea86f048-541b-11ee-a314-acde48001122/chroma.sqlite3 +0 -0
- vecstore/ea86f048-541b-11ee-a314-acde48001122/eebc97bc-095a-4715-8ec5-f3ffedb1b4ea/data_level0.bin +3 -0
- vecstore/ea86f048-541b-11ee-a314-acde48001122/eebc97bc-095a-4715-8ec5-f3ffedb1b4ea/header.bin +3 -0
- vecstore/ea86f048-541b-11ee-a314-acde48001122/eebc97bc-095a-4715-8ec5-f3ffedb1b4ea/length.bin +3 -0
- vecstore/ea86f048-541b-11ee-a314-acde48001122/eebc97bc-095a-4715-8ec5-f3ffedb1b4ea/link_lists.bin +0 -0
- vecstore/ff5dbf74-541b-11ee-a0d2-acde48001122/4dda0e11-f96d-46f6-ad54-53cd3dde649f/data_level0.bin +3 -0
- vecstore/ff5dbf74-541b-11ee-a0d2-acde48001122/4dda0e11-f96d-46f6-ad54-53cd3dde649f/header.bin +3 -0
- vecstore/ff5dbf74-541b-11ee-a0d2-acde48001122/4dda0e11-f96d-46f6-ad54-53cd3dde649f/length.bin +3 -0
- vecstore/ff5dbf74-541b-11ee-a0d2-acde48001122/4dda0e11-f96d-46f6-ad54-53cd3dde649f/link_lists.bin +0 -0
- vecstore/ff5dbf74-541b-11ee-a0d2-acde48001122/chroma.sqlite3 +0 -0
app.py
CHANGED
|
@@ -70,17 +70,17 @@ def initializeChatbot(temp, k, modelName, stdlQs, api_key_st, vsDict_st, progres
|
|
| 70 |
print('Chatbot initialized at ', datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
|
| 71 |
|
| 72 |
return qa_chain_st, btn.update(interactive=True), initChatbot_btn.update('Chatbot ready. Now visit the chatbot Tab.', interactive=False)\
|
| 73 |
-
,
|
| 74 |
|
| 75 |
|
| 76 |
-
def
|
| 77 |
api_key = transformApi(api_key)
|
| 78 |
try:
|
| 79 |
openai.Model.list(api_key=api_key) # test the API key
|
| 80 |
api_key_st = api_key
|
| 81 |
-
return
|
| 82 |
except Exception as e:
|
| 83 |
-
return
|
| 84 |
|
| 85 |
# convert user uploaded data to vectorstore
|
| 86 |
def uiData_vecStore(userFiles, userUrls, api_key_st, vsDict_st={}, progress=gr.Progress()):
|
|
@@ -172,7 +172,7 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue='orange', secondary_hue='gray
|
|
| 172 |
# Initialize state variables - stored in this browser session - these can only be used within input or output of .click/.submit etc, not as a python var coz they are not stored in backend, only as a frontend gradio component
|
| 173 |
# but if you initialize it with a default value, that value will be stored in backend and accessible across all users. You can also change it with statear.value='newValue'
|
| 174 |
qa_state = gr.State()
|
| 175 |
-
api_key_state = gr.State(
|
| 176 |
chromaVS_state = gr.State({})
|
| 177 |
|
| 178 |
|
|
@@ -182,10 +182,10 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue='orange', secondary_hue='gray
|
|
| 182 |
with gr.Tab('Initialization', id='init'):
|
| 183 |
with gr.Row():
|
| 184 |
with gr.Column():
|
| 185 |
-
|
| 186 |
, info='You can find OpenAI API key at https://platform.openai.com/account/api-keys'\
|
| 187 |
, placeholder='Enter your API key here and hit enter to begin chatting')
|
| 188 |
-
|
| 189 |
with gr.Row(visible=mode.uiAddDataVis):
|
| 190 |
upload_fb = gr.Files(scale=5, label="Upload (multiple) Files - pdf/txt/docx supported", file_types=['.doc', '.docx', 'text', '.pdf', '.csv'])
|
| 191 |
urls_tb = gr.Textbox(scale=5, label="Enter URLs starting with https (comma separated)"\
|
|
@@ -221,9 +221,9 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue='orange', secondary_hue='gray
|
|
| 221 |
### Setup the Gradio Event Listeners
|
| 222 |
|
| 223 |
# API button
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
|
| 228 |
# Data Ingest Button
|
| 229 |
data_ingest_event = data_ingest_btn.click(uiData_vecStore, [upload_fb, urls_tb, api_key_state, chromaVS_state], [chromaVS_state, status_tb, data_ingest_btn, upload_fb, urls_tb])
|
|
@@ -236,7 +236,7 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue='orange', secondary_hue='gray
|
|
| 236 |
stdlQs_rb.change(**advSet_args)
|
| 237 |
|
| 238 |
# Initialize button
|
| 239 |
-
initCb_args = {'fn':initializeChatbot, 'inputs':[temp_sld, k_sld, model_dd, stdlQs_rb, api_key_state, chromaVS_state], 'outputs':[qa_state, btn, initChatbot_btn,
|
| 240 |
if mode.type=='personalBot':
|
| 241 |
demo.load(**initCb_args) # load Chatbot UI directly on startup
|
| 242 |
initChatbot_btn.click(**initCb_args)
|
|
|
|
| 70 |
print('Chatbot initialized at ', datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
|
| 71 |
|
| 72 |
return qa_chain_st, btn.update(interactive=True), initChatbot_btn.update('Chatbot ready. Now visit the chatbot Tab.', interactive=False)\
|
| 73 |
+
, oaiKey_tb.update(), gr.Tabs.update(selected='cb'), chatbot.update(value=[('', welMsg)])
|
| 74 |
|
| 75 |
|
| 76 |
+
def setOaiApiKey(api_key):
|
| 77 |
api_key = transformApi(api_key)
|
| 78 |
try:
|
| 79 |
openai.Model.list(api_key=api_key) # test the API key
|
| 80 |
api_key_st = api_key
|
| 81 |
+
return oaiKey_tb.update('API Key accepted', interactive=False, type='text'), oaiKey_btn.update(interactive=False), api_key_st
|
| 82 |
except Exception as e:
|
| 83 |
+
return oaiKey_tb.update(str(e), type='text'), *[x.update() for x in [oaiKey_btn, api_key_state]]
|
| 84 |
|
| 85 |
# convert user uploaded data to vectorstore
|
| 86 |
def uiData_vecStore(userFiles, userUrls, api_key_st, vsDict_st={}, progress=gr.Progress()):
|
|
|
|
| 172 |
# Initialize state variables - stored in this browser session - these can only be used within input or output of .click/.submit etc, not as a python var coz they are not stored in backend, only as a frontend gradio component
|
| 173 |
# but if you initialize it with a default value, that value will be stored in backend and accessible across all users. You can also change it with statear.value='newValue'
|
| 174 |
qa_state = gr.State()
|
| 175 |
+
api_key_state = gr.State(getPersonalBotApiKey() if mode.type=='personalBot' else 'Null') # can be string (OpenAI) or dict (WX)
|
| 176 |
chromaVS_state = gr.State({})
|
| 177 |
|
| 178 |
|
|
|
|
| 182 |
with gr.Tab('Initialization', id='init'):
|
| 183 |
with gr.Row():
|
| 184 |
with gr.Column():
|
| 185 |
+
oaiKey_tb = gr.Textbox(label="OpenAI API Key", type='password'\
|
| 186 |
, info='You can find OpenAI API key at https://platform.openai.com/account/api-keys'\
|
| 187 |
, placeholder='Enter your API key here and hit enter to begin chatting')
|
| 188 |
+
oaiKey_btn = gr.Button("Submit API Key")
|
| 189 |
with gr.Row(visible=mode.uiAddDataVis):
|
| 190 |
upload_fb = gr.Files(scale=5, label="Upload (multiple) Files - pdf/txt/docx supported", file_types=['.doc', '.docx', 'text', '.pdf', '.csv'])
|
| 191 |
urls_tb = gr.Textbox(scale=5, label="Enter URLs starting with https (comma separated)"\
|
|
|
|
| 221 |
### Setup the Gradio Event Listeners
|
| 222 |
|
| 223 |
# API button
|
| 224 |
+
oaiKey_btn_args = {'fn':setOaiApiKey, 'inputs':[oaiKey_tb], 'outputs':[oaiKey_tb, oaiKey_btn, api_key_state]}
|
| 225 |
+
oaiKey_btn.click(**oaiKey_btn_args)
|
| 226 |
+
oaiKey_tb.submit(**oaiKey_btn_args)
|
| 227 |
|
| 228 |
# Data Ingest Button
|
| 229 |
data_ingest_event = data_ingest_btn.click(uiData_vecStore, [upload_fb, urls_tb, api_key_state, chromaVS_state], [chromaVS_state, status_tb, data_ingest_btn, upload_fb, urls_tb])
|
|
|
|
| 236 |
stdlQs_rb.change(**advSet_args)
|
| 237 |
|
| 238 |
# Initialize button
|
| 239 |
+
initCb_args = {'fn':initializeChatbot, 'inputs':[temp_sld, k_sld, model_dd, stdlQs_rb, api_key_state, chromaVS_state], 'outputs':[qa_state, btn, initChatbot_btn, oaiKey_tb, tabs, chatbot]}
|
| 240 |
if mode.type=='personalBot':
|
| 241 |
demo.load(**initCb_args) # load Chatbot UI directly on startup
|
| 242 |
initChatbot_btn.click(**initCb_args)
|
ttyd_functions.py
CHANGED
|
@@ -261,4 +261,16 @@ def num_tokens_from_string(string, encoding_name = "cl100k_base"):
|
|
| 261 |
"""Returns the number of tokens in a text string."""
|
| 262 |
encoding = tiktoken.get_encoding(encoding_name)
|
| 263 |
num_tokens = len(encoding.encode(string))
|
| 264 |
-
return num_tokens
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
"""Returns the number of tokens in a text string."""
|
| 262 |
encoding = tiktoken.get_encoding(encoding_name)
|
| 263 |
num_tokens = len(encoding.encode(string))
|
| 264 |
+
return num_tokens
|
| 265 |
+
|
| 266 |
+
def getPersonalBotApiKey():
|
| 267 |
+
print('funcCalled')
|
| 268 |
+
if os.getenv("OPENAI_API_KEY"):
|
| 269 |
+
return os.getenv("OPENAI_API_KEY")
|
| 270 |
+
elif os.getenv("WX_API_KEY"):
|
| 271 |
+
wxCreds = {'credentials' : {"url": "https://us-south.ml.cloud.ibm.com", "apikey": os.getenv("WX_API_KEY") },
|
| 272 |
+
'project_id': os.getenv("WX_PROJECT_ID")
|
| 273 |
+
}
|
| 274 |
+
return wxCreds
|
| 275 |
+
else:
|
| 276 |
+
return None
|
vecstore/c87f6fd4-541b-11ee-842a-acde48001122/c2ef6a64-23e9-4547-8d53-32e82b52f85f/data_level0.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f18abd8c514282db82706e52b0a33ed659cd534e925a6f149deb7af9ce34bd8e
|
| 3 |
+
size 6284000
|
vecstore/c87f6fd4-541b-11ee-842a-acde48001122/c2ef6a64-23e9-4547-8d53-32e82b52f85f/header.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:effaa959ce2b30070fdafc2fe82096fc46e4ee7561b75920dd3ce43d09679b21
|
| 3 |
+
size 100
|
vecstore/c87f6fd4-541b-11ee-842a-acde48001122/c2ef6a64-23e9-4547-8d53-32e82b52f85f/length.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4fa0f3de3eca485efea4ea8901f5a34a2bf77ee4fbba4afb491745539ccee6af
|
| 3 |
+
size 4000
|
vecstore/c87f6fd4-541b-11ee-842a-acde48001122/c2ef6a64-23e9-4547-8d53-32e82b52f85f/link_lists.bin
ADDED
|
File without changes
|
vecstore/c87f6fd4-541b-11ee-842a-acde48001122/chroma.sqlite3
ADDED
|
Binary file (168 kB). View file
|
|
|
vecstore/ea86f048-541b-11ee-a314-acde48001122/chroma.sqlite3
ADDED
|
Binary file (168 kB). View file
|
|
|
vecstore/ea86f048-541b-11ee-a314-acde48001122/eebc97bc-095a-4715-8ec5-f3ffedb1b4ea/data_level0.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f18abd8c514282db82706e52b0a33ed659cd534e925a6f149deb7af9ce34bd8e
|
| 3 |
+
size 6284000
|
vecstore/ea86f048-541b-11ee-a314-acde48001122/eebc97bc-095a-4715-8ec5-f3ffedb1b4ea/header.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:effaa959ce2b30070fdafc2fe82096fc46e4ee7561b75920dd3ce43d09679b21
|
| 3 |
+
size 100
|
vecstore/ea86f048-541b-11ee-a314-acde48001122/eebc97bc-095a-4715-8ec5-f3ffedb1b4ea/length.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fc19b1997119425765295aeab72d76faa6927d4f83985d328c26f20468d6cc76
|
| 3 |
+
size 4000
|
vecstore/ea86f048-541b-11ee-a314-acde48001122/eebc97bc-095a-4715-8ec5-f3ffedb1b4ea/link_lists.bin
ADDED
|
File without changes
|
vecstore/ff5dbf74-541b-11ee-a0d2-acde48001122/4dda0e11-f96d-46f6-ad54-53cd3dde649f/data_level0.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f18abd8c514282db82706e52b0a33ed659cd534e925a6f149deb7af9ce34bd8e
|
| 3 |
+
size 6284000
|
vecstore/ff5dbf74-541b-11ee-a0d2-acde48001122/4dda0e11-f96d-46f6-ad54-53cd3dde649f/header.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:effaa959ce2b30070fdafc2fe82096fc46e4ee7561b75920dd3ce43d09679b21
|
| 3 |
+
size 100
|
vecstore/ff5dbf74-541b-11ee-a0d2-acde48001122/4dda0e11-f96d-46f6-ad54-53cd3dde649f/length.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fc19b1997119425765295aeab72d76faa6927d4f83985d328c26f20468d6cc76
|
| 3 |
+
size 4000
|
vecstore/ff5dbf74-541b-11ee-a0d2-acde48001122/4dda0e11-f96d-46f6-ad54-53cd3dde649f/link_lists.bin
ADDED
|
File without changes
|
vecstore/ff5dbf74-541b-11ee-a0d2-acde48001122/chroma.sqlite3
ADDED
|
Binary file (168 kB). View file
|
|
|