first 'working' version

This commit is contained in:
2024-12-29 23:43:26 +01:00
parent 6200763f0b
commit b3c30398c2
11 changed files with 469 additions and 36 deletions
+10 -2
View File
@@ -1,5 +1,8 @@
from functools import partial
import streamlit as st
from webapp.new_task import render_new_task
from webapp.task_list import render_task_list
@@ -16,7 +19,12 @@ def render_home(container,
with container.container():
st.title('Annescribe Home')
st.write("Welcome to AnneScribe!")
authenticator.logout()
authenticator.logout(callback=st.session_state.clear)
# render task list
render_task_list(container, app_config)
task_list_container = st.empty()
render_task_list(task_list_container, app_config)
# render new task
new_task_container = st.empty()
render_new_task(new_task_container, app_config)
+61
View File
@@ -0,0 +1,61 @@
import os
import streamlit as st
from utils import get_logger
from transcription.job_utils import create_job
from transcription.transcription_helper import transcribe
logger = get_logger(__name__)
def create_new_task(filepath: str, app_config, overwrite: bool):
"""
Create a new task
:param filepath: path to the audio file
:param app_config: app configuration
:param overwrite: overwrite existing task
:return: None
"""
logger.info(f"Creating new task for file {filepath}")
if filepath is None:
st.error("No file uploaded")
return
transcribe(filepath, app_config, blocking=False, overwrite=overwrite)
def render_new_task(container, app_config: dict):
"""
Render the new task page
:param container: container object to place the content in
:param app_config: app configuration
:return: None
"""
with container.container():
st.write("Create a new task")
# add checkbox for overwrite
with st.form(key="new_task_form", clear_on_submit=True):
overwrite = st.checkbox("Overwrite existing task", value=False, key="overwrite")
uploaded_file = st.file_uploader("Audio File", type=["wav", "mp3", "flac", "ogg"], key="audio_file")
file_path = None
if uploaded_file is not None:
# save files to upload directory
upload_directory = app_config["data"]["upload_directory"]
# create the upload directory if it does not exist
if not os.path.exists(upload_directory):
logger.info(f"Creating upload directory {upload_directory}")
os.makedirs(upload_directory)
logger.info(f"Saving file {uploaded_file.name} to {upload_directory}")
file_path = f"{upload_directory}/{uploaded_file.name}"
with open(file_path, "wb") as file:
file.write(uploaded_file.getbuffer())
st.success(f"File saved to {file_path}")
st.form_submit_button("Create Task", on_click=create_new_task, args=(file_path, app_config, overwrite))
+50 -20
View File
@@ -4,7 +4,8 @@ import streamlit as st
from streamlit_autorefresh import st_autorefresh
from transcription.job_utils import get_existing_jobs
from transcription.job_utils import get_existing_jobs, remove_job
def show_results(job_data: dict):
"""
@@ -15,9 +16,12 @@ def show_results(job_data: dict):
if job_data is None:
return
else:
st.session_state[f"results_button_{job_data['name']}"] = True
st.write(f"Transcription Results for job {job_data['name']}")
st.write(f"Output: {job_data['output']}")
# print("showing results")
# st.session_state[f"shown_result"] = job_data['name']
# st.write(f"Transcription Results for job {job_data['name']}")
# st.write(f"Output: {job_data['output']}")
with st.popover("Transcription Results", f"Results for job {job_data['name']}"):
st.write(f"Output: {job_data['output']}")
def hide_results(job_data: dict):
"""
@@ -25,7 +29,7 @@ def hide_results(job_data: dict):
:param job_data: job data
:return: None
"""
st.session_state[f"results_button_{job_data['name']}"] = False
st.session_state[f"shown_result"] = None
def results_button(job_data: dict, app_config: dict):
"""
@@ -34,16 +38,33 @@ def results_button(job_data: dict, app_config: dict):
:param app_config: app configuration
:return: None
"""
is_toggled = st.session_state.get(f"results_button_{job_data['name']}")
other_toggled = any([st.session_state.get(f"results_button_{job['name']}") for job in get_existing_jobs(app_config)])
# is_toggled = st.session_state.get(f"shown_result") == job_data['name']
#
# if is_toggled:
# return st.button("Hide Results", on_click=partial(hide_results, job_data), key=f"hide_{job_data['name']}")
# else:
# return st.button("Show Results", on_click=partial(show_results, job_data), key=f"show_{job_data['name']}")
with st.popover("Show Results"):
st.markdown("Results")
st.write(job_data['output'])
@st.dialog("Are you sure?")
def delete_task(job_data: dict, app_config: dict):
"""
Delete a task
:param job_data: job data
:param app_config: app configuration
:return: None
"""
st.write(f"Are you sure you want to delete task {job_data['name']}?")
if st.button("Yes"):
remove_job(job_data['name'], app_config)
st.success(f"Task {job_data['name']} deleted.")
st.rerun()
if other_toggled:
for job in get_existing_jobs(app_config):
st.session_state[f"results_button_{job['name']}"] = False
if is_toggled:
return st.button("Show Results", on_click=partial(show_results, job_data))
else:
return st.button("Hide Results", on_click=partial(hide_results, job_data))
def render_task_list(container, app_config: dict):
@@ -54,15 +75,11 @@ def render_task_list(container, app_config: dict):
:return: None
"""
# disable all results
for job in get_existing_jobs(app_config):
st.session_state[f"results_button_{job['name']}"] = False
existing_jobs = get_existing_jobs(app_config)
with container.container():
st.subheader('Task List')
existing_jobs = get_existing_jobs(app_config)
column_config = [
{
"name": "Name",
@@ -79,19 +96,32 @@ def render_task_list(container, app_config: dict):
{
"name": "Output",
"render_func": lambda x: results_button(x, app_config)
},
{
"name": "Actions",
"render_func": lambda x: st.button("Delete", on_click=partial(delete_task, x, app_config), key=f"delete_{x['name']}")
}
]
if len(existing_jobs) == 0:
st.write("No tasks available.")
else:
# display header
cols = st.columns(len(column_config))
for i, col in enumerate(column_config):
with cols[i]:
st.write(col["name"])
for i, job in enumerate(existing_jobs):
cols = st.columns(len(column_config))
for j, col in enumerate(column_config):
cols[j].write(col["name"])
with cols[j]:
col["render_func"](job)
def update_job_list():
nonlocal existing_jobs
existing_jobs = get_existing_jobs(app_config)
st.button("Refresh", on_click=update_job_list)