File size: 2,784 Bytes
d032abc
 
 
 
 
a06f1d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d032abc
 
 
 
7779bfe
 
d032abc
 
fc4e32c
d032abc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b076863
d032abc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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")