Spaces:
Runtime error
Runtime error
File size: 2,025 Bytes
0809507 402c1d3 0809507 0e8a2bc 3b7cf58 4bb745d b83dc9c 4bb745d 19f4fce b83dc9c 91caeb5 f5254ad 4bb745d b83dc9c 4bb745d 23a229c b83dc9c 4bb745d b83dc9c |
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 |
import os
import openai
import requests
import streamlit as st
from utils.util import *
from langchain.memory import ConversationBufferMemory
SAVE_DIR = "uploaded_files"
os.makedirs(SAVE_DIR, exist_ok=True)
def init_session_state():
if "openai_api_key" not in st.session_state:
st.session_state.openai_api_key = ""
if "uploaded_files" not in st.session_state:
st.session_state.uploaded_files = os.listdir(SAVE_DIR)
if "huggingface_token" not in st.session_state:
st.session_state.huggingface_token = ""
init_session_state()
st.set_page_config(page_title="RegBotBeta", page_icon="📜🤖")
st.title("Welcome to RegBotBeta2.0")
st.header("Powered by `LlamaIndex🦙`, `Langchain🦜🔗` and `OpenAI API`")
uploaded_files = st.file_uploader(
"Upload Files",
accept_multiple_files=True,
type=["pdf", "docx", "txt", "csv"],
label_visibility="hidden",
)
if uploaded_files:
for file in uploaded_files:
if file not in st.session_state.uploaded_files:
# add the file to session state
st.session_state.uploaded_files.append(file.name)
# save the file to the sample_data directory
with open(os.path.join(SAVE_DIR, file.name), "wb") as f:
f.write(file.getbuffer())
st.success("File(s) uploaded successfully!")
def delete_file(filename):
"""Delete file from session state and local filesystem."""
if filename in st.session_state.uploaded_files and os.path.exists(
os.path.join(SAVE_DIR, filename)
):
st.session_state.uploaded_files.remove(filename)
os.remove(os.path.join(SAVE_DIR, filename))
st.success(f"Deleted {filename}!")
st.rerun()
if st.session_state.uploaded_files:
st.write("Uploaded Files:")
for index, filename in enumerate(st.session_state.uploaded_files):
col1, col2 = st.columns([4, 1])
col1.write(filename)
if col2.button("Delete", key=f"delete_{index}"):
delete_file(filename)
|