Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,94 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
import base64
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
def
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
def main():
|
| 12 |
-
st.title("Audio and Image Upload for Prediction")
|
| 13 |
-
|
| 14 |
-
# File uploader for audio files
|
| 15 |
-
audio_file = st.file_uploader("Upload an audio file (e.g., mp3, wav, ogg, webm)", type=["mp3", "wav", "ogg", "webm", "m4a", "aac"])
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
# Create payload for the query
|
| 21 |
-
payload = {}
|
| 22 |
-
|
| 23 |
-
if audio_file is not None:
|
| 24 |
-
# Convert the audio file to a Base64 string
|
| 25 |
-
audio_bytes = audio_file.read()
|
| 26 |
-
audio_base64 = base64.b64encode(audio_bytes).decode('utf-8')
|
| 27 |
-
audio_mime_type = audio_file.type # Get the mime type
|
| 28 |
-
|
| 29 |
-
# Add audio data to the payload
|
| 30 |
-
payload["uploads"] = [
|
| 31 |
{
|
| 32 |
-
"data":
|
| 33 |
-
"type":
|
| 34 |
-
"name":
|
| 35 |
-
"mime":
|
| 36 |
}
|
| 37 |
]
|
| 38 |
-
|
| 39 |
-
if image_file is not None:
|
| 40 |
-
# Convert the image file to a Base64 string
|
| 41 |
-
image_bytes = image_file.read()
|
| 42 |
-
image_base64 = base64.b64encode(image_bytes).decode('utf-8')
|
| 43 |
-
image_mime_type = image_file.type # Get the mime type
|
| 44 |
-
|
| 45 |
-
# Add image data to the payload
|
| 46 |
-
payload.setdefault("uploads", []).append(
|
| 47 |
-
{
|
| 48 |
-
"data": f'data:{image_mime_type};base64,{image_base64}',
|
| 49 |
-
"type": 'file',
|
| 50 |
-
"name": image_file.name,
|
| 51 |
-
"mime": image_mime_type
|
| 52 |
-
}
|
| 53 |
-
)
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
if __name__ == "__main__":
|
| 73 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
import base64
|
| 4 |
+
import json
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
import numpy as np
|
| 7 |
+
from audio_recorder_streamlit import audio_recorder
|
| 8 |
|
| 9 |
+
def convert_audio_to_base64(audio_bytes):
|
| 10 |
+
"""Convert audio bytes to base64 string"""
|
| 11 |
+
if audio_bytes is None:
|
| 12 |
+
return None
|
| 13 |
+
base64_str = base64.b64encode(audio_bytes).decode('utf-8')
|
| 14 |
+
return f"data:audio/webm;codecs=opus;base64,{base64_str}"
|
| 15 |
|
| 16 |
+
def query_prediction_api(audio_base64):
|
| 17 |
+
"""Send request to prediction API"""
|
| 18 |
+
url = "http://localhost:3000/api/v1/prediction/<chatlfowid>"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
payload = {
|
| 21 |
+
"uploads": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
{
|
| 23 |
+
"data": audio_base64,
|
| 24 |
+
"type": "audio",
|
| 25 |
+
"name": "audio.wav",
|
| 26 |
+
"mime": "audio/webm"
|
| 27 |
}
|
| 28 |
]
|
| 29 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
try:
|
| 32 |
+
response = requests.post(
|
| 33 |
+
url,
|
| 34 |
+
headers={"Content-Type": "application/json"},
|
| 35 |
+
json=payload
|
| 36 |
+
)
|
| 37 |
+
response.raise_for_status()
|
| 38 |
+
return response.json()
|
| 39 |
+
except requests.exceptions.RequestException as e:
|
| 40 |
+
st.error(f"Error making prediction: {str(e)}")
|
| 41 |
+
return None
|
| 42 |
|
| 43 |
+
def main():
|
| 44 |
+
st.title("Audio Prediction App")
|
| 45 |
+
st.write("Record or upload audio to get predictions")
|
| 46 |
+
|
| 47 |
+
# Add tabs for different input methods
|
| 48 |
+
tab1, tab2 = st.tabs(["Record Audio", "Upload Audio"])
|
| 49 |
+
|
| 50 |
+
with tab1:
|
| 51 |
+
st.write("Click the button below to record audio")
|
| 52 |
+
audio_bytes = audio_recorder()
|
| 53 |
+
|
| 54 |
+
if audio_bytes:
|
| 55 |
+
st.audio(audio_bytes, format="audio/webm")
|
| 56 |
+
|
| 57 |
+
if st.button("Get Prediction for Recorded Audio"):
|
| 58 |
+
with st.spinner("Getting prediction..."):
|
| 59 |
+
audio_base64 = convert_audio_to_base64(audio_bytes)
|
| 60 |
+
if audio_base64:
|
| 61 |
+
prediction = query_prediction_api(audio_base64)
|
| 62 |
+
if prediction:
|
| 63 |
+
st.json(prediction)
|
| 64 |
+
|
| 65 |
+
with tab2:
|
| 66 |
+
uploaded_file = st.file_uploader("Choose an audio file", type=['wav', 'mp3', 'webm'])
|
| 67 |
+
|
| 68 |
+
if uploaded_file:
|
| 69 |
+
audio_bytes = uploaded_file.read()
|
| 70 |
+
st.audio(audio_bytes)
|
| 71 |
+
|
| 72 |
+
if st.button("Get Prediction for Uploaded Audio"):
|
| 73 |
+
with st.spinner("Getting prediction..."):
|
| 74 |
+
audio_base64 = convert_audio_to_base64(audio_bytes)
|
| 75 |
+
if audio_base64:
|
| 76 |
+
prediction = query_prediction_api(audio_base64)
|
| 77 |
+
if prediction:
|
| 78 |
+
st.json(prediction)
|
| 79 |
|
| 80 |
+
st.sidebar.markdown("""
|
| 81 |
+
### About
|
| 82 |
+
This app allows you to:
|
| 83 |
+
- Record audio directly in the browser
|
| 84 |
+
- Upload audio files
|
| 85 |
+
- Get predictions from the API
|
| 86 |
+
|
| 87 |
+
Supported formats:
|
| 88 |
+
- WAV
|
| 89 |
+
- MP3
|
| 90 |
+
- WebM
|
| 91 |
+
""")
|
| 92 |
|
| 93 |
if __name__ == "__main__":
|
| 94 |
main()
|