Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import requests
|
3 |
+
import streamlit as st
|
4 |
+
import os
|
5 |
+
import openai
|
6 |
+
|
7 |
+
|
8 |
+
openai.api_type = "azure"
|
9 |
+
openai.api_version = "2023-12-01-preview"
|
10 |
+
openai.api_base = "https://tensora-oai.openai.azure.com/"
|
11 |
+
openai.api_key = os.getenv("us_key")
|
12 |
+
|
13 |
+
|
14 |
+
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. For technical purposes, please only provide the DALL路E prompt without any additional information."
|
15 |
+
headers = {"Authorization": f"Bearer {os.getenv('imageapi')}"}
|
16 |
+
url = "https://api.edenai.run/v2/image/generation"
|
17 |
+
|
18 |
+
|
19 |
+
article = st.text_input("Newsarticle to Image")
|
20 |
+
|
21 |
+
if st.button("Get Picture from Text"):
|
22 |
+
try:
|
23 |
+
response = openai.ChatCompletion.create(
|
24 |
+
engine="gpt-4-0613",
|
25 |
+
temperature = 0.2,
|
26 |
+
messages=[
|
27 |
+
{"role": "system", "content": prompt},
|
28 |
+
{"role": "system", "content": article}
|
29 |
+
],
|
30 |
+
)
|
31 |
+
except Exception as e:
|
32 |
+
st.error(f"ChatGPT Error {e}")
|
33 |
+
#st.write(response.choices[0].message["content"])
|
34 |
+
payload = {
|
35 |
+
"providers": "openai",
|
36 |
+
"settings" : {"openai": "dall-e-3"},
|
37 |
+
"text": response.choices[0].message["content"],
|
38 |
+
"resolution": "1024x1024",
|
39 |
+
"fallback_providers": "openai"
|
40 |
+
}
|
41 |
+
|
42 |
+
#st.code(payload)
|
43 |
+
|
44 |
+
response = requests.post(url, json=payload, headers=headers)
|
45 |
+
result = json.loads(response.text)
|
46 |
+
responseimg = requests.get(result['openai']['items'][0]['image_resource_url'])
|
47 |
+
if response.status_code == 200:
|
48 |
+
st.image(responseimg.content)
|
49 |
+
else:
|
50 |
+
st.error("Failed download")
|