Spaces:
Runtime error
Runtime error
File size: 1,832 Bytes
b80864a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import streamlit as st
from llama_index import SimpleDirectoryReader, GPTVectorStoreIndex
import os
import shutil
def save_file(files):
directory_name = 'tmp_docs'
# Remove existing files in the directory
if os.path.exists(directory_name):
for filename in os.listdir(directory_name):
file_path = os.path.join(directory_name, filename)
try:
if os.path.isfile(file_path):
os.remove(file_path)
except Exception as e:
print(f"Error: {e}")
# Save the new file with original filename
if files is not None:
for file in files:
file_name = file.name
file_path = os.path.join(directory_name, file_name)
with open(file_path, 'wb') as f:
shutil.copyfileobj(file, f)
def ingest(docs_dir):
documents = SimpleDirectoryReader(docs_dir).load_data()
index = GPTVectorStoreIndex.from_documents(documents)
return index
def get_answer(index, message):
response = query(index, message)
return [('Chatbot', ''.join(response.response))]
def query(index, query_text):
query_engine = index.as_query_engine()
response = query_engine.query(query_text)
return response
# Initialize chatbot history
chatbot = []
# Display file upload component
files = st.file_uploader('Upload Files', accept_multiple_files=True)
if files is not None:
save_file(files)
index = ingest('tmp_docs')
# Display message input component
message = st.text_input('Enter message')
# If message is entered, ingest documents and get chatbot response
if message:
chatbot.append(('You', message))
chatbot += get_answer(index, message)
# Display chat history
st.text_area('Chatbot:', value='\n'.join(
[f'{x[0]}: {x[1]}' for x in chatbot]), height=250)
|