import json import requests import streamlit as st import os from openai import AzureOpenAI 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. swedenclient = AzureOpenAI( api_version = "2023-12-01-preview", azure_endpoint = os.getenv("sweden"), api_key = os.getenv("sweden_key"), ) usclient = AzureOpenAI( api_version = "2023-12-01-preview", azure_endpoint = os.getenv("us"), api_key = os.getenv("us_key"), ) prompt = os.getenv("prompt") headers = {"Authorization": f"Bearer {os.getenv('imageapi')}"} dalle_prompt = os.getenv("dalle_prompt") 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") pain_points = st.text_input("Pain Points for the picture") amount_pictures = st.number_input("Anzahl Bilder", 1 , 2) if st.button("Get Picture from Text"): for i in range(amount_pictures): try: response = usclient.chat.completions.create( model="gpt-4-0613", temperature = 0.2, messages=[ {"role": "system", "content": prompt + pain_points}, {"role": "system", "content": article} ], ) except Exception as e: st.error(f"ChatGPT Error {e}") st.code(response.choices[0].message.content) try: st.code(response.choices[0].message.content + dalle_prompt) responseimg = swedenclient.images.generate( model="dalle-3", prompt = response.choices[0].message.content + dalle_prompt, n = 1 ) except Exception as e2: st.error(f"Dall-E Error {e2}") image_url = json.loads(responseimg.model_dump_json())['data'][0]['url'] responseimg2 = requests.get(image_url) if responseimg2.status_code ==200: st.image(responseimg2.content) else: st.error("Failed download")