CoolFace
Apppublic

startrz/OpenAI-API-Key-Usage

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py39 linesDownload Raw Back to root
1import streamlit as st2import requests3import datetime4 5# Function to retrieve usage data for a specific date6def get_usage(api_key, date):7    url = 'https://api.openai.com/v1/dashboard/billing/usage'8    headers = {'Authorization': f'Bearer {api_key}'}9    params = {'date': date.strftime('%Y-%m-%d')}10    11    response = requests.get(url, headers=headers, params=params)12    13    if response.status_code == 200:14        return response.json()15    else:16        st.error(f"Error: {response.status_code} - {response.text}")17        return None18 19# Streamlit application interface20st.title("OpenAI API Usage Tracker")21 22# Input for API key23api_key = st.text_input("Enter your OpenAI API Key", type="password")24 25# Date input26date = st.date_input("Select a date", datetime.date.today())27 28# Display usage data when button is clicked29if st.button("Get Usage Data"):30    if api_key:31        usage_data = get_usage(api_key, date)32        33        if usage_data:34            total_tokens_used = sum(item['n_generated_tokens_total'] + item['n_context_tokens_total'] for item in usage_data['data'])35            st.write(f"Total tokens used on {date}: {total_tokens_used}")36        else:37            st.write("No usage data available for the selected date.")38    else:39        st.warning("Please enter your API key.")