Add1E commited on
Commit
ba9de2c
verified
1 Parent(s): a06f1d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -33
app.py CHANGED
@@ -34,46 +34,63 @@ if not check_password():
34
  st.stop() # Do not continue if check_password is not True.
35
 
36
 
37
- openai.api_type = "azure"
38
- openai.api_version = "2023-12-01-preview"
39
- openai.api_base = "https://tensora-oai-france.openai.azure.com/"
40
- openai.api_key = os.getenv("france_key")
 
41
 
42
 
 
 
 
 
 
 
 
 
 
 
 
43
  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."
44
  headers = {"Authorization": f"Bearer {os.getenv('imageapi')}"}
45
  url = "https://api.edenai.run/v2/image/generation"
46
 
47
 
48
  article = st.text_input("Newsarticle to Image")
 
 
49
 
50
  if st.button("Get Picture from Text"):
51
- try:
52
- response = openai.ChatCompletion.create(
53
- engine="gpt-4-0613",
54
- temperature = 0.2,
55
- messages=[
56
- {"role": "system", "content": prompt},
57
- {"role": "system", "content": article}
58
- ],
59
- )
60
- except Exception as e:
61
- st.error(f"ChatGPT Error {e}")
62
- st.write(response.choices[0].message["content"])
63
- payload = {
64
- "providers": "openai",
65
- "settings" : {"openai": "dall-e-3"},
66
- "text": response.choices[0].message["content"],
67
- "resolution": "1024x1024",
68
- "fallback_providers": "openai"
69
- }
70
-
71
- #st.code(payload)
72
-
73
- response = requests.post(url, json=payload, headers=headers)
74
- result = json.loads(response.text)
75
- responseimg = requests.get(result['openai']['items'][0]['image_resource_url'])
76
- if response.status_code == 200:
77
- st.image(responseimg.content)
78
- else:
79
- st.error("Failed download")
 
 
 
 
34
  st.stop() # Do not continue if check_password is not True.
35
 
36
 
37
+ swedenclient = AzureOpenAI(
38
+ api_version = "2023-12-01-preview",
39
+ azure_endpoint = os.getenv("sweden"),
40
+ api_key = os.getenv("sweden_key"),
41
+ )
42
 
43
 
44
+ usclient = AzureOpenAI(
45
+ api_version = "2023-12-01-preview",
46
+ azure_endpoint = os.getenv("us"),
47
+ api_key = os.getenv("us_key"),
48
+ )
49
+
50
+
51
+ prompt = os.getenv("prompt")
52
+ headers = {"Authorization": f"Bearer {os.getenv('imageapi')}"}
53
+ dalle_prompt = os.getenv("dalle_prompt")
54
+
55
  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."
56
  headers = {"Authorization": f"Bearer {os.getenv('imageapi')}"}
57
  url = "https://api.edenai.run/v2/image/generation"
58
 
59
 
60
  article = st.text_input("Newsarticle to Image")
61
+ pain_points = st.text_input("Pain Points for the picture")
62
+ amount_pictures = st.number_input("Anzahl Bilder", 1 , 2)
63
 
64
  if st.button("Get Picture from Text"):
65
+ for i in range(amount_pictures):
66
+ try:
67
+ response = usclient.chat.completions.create(
68
+ model="gpt-4-0613",
69
+ temperature = 0.2,
70
+ messages=[
71
+ {"role": "system", "content": prompt + pain_points},
72
+ {"role": "system", "content": article}
73
+ ],
74
+ )
75
+ except Exception as e:
76
+ st.error(f"ChatGPT Error {e}")
77
+ st.code(response.choices[0].message.content)
78
+
79
+ try:
80
+ st.code(response.choices[0].message.content + dalle_prompt)
81
+ responseimg = swedenclient.images.generate(
82
+ model="dalle-3",
83
+ prompt = response.choices[0].message.content + dalle_prompt,
84
+ n = 1
85
+ )
86
+ except Exception as e2:
87
+ st.error(f"Dall-E Error {e2}")
88
+
89
+ image_url = json.loads(responseimg.model_dump_json())['data'][0]['url']
90
+
91
+ responseimg2 = requests.get(image_url)
92
+ if responseimg2.status_code ==200:
93
+ st.image(responseimg2.content)
94
+ else:
95
+ st.error("Failed download")
96
+