Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,43 @@
|
|
| 1 |
-
import requests
|
| 2 |
-
import tiktoken
|
| 3 |
import streamlit as st
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
|
| 7 |
-
# Define the API URL
|
| 8 |
API_URL = "https://startrz-proagents.hf.space/api/v1/prediction/9ad2e52d-052f-44e5-b3a0-7fd432d86506"
|
| 9 |
|
| 10 |
-
# Function to query the API
|
| 11 |
def query(payload):
|
| 12 |
response = requests.post(API_URL, json=payload)
|
| 13 |
return response.json()
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
response_data = query(payload)
|
| 38 |
-
|
| 39 |
-
# End the timer and calculate response time
|
| 40 |
-
response_time = time.time() - start_time
|
| 41 |
-
|
| 42 |
-
# Calculate input tokens
|
| 43 |
-
input_tokens = calculate_tokens(str(payload))
|
| 44 |
-
|
| 45 |
-
# Calculate output tokens from the response (assuming 'result' field contains the output text)
|
| 46 |
-
output_text = response_data.get("text", "")
|
| 47 |
-
output_tokens = calculate_tokens(output_text)
|
| 48 |
-
|
| 49 |
-
# Total tokens
|
| 50 |
-
total_tokens = len(input_tokens) + len(output_tokens)
|
| 51 |
-
|
| 52 |
-
# Structure the data with token information
|
| 53 |
-
data = {
|
| 54 |
-
"prediction": response_data,
|
| 55 |
-
"inputTokens": len(input_tokens),
|
| 56 |
-
"outputTokens": len(output_tokens),
|
| 57 |
-
"totalTokens": total_tokens,
|
| 58 |
-
"predictionTime": f"{round(response_time, 2)} secs"
|
| 59 |
}
|
| 60 |
|
| 61 |
-
#
|
| 62 |
-
st.
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import base64
|
| 4 |
|
|
|
|
| 5 |
API_URL = "https://startrz-proagents.hf.space/api/v1/prediction/9ad2e52d-052f-44e5-b3a0-7fd432d86506"
|
| 6 |
|
|
|
|
| 7 |
def query(payload):
|
| 8 |
response = requests.post(API_URL, json=payload)
|
| 9 |
return response.json()
|
| 10 |
|
| 11 |
+
def main():
|
| 12 |
+
st.title("Audio Upload and Prediction")
|
| 13 |
+
|
| 14 |
+
# File uploader for audio files
|
| 15 |
+
audio_file = st.file_uploader("Upload an audio file (WebM format)", type=["webm"])
|
| 16 |
+
|
| 17 |
+
if audio_file is not None:
|
| 18 |
+
# Convert the audio file to a Base64 string
|
| 19 |
+
audio_bytes = audio_file.read()
|
| 20 |
+
audio_base64 = base64.b64encode(audio_bytes).decode('utf-8')
|
| 21 |
+
audio_mime_type = audio_file.type # Get the mime type
|
| 22 |
+
|
| 23 |
+
# Prepare the payload
|
| 24 |
+
payload = {
|
| 25 |
+
"uploads": [
|
| 26 |
+
{
|
| 27 |
+
"data": f'data:{audio_mime_type};base64,{audio_base64}',
|
| 28 |
+
"type": 'audio',
|
| 29 |
+
"name": audio_file.name,
|
| 30 |
+
"mime": audio_mime_type
|
| 31 |
+
}
|
| 32 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
}
|
| 34 |
|
| 35 |
+
# Send the request to the API and get the output
|
| 36 |
+
if st.button("Submit"):
|
| 37 |
+
with st.spinner("Sending request..."):
|
| 38 |
+
output = query(payload)
|
| 39 |
+
st.success("Request sent successfully!")
|
| 40 |
+
st.json(output) # Display the API response
|
| 41 |
+
|
| 42 |
+
if __name__ == "__main__":
|
| 43 |
+
main()
|