Hello folks. I’m planning to launch the package into PyPI which is useful to create the text to image converter app with in seconds. For that i need to the check how many free trails are available for the user specified api key. Can anyone help me out in the form of python? I have chatgpt and bard. But they are not working. Hoping that anyone can help with the code?
Here is my code:-
import openai
import time
import streamlit as st
def get_remaining_trials():
#code
def main():
api_key = input('Enter your OpenAI API key: ')
if st.button('Upload key'):
openai.api_key = st.secrets['openai']
if not openai.api_key:
st.error("OpenAI API key not found. Please set the OPENAI_API_KEY environment variable.")
return
# Check remaining trials
remaining_trials = get_remaining_trials()
st.info(f"You have {remaining_trials} trials remaining on your API key.")
if remaining_trials <= 0:
st.error("Your trial limit is fully consumed on your API key. Please upgrade your API key or contact OpenAI support.")
return
text = st.text_input('Enter the text')
if st.button('Generate Images'):
st.info('Please wait for a few seconds!')
text1 = text
if remaining_trials <= 10:
st.warning(f"Caution: You have {remaining_trials} trials remaining on your API key.")
response = openai.Image.create(
prompt=text,
n=4,
size='1024x1024'
)
progress_bar = st.progress(0.0)
status_text = st.empty()
for i in range(100):
progress_bar.progress((i + 1) / 100)
status_text.text(f"Processing {i+1}%")
time.sleep(0.01) # Add a delay to simulate processing time
image_url1 = response['data'][0]['url']
image_url2 = response['data'][1]['url']
image_url3 = response['data'][2]['url']
image_url4 = response['data'][3]['url']
st.markdown(f"<h5 style='text-align: center; color:red'>{text1}</h5>", unsafe_allow_html=True)
cols = st.columns(2)
with cols[0]:
st.image(image_url1)
st.image(image_url2)
with cols[1]:
st.image(image_url3)
st.image(image_url4)
if __name__ == '__main__':
main()