02_Add_Document.py
7.61 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import streamlit as st
import os, json, re, io
from os import path
import requests
import mimetypes
import traceback
import chardet
from utilities.helper import LLMHelper
import uuid
from redis.exceptions import ResponseError
from urllib import parse
from configs.model_config import *
def upload_text_and_embeddings():
file_name = f"{uuid.uuid4()}.txt"
source_url = llm_helper.blob_client.upload_file(st.session_state['doc_text'], file_name=file_name, content_type='text/plain; charset=utf-8')
llm_helper.add_embeddings_lc(source_url)
st.success("Embeddings added successfully.")
def remote_convert_files_and_add_embeddings(process_all=False):
url = os.getenv('CONVERT_ADD_EMBEDDINGS_URL')
if process_all:
url = f"{url}?process_all=true"
try:
response = requests.post(url)
if response.status_code == 200:
st.success(f"{response.text}\nPlease note this is an asynchronous process and may take a few minutes to complete.")
else:
st.error(f"Error: {response.text}")
except Exception as e:
st.error(traceback.format_exc())
def delete_row():
st.session_state['data_to_drop']
redisembeddings.delete_document(st.session_state['data_to_drop'])
def add_urls():
urls = st.session_state['urls'].split('\n')
for url in urls:
if url:
llm_helper.add_embeddings_lc(url)
st.success(f"Embeddings added successfully for {url}")
def upload_file(bytes_data: bytes, file_name: str):
# Upload a new file
st.session_state['filename'] = file_name
st.session_state['file_url'] = llm_helper.local_client.upload_local_file(data=bytes_data, file_name=file_name)
# content_type = mimetypes.MimeTypes().guess_type(file_name)[0]
# charset = f"; charset={chardet.detect(bytes_data)['encoding']}" if content_type == 'text/plain' else ''
# st.session_state['file_url'] = llm_helper.blob_client.upload_file(bytes_data, st.session_state['filename'], content_type=content_type+charset)
try:
# Set page layout to wide screen and menu item
menu_items = {
'Get help': None,
'Report a bug': None,
'About': '''
## Embeddings App
Embedding testing application.
'''
}
st.set_page_config(layout="wide", menu_items=menu_items)
llm_helper = LLMHelper()
with st.expander("Add a single document to the knowledge base", expanded=True):
st.write("For heavy or long PDF, please use the 'Add documents in batch' option below.")
st.checkbox("Translate document to English", key="translate")
uploaded_file = st.file_uploader("Upload a document to add it to the knowledge base", type=['pdf','jpeg','jpg','png', 'txt'])
if uploaded_file is not None:
# To read file as bytes:
bytes_data = uploaded_file.getvalue()
if st.session_state.get('filename', '') != uploaded_file.name:
upload_file(bytes_data, uploaded_file.name)
converted_filename = ''
if uploaded_file.name.endswith('.txt'):
# Add the text to the embeddings
llm_helper.add_embeddings_lc(st.session_state['file_url'])
# else:
# # Get OCR with Layout API and then add embeddigns
# converted_filename = llm_helper.convert_file_and_add_embeddings(st.session_state['file_url'], st.session_state['filename'], st.session_state['translate'])
#
# llm_helper.blob_client.upsert_blob_metadata(uploaded_file.name, {'converted': 'true', 'embeddings_added': 'true', 'converted_filename': parse.quote(converted_filename)})
st.success(f"File {uploaded_file.name} embeddings added to the knowledge base.")
# pdf_display = f'<iframe src="{st.session_state["file_url"]}" width="700" height="1000" type="application/pdf"></iframe>'
with st.expander("添加多个文件位置", expanded=True):
uploaded_files = st.file_uploader("上传多个文件到知识库", type=['pdf','jpeg','jpg','png', 'txt'],key="upload_folder",accept_multiple_files=True)
for uploaded_file in uploaded_files:
# st.write("filename:", uploaded_file.name)
# st.write(bytes_data)
if uploaded_file is not None:
# To read file as bytes:
bytes_data = uploaded_file.read()
# if st.session_state.get('filename', '') != uploaded_file.name:
upload_file(bytes_data, uploaded_file.name)
if uploaded_file.name.endswith('.txt'):
# Add the text to the embeddings
llm_helper.add_embeddings_lc(st.session_state['file_url'])
st.success(f"文件 {uploaded_file.name} 已添加到知识库.")
with st.expander("Add text to the knowledge base", expanded=False):
col1, col2 = st.columns([3,1])
with col1:
st.session_state['doc_text'] = st.text_area("Add a new text content and them click on 'Compute Embeddings'", height=600)
with col2:
st.session_state['embeddings_model'] = st.selectbox('Embeddings models', [llm_helper.get_embeddings_model()['doc']], disabled=True)
st.button("Compute Embeddings", on_click=upload_text_and_embeddings)
with st.expander("Add documents in Batch", expanded=False):
uploaded_files = st.file_uploader("Upload a document to add it to the Azure Storage Account", type=['pdf','jpeg','jpg','png', 'txt'], accept_multiple_files=True)
if uploaded_files is not None:
for up in uploaded_files:
# To read file as bytes:
bytes_data = up.getvalue()
if st.session_state.get('filename', '') != up.name:
# Upload a new file
upload_file(bytes_data, up.name)
if up.name.endswith('.txt'):
# Add the text to the embeddings
llm_helper.blob_client.upsert_blob_metadata(up.name, {'converted': "true"})
col1, col2, col3 = st.columns([2,2,2])
with col1:
st.button("Convert new files and add embeddings", on_click=remote_convert_files_and_add_embeddings)
with col3:
st.button("Convert all files and add embeddings", on_click=remote_convert_files_and_add_embeddings, args=(True,))
with st.expander("Add URLs to the knowledge base", expanded=True):
col1, col2 = st.columns([3,1])
with col1:
st.session_state['urls'] = st.text_area("Add a URLs and than click on 'Compute Embeddings'", placeholder="PLACE YOUR URLS HERE SEPARATED BY A NEW LINE", height=100)
with col2:
st.selectbox('Embeddings models', [llm_helper.get_embeddings_model()['doc']], disabled=True, key="embeddings_model_url")
st.button("Compute Embeddings", on_click=add_urls, key="add_url")
with st.expander("View documents in the knowledge base", expanded=False):
# Query RediSearch to get all the embeddings
try:
data = llm_helper.get_all_documents(k=1000)
if len(data) == 0:
st.warning("No embeddings found. Copy paste your data in the text input and click on 'Compute Embeddings' or drag-and-drop documents.")
else:
st.dataframe(data, use_container_width=True)
except Exception as e:
if isinstance(e, ResponseError):
st.warning("No embeddings found. Copy paste your data in the text input and click on 'Compute Embeddings' or drag-and-drop documents.")
else:
st.error(traceback.format_exc())
except Exception as e:
st.error(traceback.format_exc())