Spaces:
Sleeping
Sleeping
File size: 1,305 Bytes
da04961 bffc389 be1b585 da04961 be1b585 bffc389 be1b585 bffc389 be1b585 bffc389 be1b585 bffc389 be1b585 10d41b6 be1b585 10d41b6 be1b585 bffc389 be1b585 bffc389 be1b585 bffc389 be1b585 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import streamlit as st
import requests
import datetime
# Function to retrieve usage data for a specific date
def get_usage(api_key, date):
url = 'https://api.openai.com/v1/dashboard/billing/usage'
headers = {'Authorization': f'Bearer {api_key}'}
params = {'date': date.strftime('%Y-%m-%d')}
response = requests.get(url, headers=headers, params=params)
if response.status_code == 200:
return response.json()
else:
st.error(f"Error: {response.status_code} - {response.text}")
return None
# Streamlit application interface
st.title("OpenAI API Usage Tracker")
# Input for API key
api_key = st.text_input("Enter your OpenAI API Key", type="password")
# Date input
date = st.date_input("Select a date", datetime.date.today())
# Display usage data when button is clicked
if st.button("Get Usage Data"):
if api_key:
usage_data = get_usage(api_key, date)
if usage_data:
total_tokens_used = sum(item['n_generated_tokens_total'] + item['n_context_tokens_total'] for item in usage_data['data'])
st.write(f"Total tokens used on {date}: {total_tokens_used}")
else:
st.write("No usage data available for the selected date.")
else:
st.warning("Please enter your API key.") |