Update app.py
Browse files
app.py
CHANGED
|
@@ -9,10 +9,16 @@ def query(payload):
|
|
| 9 |
return response.json()
|
| 10 |
|
| 11 |
def main():
|
| 12 |
-
st.title("Audio Upload
|
| 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 |
if audio_file is not None:
|
| 18 |
# Convert the audio file to a Base64 string
|
|
@@ -20,25 +26,45 @@ def main():
|
|
| 20 |
audio_base64 = base64.b64encode(audio_bytes).decode('utf-8')
|
| 21 |
audio_mime_type = audio_file.type # Get the mime type
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
payload =
|
| 25 |
-
|
| 26 |
-
{
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
with st.spinner("Sending request..."):
|
| 39 |
output = query(payload)
|
| 40 |
st.success("Request sent successfully!")
|
| 41 |
st.json(output) # Display the API response
|
|
|
|
|
|
|
| 42 |
|
| 43 |
if __name__ == "__main__":
|
| 44 |
main()
|
|
|
|
| 9 |
return response.json()
|
| 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 |
+
# File uploader for image files
|
| 18 |
+
image_file = st.file_uploader("Upload an image file (e.g., png, jpg, jpeg)", type=["png", "jpg", "jpeg"])
|
| 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
|
|
|
|
| 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": f'data:{audio_mime_type};base64,{audio_base64}',
|
| 33 |
+
"type": 'audio',
|
| 34 |
+
"name": audio_file.name,
|
| 35 |
+
"mime": audio_mime_type
|
| 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 |
+
# Add a question to the payload if needed
|
| 56 |
+
if st.text_input("Enter your question", "Can you describe the image?"):
|
| 57 |
+
payload["question"] = st.text_input("Enter your question", "Can you describe the image?")
|
| 58 |
+
|
| 59 |
+
# Send the request to the API and get the output
|
| 60 |
+
if st.button("Submit"):
|
| 61 |
+
if payload:
|
| 62 |
with st.spinner("Sending request..."):
|
| 63 |
output = query(payload)
|
| 64 |
st.success("Request sent successfully!")
|
| 65 |
st.json(output) # Display the API response
|
| 66 |
+
else:
|
| 67 |
+
st.error("Please upload at least one audio or image file.")
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
| 70 |
main()
|