-
Notifications
You must be signed in to change notification settings - Fork 1
/
journal.py
224 lines (189 loc) · 8.32 KB
/
journal.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import streamlit as st
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
from datetime import datetime
import streamlit_authenticator as stauth
import whisper
from pydub import AudioSegment
import os
from audio_recorder_streamlit import audio_recorder
TEXT_HEIGHT = 300
# change logo to a book
st.set_page_config(
page_title="Personal Journal",
page_icon="📖"
)
audio_tags = {'comments': 'Converted using pydub!'}
upload_path = "uploads/"
download_path = "downloads/"
uploaded_file = "audiofile.wav"
try:
os.mkdir(upload_path)
except:
pass
try:
os.mkdir(download_path)
except:
pass
def to_mp3(audio_file, output_audio_file, upload_path, download_path):
## Converting Different Audio Formats To MP3 ##
if audio_file.split('.')[-1].lower()=="wav":
audio_data = AudioSegment.from_wav(os.path.join(upload_path,audio_file))
audio_data.export(os.path.join(download_path,output_audio_file), format="mp3", tags=audio_tags)
elif audio_file.split('.')[-1].lower()=="mp3":
audio_data = AudioSegment.from_mp3(os.path.join(upload_path,audio_file))
audio_data.export(os.path.join(download_path,output_audio_file), format="mp3", tags=audio_tags)
elif audio_file.split('.')[-1].lower()=="ogg":
audio_data = AudioSegment.from_ogg(os.path.join(upload_path,audio_file))
audio_data.export(os.path.join(download_path,output_audio_file), format="mp3", tags=audio_tags)
elif audio_file.split('.')[-1].lower()=="wma":
audio_data = AudioSegment.from_file(os.path.join(upload_path,audio_file),"wma")
audio_data.export(os.path.join(download_path,output_audio_file), format="mp3", tags=audio_tags)
elif audio_file.split('.')[-1].lower()=="aac":
audio_data = AudioSegment.from_file(os.path.join(upload_path,audio_file),"aac")
audio_data.export(os.path.join(download_path,output_audio_file), format="mp3", tags=audio_tags)
elif audio_file.split('.')[-1].lower()=="flac":
audio_data = AudioSegment.from_file(os.path.join(upload_path,audio_file),"flac")
audio_data.export(os.path.join(download_path,output_audio_file), format="mp3", tags=audio_tags)
elif audio_file.split('.')[-1].lower()=="flv":
audio_data = AudioSegment.from_flv(os.path.join(upload_path,audio_file))
audio_data.export(os.path.join(download_path,output_audio_file), format="mp3", tags=audio_tags)
elif audio_file.split('.')[-1].lower()=="mp4":
audio_data = AudioSegment.from_file(os.path.join(upload_path,audio_file),"mp4")
audio_data.export(os.path.join(download_path,output_audio_file), format="mp3", tags=audio_tags)
return output_audio_file
@st.cache_data()
def process_audio(filename):
with open(os.path.join(upload_path, uploaded_file),"wb") as f:
f.write(audio_bytes)
with st.spinner(f"Processing Audio ... 💫"):
output_audio_file = uploaded_file.split('.')[0] + '.mp3'
output_audio_file = to_mp3(uploaded_file, output_audio_file, upload_path, download_path)
# audio_file = open(os.path.join(download_path,output_audio_file), 'rb')
# audio_bytes = audio_file.read()
with st.spinner(f"Generating Transcript... 💫"):
filename = str(os.path.abspath(os.path.join(download_path, output_audio_file)))
model = whisper.load_model("base")
result = model.transcribe(filename)
return result["text"]
def reset_modifying():
st.session_state["modifying"] = False
authenticator = stauth.Authenticate(
dict(st.secrets['credentials']).copy(),
st.secrets['cookie']['name'],
st.secrets['cookie']['key'],
st.secrets['cookie']['expiry_days']
)
name, authentication_status, username = authenticator.login('Login', 'main')
# print(name, authentication_status, username)
if authentication_status:
authenticator.logout('Logout', 'sidebar')
# Initialize connection.
@st.cache_resource
def init_connection():
uri = st.secrets["mongo"].uri
return MongoClient(uri, server_api=ServerApi('1'))
client = init_connection()
# Pull data from the collection.
# Uses st.cache_data to only rerun when the query changes or after 10 min.
@st.cache_data(ttl=600)
def get_data(num_entries, start_date, end_date):
start_date = datetime.combine(start_date, datetime.max.time())
end_date = datetime.combine(end_date, datetime.min.time())
try:
items = client.journal_entries.entries.find(
{"date": {"$gte": end_date, "$lte": start_date}},
limit=num_entries,
)
items = list(items)
except Exception as e:
st.warning("Failed to load from MongoDB collection.")
st.error(repr(e))
items = []
return items
if not st.session_state.get("modifying", False):
# Add a new entry.
col1, col2 = st.columns(2)
with col1:
st.subheader("New Entry")
# audio upload
with col2:
audio_bytes = audio_recorder(
text="",
icon_size="3x",
)
if audio_bytes is not None:
try:
transcript = process_audio(audio_bytes)
except Exception as e:
st.warning("There was an issue while processing the audio, try again!")
transcript = None
print(e)
else:
transcript = None
entry = st.text_area(label="Entry", height=TEXT_HEIGHT, label_visibility="hidden", value=transcript)
submit = st.button("Add entry", use_container_width=True)
st.divider()
if submit and entry:
date = datetime.now()
client.journal_entries.entries.insert_one({"date": date, "entry": entry})
get_data.clear()
st.experimental_rerun()
# sidebar to filter by date
st.sidebar.subheader("Filter by date")
today = datetime.now()
start_date = st.sidebar.date_input("Start date", today)
one_year_ago = today.replace(year=today.year - 1)
end_date = st.sidebar.date_input("End date", one_year_ago)
# number of entries
st.sidebar.subheader("Number of entries")
num_entries = st.sidebar.slider(
"Select number of entries", 1, 10, 3
)
# Show previous 5 previous entries.
st.subheader("Previous Entries")
items = get_data(num_entries, start_date, end_date)
for item in items:
date = item["date"].strftime("%b %d, %Y")
st.markdown(f"**{date} -** {item['entry']}")
modify = st.button("Modify", key="modify_"+repr(item["_id"]),
use_container_width=True)
# option to modify
if modify:
st.session_state["modifying"] = True
st.session_state["entry"] = item["entry"]
st.session_state["date"] = item["date"]
st.session_state["id"] = item["_id"]
st.experimental_rerun()
else:
# Modify an existing entry.
header_columns = st.columns(3)
header_columns[0].subheader("Modify Entry")
header_columns[2].button("Return", key="come_back", use_container_width=True,
on_click = reset_modifying)
id = st.session_state["id"]
date = st.session_state["date"]
entry = st.text_area("Entry", st.session_state["entry"], height=TEXT_HEIGHT,
label_visibility="hidden")
col1, col2 = st.columns(2)
delete = col1.button("Delete entry", use_container_width=True)
if delete:
client.journal_entries.entries.delete_one(
{"_id": id},
)
st.session_state["modifying"] = False
get_data.clear()
st.experimental_rerun()
submit = col2.button("Modify entry", use_container_width=True)
if submit:
client.journal_entries.entries.update_one(
{"_id": id},
{"$set": {"entry": entry}},
)
st.session_state["modifying"] = False
get_data.clear()
st.experimental_rerun()
elif authentication_status == False:
st.error('Username/password is incorrect')
elif authentication_status == None:
st.warning('Please enter your username and password')