import json import requests import streamlit as st import os import openai import hmac def check_password(): """Returns `True` if the user had the correct password.""" def password_entered(): """Checks whether a password entered by the user is correct.""" if hmac.compare_digest(st.session_state["password"], os.environ.get("PASSWORT")): st.session_state["password_correct"] = True del st.session_state["password"] # Don't store the password. else: st.session_state["password_correct"] = False # Return True if the password is validated. if st.session_state.get("password_correct", False): return True # Show input for password. st.text_input( "Password", type="password", on_change=password_entered, key="password" ) if "password_correct" in st.session_state: st.error("😕 Password incorrect") return False if not check_password(): st.stop() # Do not continue if check_password is not True. openai.api_type = "azure" openai.api_version = "2023-12-01-preview" openai.api_base = "https://tensora-oai-france.openai.azure.com/" openai.api_key = os.getenv("france_key") prompt = "Please analyze a complete news article and generate a suitable DALL·E prompt in response. The prompt should provide a general overview without being overly specific and the picture should be easy to interpret. For technical purposes, please only provide the DALL·E prompt without any additional information." headers = {"Authorization": f"Bearer {os.getenv('imageapi')}"} url = "https://api.edenai.run/v2/image/generation" article = st.text_input("Newsarticle to Image") if st.button("Get Picture from Text"): try: response = openai.ChatCompletion.create( engine="gpt-4-0613", temperature = 0.2, messages=[ {"role": "system", "content": prompt}, {"role": "system", "content": article} ], ) except Exception as e: st.error(f"ChatGPT Error {e}") st.write(response.choices[0].message["content"]) payload = { "providers": "openai", "settings" : {"openai": "dall-e-3"}, "text": response.choices[0].message["content"], "resolution": "1024x1024", "fallback_providers": "openai" } #st.code(payload) response = requests.post(url, json=payload, headers=headers) result = json.loads(response.text) responseimg = requests.get(result['openai']['items'][0]['image_resource_url']) if response.status_code == 200: st.image(responseimg.content) else: st.error("Failed download")