breathaudio-frontend/main.py
2023-05-08 15:28:29 +00:00

142 lines
4.4 KiB
Python

import requests
# Copyright (C) 2023 François Pelletier - Je valide ça, service-conseil
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import streamlit as st
from pydantic import BaseModel
backend_url = os.environ.get('BACKEND')
class AudioProperties(BaseModel):
"""
Audio properties
"""
main_frequency: int
lag_frequency: int
volume: float
breath_pattern_in: int
breath_pattern_out: int
bell_sound: str
class VideoProperties(BaseModel):
"""
Video properties
"""
main_frequency: int
breath_pattern_in: int
breath_pattern_out: int
color_scheme = str
def write_app():
st.title("Audio Respiration")
st.write("Cette application permet de générer des visualisation avec audio pour rythmer la respiration")
# Select main_frequency with a slider from 30 to 3000
main_frequency = st.sidebar.slider(
"Choisissez la fréquence de la portion gauche",
min_value=30,
max_value=3000,
value=3000,
step=10
)
# Select lag_frequency with a slider from 30 to 3000
lag_frequency = st.sidebar.slider(
"Choisissez la fréquence de la portion droite",
min_value=main_frequency - 100,
max_value=main_frequency + 100,
value=main_frequency,
step=1
)
# Select volume from 0 to 1 by 0.01 increments
volume = st.sidebar.slider(
"Choisissez le volume",
min_value=0.0,
max_value=1.0,
value=0.01,
step=0.01
)
# Select length of breathing in seconds with a slider from 0 to 30
breath_pattern_in = st.sidebar.slider(
"Choisissez la durée d'inspiration",
min_value=0,
max_value=30,
value=4,
step=1
)
# Select length of breathing out seconds with a slider from 0 to 30
breath_pattern_out = st.sidebar.slider(
"Choisissez la durée d'expiration",
min_value=0,
max_value=30,
value=4,
step=1
)
# Choisir le son de cloche parmi une liste
bell_sound = st.sidebar.selectbox(
"Choisissez le son de cloche",
options=["Cloche", "Bol tibétain"]
)
# Choisir la couleur principale de la visualisation
color_scheme = st.sidebar.selectbox(
"Choisissez la couleur principale de la visualisation",
options=["Rouge", "Orange", "Jaune", "Vert", "Bleu", "Indigo", "Violet"]
)
# Bouton pour générer l'audio
generate_audio = st.sidebar.button("Générer l'audio")
if generate_audio:
ap = AudioProperties(
main_frequency=main_frequency,
lag_frequency=lag_frequency,
volume=volume,
breath_pattern_in=breath_pattern_in,
breath_pattern_out=breath_pattern_out,
bell_sound=bell_sound
)
audio_response = requests.get(
backend_url + "/generate_audio",
json=ap.dict(),
headers={"Content-Type": "application/json"})
st.write(audio_response.reason)
# Save the generated audio to a file
with open("audio.wav", "wb") as audio_file:
audio_file.write(audio_response.content)
st.audio("audio.wav")
generate_video = st.sidebar.button("Générer la vidéo")
if generate_video:
vp = VideoProperties(
main_frequency=main_frequency,
breath_pattern_in=breath_pattern_in,
breath_pattern_out=breath_pattern_out,
color_scheme=color_scheme
)
video_response = requests.get(
backend_url + "/generate_video",
json=vp.dict(),
headers={"Content-Type": "application/json"})
st.write(video_response.reason)
# Save the generated video to a file
with open("video.gif", "wb") as video_file:
video_file.write(video_response.content)
st.image("video.gif")
st.sidebar.write("App réalisé par François Pelletier")
def main():
write_app()
if __name__ == "__main__":
main()