JamesKim commited on
Commit
77f16cd
β€’
1 Parent(s): 2dc6e3e
app.py CHANGED
@@ -5,9 +5,13 @@ Date: 2023-05-19
5
  """
6
  import os
7
  from typing import Union
 
 
8
  import streamlit as st
9
  import requests
10
  from PIL import Image
 
 
11
 
12
  def download_image(image_url:str,
13
  fname:str=None,
@@ -31,11 +35,11 @@ def download_image(image_url:str,
31
  return None
32
 
33
  def save_rgba_image(uploaded_file_name:str, uploaded_file_buffer:object,
34
- Default_fold="images/"):
35
  """
36
  # Save the uploaded file to a specific location
37
  """
38
- file_path = os.path.join(Default_fold[:-1], uploaded_file_name)
39
  with open(file_path, "wb") as file:
40
  file.write(uploaded_file_buffer)
41
  image_file = Image.open(file_path)
@@ -43,13 +47,34 @@ def save_rgba_image(uploaded_file_name:str, uploaded_file_buffer:object,
43
  image_file_rgba.save(file_path)
44
  return file_path
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  def main():
47
  """
48
  Main function for the streamlit app.
49
  """
 
 
50
  st.set_page_config(page_title="Avatar Generation")
51
  st.header("Artistic Avatar Generation πŸ’–")
52
 
 
 
 
53
  mode = st.radio("Choose your option", ("Upload photo by file", "Upload photo by url"))
54
  photo_filename = None
55
 
@@ -58,15 +83,15 @@ def main():
58
  if uploaded_file is not None:
59
  st.image(uploaded_file, width=256, caption="Your photo")
60
  # Save the uploaded file to a specific location
61
- photo_filename = save_rgba_image(uploaded_file.name, uploaded_file.getbuffer())
 
62
  else:
63
  photo_url = st.text_input("Enter your photo url:",
64
  value="https://pngimg.com/uploads/monkey/monkey_PNG18720.png")
65
  if photo_url:
66
  st.image(photo_url, width=256, caption="Your photo")
67
- photo_filename = download_image(photo_url) # e.g., "images/monkey_PNG18720.png"
68
-
69
- st.write("Your photo filename:", photo_filename)
70
 
71
  if photo_filename:
72
  image = Image.open(photo_filename)
@@ -75,7 +100,17 @@ def main():
75
  prompt = st.text_input("Prompt to transform to avatar: ",
76
  value="Change background to a beautiful sunset over the ocean")
77
  if prompt:
78
- st.write("Prompt:", prompt)
 
 
 
 
 
 
 
 
 
 
79
 
80
  if __name__ == '__main__':
81
  main()
 
5
  """
6
  import os
7
  from typing import Union
8
+ from datetime import datetime
9
+
10
  import streamlit as st
11
  import requests
12
  from PIL import Image
13
+ from langchain.callbacks import get_openai_callback
14
+ import openai
15
 
16
  def download_image(image_url:str,
17
  fname:str=None,
 
35
  return None
36
 
37
  def save_rgba_image(uploaded_file_name:str, uploaded_file_buffer:object,
38
+ default_fold:str="images/"):
39
  """
40
  # Save the uploaded file to a specific location
41
  """
42
+ file_path = os.path.join(default_fold[:-1], uploaded_file_name)
43
  with open(file_path, "wb") as file:
44
  file.write(uploaded_file_buffer)
45
  image_file = Image.open(file_path)
 
47
  image_file_rgba.save(file_path)
48
  return file_path
49
 
50
+ def edit_image(img_name:str,
51
+ prompt:str="change backgroud to a beautiful sunset over the ocean")-> str:
52
+ """
53
+ code is generated by being
54
+ im_size : one of ['256x256', '512x512', '1024x1024'] - 'size'
55
+ """
56
+ response = openai.Image.create_edit(
57
+ image=open(img_name, "rb"),
58
+ prompt=prompt,
59
+ size="256x256",
60
+ n=1,
61
+ response_format="url"
62
+ )
63
+ img_url = response["data"][0]["url"]
64
+ return img_url
65
+
66
  def main():
67
  """
68
  Main function for the streamlit app.
69
  """
70
+ default_fold = "images/"
71
+ # load_dotenv()
72
  st.set_page_config(page_title="Avatar Generation")
73
  st.header("Artistic Avatar Generation πŸ’–")
74
 
75
+ api_key = st.text_input("Enter your OpenAI API key:")
76
+ openai.api_key = api_key
77
+
78
  mode = st.radio("Choose your option", ("Upload photo by file", "Upload photo by url"))
79
  photo_filename = None
80
 
 
83
  if uploaded_file is not None:
84
  st.image(uploaded_file, width=256, caption="Your photo")
85
  # Save the uploaded file to a specific location
86
+ photo_filename = save_rgba_image(uploaded_file.name, uploaded_file.getbuffer(),
87
+ default_fold=default_fold)
88
  else:
89
  photo_url = st.text_input("Enter your photo url:",
90
  value="https://pngimg.com/uploads/monkey/monkey_PNG18720.png")
91
  if photo_url:
92
  st.image(photo_url, width=256, caption="Your photo")
93
+ photo_filename = download_image(photo_url, default_fold=default_fold)
94
+ # e.g., photo_url = "images/monkey_PNG18720.png"
 
95
 
96
  if photo_filename:
97
  image = Image.open(photo_filename)
 
100
  prompt = st.text_input("Prompt to transform to avatar: ",
101
  value="Change background to a beautiful sunset over the ocean")
102
  if prompt:
103
+ with get_openai_callback() as oai_call_back:
104
+ # img_url = gen_image(prompt)
105
+ img_url = edit_image(photo_filename, prompt)
106
+ print(oai_call_back)
107
+ print(f"Avatar image url: {img_url}")
108
+ st.image(img_url, width=256, caption=f"prompt: {prompt}")
109
+ fname = f"{prompt}_{datetime.now().strftime('%Y%m%d%H%M%S')}.png"
110
+ output_filename = download_image(img_url, fname=fname)
111
+ if output_filename:
112
+ st.write(f"Your avatar filename: {output_filename}")
113
+ st.write("Done!")
114
 
115
  if __name__ == '__main__':
116
  main()
images/Change background to a beautiful sunset over the ocean_20230522100251.png ADDED
images/Change background to a beautiful sunset over the ocean_20230522101001.png ADDED
requirements.txt CHANGED
@@ -1,3 +1,5 @@
1
  streamlit
2
  requests
3
- Pillow
 
 
 
1
  streamlit
2
  requests
3
+ Pillow
4
+ langchain
5
+ openai