startrz commited on
Commit
c89022e
·
verified ·
1 Parent(s): 77440ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -53
app.py CHANGED
@@ -1,63 +1,43 @@
1
- import requests
2
- import tiktoken
3
  import streamlit as st
4
- import json
5
- import time
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
- # Function to calculate token usage using tiktoken
16
- def calculate_tokens(text):
17
- encoding = tiktoken.get_encoding("cl100k_base")
18
- tokens = encoding.encode(text)
19
- return tokens
20
-
21
- # Streamlit app
22
- st.title("API Query with Token Calculation")
23
-
24
- # User input for the question
25
- question = st.text_input("Enter your question:", "Hey, how are you?")
26
-
27
- # Button to trigger the API call and calculations
28
- if st.button("Submit"):
29
- with st.spinner("Calculating..."):
30
- # Payload for the API
31
- payload = {"question": question}
32
-
33
- # Measure the response time
34
- start_time = time.time() # Start the timer
35
-
36
- # Query the API
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
- # Display the data
62
- st.subheader("Response Data")
63
- st.json(data)
 
 
 
 
 
 
 
 
 
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()