-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtvcf_documentUploader.py
More file actions
63 lines (50 loc) · 2.36 KB
/
tvcf_documentUploader.py
File metadata and controls
63 lines (50 loc) · 2.36 KB
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
import streamlit as st
import toml
from openai import OpenAI
# Load API key from secrets file
with open("api_secrets.toml", "r") as f:
secrets = toml.load(f)
api_key_secret = secrets["openai"]["api_key"]
client = OpenAI(api_key=api_key_secret)
# Function to list existing vector stores
def list_vector_stores():
vector_stores = client.beta.vector_stores.list()
return vector_stores.data
# Function to create a new vector store
def create_vector_store(vector_store_name):
return client.beta.vector_stores.create(name=vector_store_name)
# Function to upload a file to a vector store
def upload_file_to_vector_store(vector_store_id, file):
file_streams = [file]
file_batch = client.beta.vector_stores.file_batches.upload_and_poll(
vector_store_id=vector_store_id, files=file_streams
)
return file_batch
# Streamlit UI
st.title("Vector Store Manager")
# Section to create a new vector store
st.header("Create Vector Store")
vector_store_name_input = st.text_input("Enter vector store name")
if st.button("Create Vector Store"):
if vector_store_name_input:
new_vector_store = create_vector_store(vector_store_name_input)
st.success(f"Vector store '{vector_store_name_input}' created with ID: {new_vector_store.id}")
else:
st.error("Please enter a name for the new vector store")
# Section to select a vector store and upload a file
st.header("Select Vector Store and Upload File")
vector_stores = list_vector_stores()
vector_store_names = [store.name for store in vector_stores]
selected_vector_store_name = st.selectbox("Select existing vector store", [""] + vector_store_names)
uploaded_file = st.file_uploader("Choose a file")
if st.button("Upload File"):
if selected_vector_store_name and uploaded_file:
selected_vector_store = next(store for store in vector_stores if store.name == selected_vector_store_name)
file_batch = upload_file_to_vector_store(selected_vector_store.id, uploaded_file)
# Update the secrets file with the latest vector_id
secrets["openai"]["vector_id"] = selected_vector_store.id
with open("api_secrets.toml", "w") as f:
toml.dump(secrets, f)
st.success(f"File uploaded to vector store '{selected_vector_store_name}'. Status: {file_batch.status}")
else:
st.error("Please select a vector store and upload a file")