-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
195 lines (161 loc) · 5.8 KB
/
app.py
File metadata and controls
195 lines (161 loc) · 5.8 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from io import BytesIO
import streamlit as st
from audiorecorder import audiorecorder # type: ignore
from dotenv import dotenv_values
from openai import OpenAI
from hashlib import md5
from qdrant_client import QdrantClient
from qdrant_client.models import PointStruct, Distance, VectorParams
env = dotenv_values(".env")
# Secrets using Streamlit Cloud Mechanism
if 'QDRANT_URL' in st.secrets:
env['QDRANT_URL'] = st.secrets['QDRANT_URL']
if 'QDRANT_API_KEY' in st.secrets:
env['QDRANT_API_KEY'] = st.secrets['QDRANT_API_KEY']
EMBEDDING_MODEL = "text-embedding-3-large"
EMBEDDING_DIM = 3072
QDRANT_COLLECTION_NAME = "notes"
AUDIO_TRANSCRIBE_MODEL = "whisper-1"
def get_openai_client():
return OpenAI(api_key=st.session_state["openai_api_key"])
def transcribe_audio(audio_bytes):
openai_client = get_openai_client()
audio_file = BytesIO(audio_bytes)
audio_file.name = "audio.mp3"
transcript = openai_client.audio.transcriptions.create(
file=audio_file,
model=AUDIO_TRANSCRIBE_MODEL,
response_format="verbose_json",
)
return transcript.text
#
# DB
#
@st.cache_resource
def get_qdrant_client():
return QdrantClient(
url=env["QDRANT_URL"],
api_key=env["QDRANT_API_KEY"],
)
def assure_db_collection_exists():
qdrant_client = get_qdrant_client()
if not qdrant_client.collection_exists(QDRANT_COLLECTION_NAME):
print("Tworzę kolekcję")
qdrant_client.create_collection(
collection_name=QDRANT_COLLECTION_NAME,
vectors_config=VectorParams(
size=EMBEDDING_DIM,
distance=Distance.COSINE,
),
)
else:
print("Kolekcja już istnieje")
def get_embedding(text):
openai_client = get_openai_client()
result = openai_client.embeddings.create(
input=[text],
model=EMBEDDING_MODEL,
dimensions=EMBEDDING_DIM,
)
return result.data[0].embedding
def add_note_to_db(note_text):
qdrant_client = get_qdrant_client()
points_count = qdrant_client.count(
collection_name=QDRANT_COLLECTION_NAME,
exact=True,
)
qdrant_client.upsert(
collection_name=QDRANT_COLLECTION_NAME,
points=[
PointStruct(
id=points_count.count + 1,
vector=get_embedding(text=note_text),
payload={
"text": note_text,
},
)
]
)
def list_notes_from_db(query=None):
qdrant_client = get_qdrant_client()
if not query:
notes = qdrant_client.scroll(collection_name=QDRANT_COLLECTION_NAME, limit=10)[0]
result = []
for note in notes:
result.append({
"text": note.payload["text"],
"score": None,
})
return result
else:
notes = qdrant_client.search(
collection_name=QDRANT_COLLECTION_NAME,
query_vector=get_embedding(text=query),
limit=10,
)
result = []
for note in notes:
result.append({
"text": note.payload["text"],
"score": note.score,
})
return result
#
# MAIN
#
st.set_page_config(page_title="Transcript me", layout="centered")
#OpenAI API key protection
if not st.session_state.get("openai_api_key"):
if "OPENAI_API_KEY" in env:
st.session_state["openai_api_key"] = env["OPENAI_API_KEY"]
else:
st.info("Add your OpenAI AI key to use this application")
st.session_state["openai_api_key"] = st.text_input("API key", type="password")
if st.session_state["openai_api_key"]:
st.rerun()
#Stop rendering until theres no openai key
if not st.session_state.get("openai_api_key"):
st.stop()
# Session state initialization
if "note_audio_bytes_md5" not in st.session_state:
st.session_state["note_audio_bytes_md5"] = None
if "note_audio_bytes" not in st.session_state:
st.session_state["note_audio_bytes"] = None
if "note_text" not in st.session_state:
st.session_state["note_text"] = ""
if "note_audio_text" not in st.session_state:
st.session_state["note_audio_text"] = ""
st.title(":studio_microphone: Transcript me")
assure_db_collection_exists()
add_tab, search_tab = st.tabs(["Add", "Search"])
with add_tab:
note_audio = audiorecorder(
start_prompt="Start recording",
stop_prompt="Stop recording",
)
if note_audio:
audio = BytesIO()
note_audio.export(audio, format="mp3")
st.session_state["note_audio_bytes"] = audio.getvalue()
current_md5 = md5(st.session_state["note_audio_bytes"]).hexdigest()
if st.session_state["note_audio_bytes_md5"] != current_md5:
st.session_state["note_audio_text"] = ""
st.session_state["note_text"] = ""
st.session_state["note_audio_bytes_md5"] = current_md5
st.audio(st.session_state["note_audio_bytes"], format="audio/mp3")
if st.button("Transcribe the audio"):
st.session_state["note_audio_text"] = transcribe_audio(st.session_state["note_audio_bytes"])
if st.session_state["note_audio_text"]:
st.session_state["note_text"] = st.text_area("Edit note", value=st.session_state["note_audio_text"])
if st.session_state["note_text"] and st.button("Save note", disabled=not st.session_state["note_text"]):
qdrant_client = get_qdrant_client()
add_note_to_db(note_text=st.session_state["note_text"])
st.toast("Note saved", icon="🎉")
with search_tab:
query = st.text_input("Search for a note")
if st.button("Search"):
for note in list_notes_from_db(query):
with st.container(border=True):
st.markdown(note["text"])
if note["score"]:
st.markdown(f':violet[{note["score"]}]')