-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
82 lines (61 loc) · 2.56 KB
/
app.py
File metadata and controls
82 lines (61 loc) · 2.56 KB
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
import streamlit as st
import openai
import requests
openai.api_key = st.secrets["api_key"]
st.title("Make your own Image using ChatGPT Plus DALL-E")
with st.form("form"):
user_input = st.text_input("Prompt")
size = st.selectbox("Size", ["1024x1024", "512x512", "256x256"])
submit = st.form_submit_button("Submit")
if submit and user_input:
gpt_prompt = [{
"role": "system",
"content": "Imagine the detail appeareance of the input. Response it shortly around 20 words"
}]
gpt_prompt.append({
"role": "user",
"content": user_input
})
with st.spinner("Waiting for ChatGPT..."):
gpt_response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=gpt_prompt
)
prompt = gpt_response["choices"][0]["message"]["content"]
st.write(prompt)
with st.spinner("Waiting for DALL-E..."):
dalle_response = openai.Image.create(
prompt=prompt,
size=size
)
# 생성된 이미지 URL을 세션 상태에 저장
st.session_state.image_url = dalle_response["data"][0]["url"]
# 이미지를 표시. 이미 생성된 이미지가 있는 경우 세션 상태에서 가져와 표시
if 'image_url' in st.session_state:
st.image(st.session_state.image_url)
# 사용자에게 앨범 정보를 입력받기.
atitle = st.text_input("앨범 제목")
atype_options = ["App", "Card", "Web"]
atype = st.radio("앨범 유형 선택", atype_options)
anote = st.text_input("앨범 설명")
save_button = st.button("장고에 저장")
if save_button:
django_api_endpoint = "http://localhost/api/album_insert/"
# 이미지 데이터를 가져옵니다.
image_data = requests.get(st.session_state.image_url).content
files = {"ufile": ("image.jpg", image_data)}
response = requests.post(django_api_endpoint, files=files, data={
"a_title": atitle,
"a_type": atype,
"a_note": anote,
})
if response.status_code == 200:
if response.json().get("success"):
st.success(response.json().get("message"))
st.markdown(
'<a href="http://localhost/album/" target="_blank" style="display: inline-block; text-align: center; border: 1px solid #FF4B4B; padding: 10px 20px; margin: 20px 0px; border-radius: 3px; color: white; background-color: #FF4B4B; text-decoration: none;">앨범 보기</a>',
unsafe_allow_html=True)
else:
st.error("이미지 저장에 실패했습니다.")
else:
st.error(f"서버 오류: {response.status_code}")