-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
166 lines (134 loc) · 5.69 KB
/
main.py
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
162
163
164
165
import os
import time
import requests
import streamlit as st
from dotenv import load_dotenv
from utils import Typesense
load_dotenv()
TYPESENSE_API_KEY = os.getenv("TYPESENSE_API_KEY")
HUGGINGFACE_API_KEY = os.getenv("HUGGINGFACE_API_KEY")
ts = Typesense(
nodes=[{"host": "localhost", "port": "8108", "protocol": "http"}],
api_key=TYPESENSE_API_KEY,
)
st.set_page_config(page_title="Typesense UI", page_icon="🔍", layout="wide")
page = st.sidebar.radio("Navigation", ["Manage Collections", "Search & Sort"])
if page == "Manage Collections":
st.title("Manage Collections")
action = st.radio("Choose Action", ["Create Collection", "Delete Collection"])
if action == "Create Collection":
st.subheader("Create a New Collection")
collection_name = st.text_input("Collection Name")
st.write("Define Schema")
fields = []
num_fields = st.number_input("Number of Fields", min_value=1, step=1, value=1)
for i in range(num_fields):
col1, col2, col3 = st.columns(3)
with col1:
field_name = st.text_input(f"Field {i+1} Name")
with col2:
field_type = st.selectbox(
f"Field {i+1} Type",
["string", "int32", "float", "bool", "string[]", "auto"],
)
with col3:
is_facet = st.checkbox(f"Facet? {i+1}")
fields.append({"name": field_name, "type": field_type, "facet": is_facet})
schema = {
"name": collection_name,
"fields": fields,
"voice_query_model": {"model_name": "ts/whisper/base.en"},
}
if st.button("Create Collection"):
response = ts.create_collection(schema)
st.success(f"{response}")
st.write("Upload Data File")
uploaded_file = st.file_uploader(
"Upload a CSV, JSONL, XLSX or JSON file",
type=["csv", "json", "jsonl", "xlsx"],
)
file_type = st.selectbox("File Type", ["csv", "json", "jsonl", "xlsx"])
if uploaded_file and collection_name:
if st.button("Upload Data"):
try:
ts.import_documents_into_collection(
collection_name, uploaded_file, file_type
)
st.success("Data uploaded successfully!")
except Exception as e:
st.error(f"Upload Error: {e}")
elif action == "Delete Collection":
st.subheader("Delete a Collection")
collections = ts.get_collection_names()
collection_to_delete = st.selectbox("Select Collection", collections)
if st.button("Delete Collection"):
try:
ts.client.collections[collection_to_delete].delete()
st.success(f"Collection '{collection_to_delete}' deleted successfully!")
except Exception as e:
st.error(f"Error: {e}")
elif page == "Search & Sort":
st.title("Search & Sort Data")
collections = ts.get_collection_names()
collection_name = st.selectbox("Select Collection", collections)
if collection_name:
fields = [
field["name"]
for field in ts.client.collections[collection_name].retrieve()["fields"]
]
search_field = st.selectbox("Search Field", fields)
search_query = st.text_input("Search Query")
sort_field = st.selectbox("Sort By Field", fields)
sort_order = st.radio("Sort Order", ["asc", "desc"])
if st.button("Search"):
try:
results = ts.search_typed_query(
collection_name=collection_name,
query=search_query,
query_by=search_field,
sort_by=sort_field,
sort_order=sort_order,
)
df, found, out_of, time_taken = results
st.write("Search Results")
st.write(
f"Took {time_taken} milliseconds for typesense to search query"
)
st.write(f"Found {found} result(s) out of {out_of}")
st.dataframe(df)
except Exception as e:
st.error(f"Search Error: {e}")
st.write(
"Voice Search: Record a voice message (Working: Using external Whisper from HF)"
)
audio_file = st.audio_input("Record audio")
if audio_file:
start_time = time.time()
audio_data = audio_file.read()
headers = {"Authorization": f"Bearer {HUGGINGFACE_API_KEY}"}
response = requests.post(
"https://api-inference.huggingface.co/models/openai/whisper-large-v3-turbo",
headers=headers,
data=audio_data,
)
transcript = response.json()["text"]
end_time = time.time()
execution_time = end_time - start_time
try:
results = ts.search_typed_query(
collection_name=collection_name,
query=transcript,
query_by=search_field,
sort_by=sort_field,
sort_order=sort_order,
)
df, found, out_of, time_taken = results
st.write("Search Results")
st.write(
f"Took {time_taken} milliseconds for typesense to search query"
)
st.write(f"Took {execution_time:.2f} seconds for generating transcript")
st.write(f"Found {found} result(s) out of {out_of}")
st.dataframe(df)
except Exception as e:
st.error(f"Search Error: {e}")