Update app.py
Browse files
app.py
CHANGED
|
@@ -1,97 +1,36 @@
|
|
| 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 |
-
|
| 10 |
-
|
| 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 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
payload = {
|
| 21 |
-
"uploads": [
|
| 22 |
-
{
|
| 23 |
-
"data": audio_base64,
|
| 24 |
-
"type": "audio",
|
| 25 |
-
"name": "audio.wav",
|
| 26 |
-
"mime": "audio/webm"
|
| 27 |
-
}
|
| 28 |
-
],
|
| 29 |
-
"overrideConfig": {
|
| 30 |
-
"sessionId": "test"
|
| 31 |
-
}
|
| 32 |
-
}
|
| 33 |
-
|
| 34 |
try:
|
| 35 |
-
response = requests.post(
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
json=payload
|
| 39 |
-
)
|
| 40 |
-
response.raise_for_status()
|
| 41 |
-
return response.json()
|
| 42 |
except requests.exceptions.RequestException as e:
|
| 43 |
-
|
| 44 |
-
return None
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
st.write("Record or upload audio to get predictions")
|
| 49 |
-
|
| 50 |
-
# Add tabs for different input methods
|
| 51 |
-
tab1, tab2 = st.tabs(["Record Audio", "Upload Audio"])
|
| 52 |
-
|
| 53 |
-
with tab1:
|
| 54 |
-
st.write("Click the button below to record audio")
|
| 55 |
-
audio_bytes = audio_recorder()
|
| 56 |
-
|
| 57 |
-
if audio_bytes:
|
| 58 |
-
st.audio(audio_bytes, format="audio/webm")
|
| 59 |
-
|
| 60 |
-
if st.button("Get Prediction for Recorded Audio"):
|
| 61 |
-
with st.spinner("Getting prediction..."):
|
| 62 |
-
audio_base64 = convert_audio_to_base64(audio_bytes)
|
| 63 |
-
if audio_base64:
|
| 64 |
-
prediction = query_prediction_api(audio_base64)
|
| 65 |
-
if prediction:
|
| 66 |
-
st.json(prediction)
|
| 67 |
-
|
| 68 |
-
with tab2:
|
| 69 |
-
uploaded_file = st.file_uploader("Choose an audio file", type=['wav', 'mp3', 'webm'])
|
| 70 |
-
|
| 71 |
-
if uploaded_file:
|
| 72 |
-
audio_bytes = uploaded_file.read()
|
| 73 |
-
st.audio(audio_bytes)
|
| 74 |
-
|
| 75 |
-
if st.button("Get Prediction for Uploaded Audio"):
|
| 76 |
-
with st.spinner("Getting prediction..."):
|
| 77 |
-
audio_base64 = convert_audio_to_base64(audio_bytes)
|
| 78 |
-
if audio_base64:
|
| 79 |
-
prediction = query_prediction_api(audio_base64)
|
| 80 |
-
if prediction:
|
| 81 |
-
st.json(prediction)
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
if __name__ == "__main__":
|
| 97 |
-
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# API URL
|
| 5 |
+
API_URL = "https://startrz-proagents.hf.space/api/v1/prediction/c4349321-8eba-43b6-a5ee-071fd56b8cd1"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# Function to query the API
|
| 8 |
+
def query_api(question):
|
| 9 |
+
payload = {"question": question}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
try:
|
| 11 |
+
response = requests.post(API_URL, json=payload)
|
| 12 |
+
response.raise_for_status() # Raise an error for bad responses
|
| 13 |
+
return response.json() # Return the JSON response
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
except requests.exceptions.RequestException as e:
|
| 15 |
+
return {"error": str(e)} # Return the error message
|
|
|
|
| 16 |
|
| 17 |
+
# Streamlit app title
|
| 18 |
+
st.title("API Query App")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# User input for the question
|
| 21 |
+
user_question = st.text_input("Ask a question:")
|
| 22 |
+
|
| 23 |
+
# Submit button
|
| 24 |
+
if st.button("Submit"):
|
| 25 |
+
if user_question:
|
| 26 |
+
with st.spinner("Fetching response..."):
|
| 27 |
+
output = query_api(user_question)
|
| 28 |
+
if "error" in output:
|
| 29 |
+
st.error(f"Error: {output['error']}")
|
| 30 |
+
else:
|
| 31 |
+
# Display the response from the API
|
| 32 |
+
st.subheader("Response:")
|
| 33 |
+
st.write(output) # Adjust this line based on the expected response structure
|
| 34 |
+
else:
|
| 35 |
+
st.warning("Please enter a question before submitting.")
|
| 36 |
|
|
|
|
|
|