Spaces:
Runtime error
Runtime error
nicole-ait commited on
Commit ·
572d7fe
1
Parent(s): 402f092
refresh button
Browse files
app.py
CHANGED
|
@@ -39,15 +39,16 @@ def get_persist_directory(file_name):
|
|
| 39 |
|
| 40 |
|
| 41 |
def process_file(file, chunk_size, chunk_overlap):
|
|
|
|
|
|
|
|
|
|
| 42 |
file_name, _ = os.path.splitext(os.path.basename(file.name))
|
| 43 |
persist_directory = get_persist_directory(file_name)
|
| 44 |
-
print("persist directory", persist_directory)
|
| 45 |
|
| 46 |
-
|
| 47 |
-
embeddings = load_embeddings()
|
| 48 |
vectordb = Chroma.from_documents(documents=docs, embedding=embeddings,
|
| 49 |
collection_name=file_name, persist_directory=persist_directory)
|
| 50 |
-
print(vectordb._client.list_collections())
|
| 51 |
vectordb.persist()
|
| 52 |
return 'Done!', gr.Dropdown.update(choices=get_vector_dbs(), value=file_name)
|
| 53 |
|
|
@@ -97,6 +98,11 @@ def create_qa_chain(collection_name, temperature, max_length):
|
|
| 97 |
llm=llm, retriever=vectordb.as_retriever(), memory=memory)
|
| 98 |
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
def submit_message(bot_history, text):
|
| 101 |
bot_history = bot_history + [(text, None)]
|
| 102 |
return bot_history, ""
|
|
@@ -130,16 +136,21 @@ with gr.Blocks() as demo:
|
|
| 130 |
|
| 131 |
with gr.Tab("Bot"):
|
| 132 |
with gr.Row():
|
| 133 |
-
with gr.Column(
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
temperature = gr.Slider(
|
| 138 |
0.0, 1.0, value=0.5, step=0.05, label="Temperature")
|
| 139 |
max_length = gr.Slider(
|
| 140 |
20, 1000, value=100, step=10, label="Max Length")
|
| 141 |
|
| 142 |
-
with gr.Column(
|
| 143 |
chatbot = gr.Chatbot([], elem_id="chatbot").style(height=550)
|
| 144 |
message = gr.Textbox(
|
| 145 |
show_label=False, placeholder="Ask me anything!")
|
|
@@ -156,6 +167,7 @@ with gr.Blocks() as demo:
|
|
| 156 |
temperature.change(create_qa_chain, [collection, temperature, max_length])
|
| 157 |
max_length.change(create_qa_chain, [collection, temperature, max_length])
|
| 158 |
|
|
|
|
| 159 |
message.submit(submit_message, [chatbot, message], [chatbot, message]).then(
|
| 160 |
bot, chatbot, chatbot
|
| 161 |
)
|
|
|
|
| 39 |
|
| 40 |
|
| 41 |
def process_file(file, chunk_size, chunk_overlap):
|
| 42 |
+
docs = split_file(file, chunk_size, chunk_overlap)
|
| 43 |
+
embeddings = load_embeddings()
|
| 44 |
+
|
| 45 |
file_name, _ = os.path.splitext(os.path.basename(file.name))
|
| 46 |
persist_directory = get_persist_directory(file_name)
|
|
|
|
| 47 |
|
| 48 |
+
print("initializing vector store...", persist_directory)
|
|
|
|
| 49 |
vectordb = Chroma.from_documents(documents=docs, embedding=embeddings,
|
| 50 |
collection_name=file_name, persist_directory=persist_directory)
|
| 51 |
+
print("persisting...", vectordb._client.list_collections())
|
| 52 |
vectordb.persist()
|
| 53 |
return 'Done!', gr.Dropdown.update(choices=get_vector_dbs(), value=file_name)
|
| 54 |
|
|
|
|
| 98 |
llm=llm, retriever=vectordb.as_retriever(), memory=memory)
|
| 99 |
|
| 100 |
|
| 101 |
+
def refresh_collection():
|
| 102 |
+
choices = get_vector_dbs()
|
| 103 |
+
return gr.Dropdown.update(choices=choices, value=choices[0] if choices else None)
|
| 104 |
+
|
| 105 |
+
|
| 106 |
def submit_message(bot_history, text):
|
| 107 |
bot_history = bot_history + [(text, None)]
|
| 108 |
return bot_history, ""
|
|
|
|
| 136 |
|
| 137 |
with gr.Tab("Bot"):
|
| 138 |
with gr.Row():
|
| 139 |
+
with gr.Column():
|
| 140 |
+
with gr.Row():
|
| 141 |
+
with gr.Column(scale=3):
|
| 142 |
+
choices = get_vector_dbs()
|
| 143 |
+
collection = gr.Dropdown(
|
| 144 |
+
choices, value=choices[0] if choices else None, label="Document", allow_custom_value=True)
|
| 145 |
+
with gr.Column():
|
| 146 |
+
refresh = gr.Button("Refresh")
|
| 147 |
+
|
| 148 |
temperature = gr.Slider(
|
| 149 |
0.0, 1.0, value=0.5, step=0.05, label="Temperature")
|
| 150 |
max_length = gr.Slider(
|
| 151 |
20, 1000, value=100, step=10, label="Max Length")
|
| 152 |
|
| 153 |
+
with gr.Column():
|
| 154 |
chatbot = gr.Chatbot([], elem_id="chatbot").style(height=550)
|
| 155 |
message = gr.Textbox(
|
| 156 |
show_label=False, placeholder="Ask me anything!")
|
|
|
|
| 167 |
temperature.change(create_qa_chain, [collection, temperature, max_length])
|
| 168 |
max_length.change(create_qa_chain, [collection, temperature, max_length])
|
| 169 |
|
| 170 |
+
refresh.click(refresh_collection, None, collection)
|
| 171 |
message.submit(submit_message, [chatbot, message], [chatbot, message]).then(
|
| 172 |
bot, chatbot, chatbot
|
| 173 |
)
|