Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import tiktoken
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
# Define the API URL
|
| 7 |
+
API_URL = "https://startrz-proagents.hf.space/api/v1/prediction/9ad2e52d-052f-44e5-b3a0-7fd432d86506"
|
| 8 |
+
|
| 9 |
+
# Function to query the API
|
| 10 |
+
def query(payload):
|
| 11 |
+
response = requests.post(API_URL, json=payload)
|
| 12 |
+
return response.json()
|
| 13 |
+
|
| 14 |
+
# Function to calculate token usage using tiktoken
|
| 15 |
+
def calculate_tokens(text, model="gpt-3.5-turbo"):
|
| 16 |
+
encoding = tiktoken.encoding_for_model(model)
|
| 17 |
+
tokens = encoding.encode(text)
|
| 18 |
+
return tokens
|
| 19 |
+
|
| 20 |
+
# Streamlit app
|
| 21 |
+
st.title("API Query with Token Calculation")
|
| 22 |
+
|
| 23 |
+
# User input for the question
|
| 24 |
+
question = st.text_input("Enter your question:", "Hey, how are you?")
|
| 25 |
+
|
| 26 |
+
# Button to trigger the API call and calculations
|
| 27 |
+
if st.button("Submit"):
|
| 28 |
+
with st.spinner("Calculating..."):
|
| 29 |
+
# Payload for the API
|
| 30 |
+
payload = {"question": question}
|
| 31 |
+
|
| 32 |
+
# Query the API
|
| 33 |
+
response_data = query(payload)
|
| 34 |
+
|
| 35 |
+
# Calculate input tokens
|
| 36 |
+
input_tokens = calculate_tokens(str(payload))
|
| 37 |
+
|
| 38 |
+
# Calculate output tokens from the response (assuming 'result' field contains the output text)
|
| 39 |
+
output_text = response_data.get("result", "")
|
| 40 |
+
output_tokens = calculate_tokens(output_text)
|
| 41 |
+
|
| 42 |
+
# Total tokens
|
| 43 |
+
total_tokens = len(input_tokens) + len(output_tokens)
|
| 44 |
+
|
| 45 |
+
# Structure the data with token information
|
| 46 |
+
data = {
|
| 47 |
+
"response": response_data,
|
| 48 |
+
"input_tokens": len(input_tokens),
|
| 49 |
+
"output_tokens": len(output_tokens),
|
| 50 |
+
"total_tokens": total_tokens
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
# Display the data
|
| 54 |
+
st.subheader("Response Data")
|
| 55 |
+
st.json(data)
|
| 56 |
+
|
| 57 |
+
st.subheader("Token Information")
|
| 58 |
+
st.write(f"Input Tokens: {len(input_tokens)}")
|
| 59 |
+
st.write(f"Output Tokens: {len(output_tokens)}")
|
| 60 |
+
st.write(f"Total Tokens: {total_tokens}")
|