Spaces:
Runtime error
Runtime error
Commit ·
b6f4571
1
Parent(s): 54e7cfa
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,11 +5,67 @@ import base64
|
|
| 5 |
from io import BytesIO
|
| 6 |
import os
|
| 7 |
import requests
|
|
|
|
| 8 |
|
| 9 |
|
|
|
|
| 10 |
st.title("Photo Editor V1.0")
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
|
| 15 |
@st.cache
|
|
@@ -18,29 +74,61 @@ def load_image(image_path):
|
|
| 18 |
|
| 19 |
return np.array(image)
|
| 20 |
|
| 21 |
-
def inpaint_image(
|
| 22 |
-
image = PIL.Image.fromarray(image)
|
| 23 |
-
buffered = BytesIO()
|
| 24 |
-
image.save(buffered, format="JPEG")
|
| 25 |
-
encoded_image = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 26 |
-
|
| 27 |
-
response = requests.post(
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
)
|
| 37 |
-
|
| 38 |
-
if response.status_code == 200:
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
else:
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from io import BytesIO
|
| 6 |
import os
|
| 7 |
import requests
|
| 8 |
+
import urllib.request
|
| 9 |
|
| 10 |
|
| 11 |
+
st.write("This app generates photos with the DALL-E Encoder. \n \n To use this app, simply upload a photo and click 'Generate'. \n \n This app is in Alpha so please be patient with it.")
|
| 12 |
st.title("Photo Editor V1.0")
|
| 13 |
|
| 14 |
+
def generate_image(image_path):
|
| 15 |
+
image = load_image(image_path)
|
| 16 |
+
st.image(image, use_column_width=True)
|
| 17 |
+
|
| 18 |
+
# st.write("Generated image:")
|
| 19 |
+
# generated_image = inpaint_image(image_path)
|
| 20 |
+
# st.image(generated_image, use_column_width=True)
|
| 21 |
+
|
| 22 |
+
# st.write("Generated image:")
|
| 23 |
+
# generated_image = inpaint_image(image)
|
| 24 |
+
# st.image(generated_image, use_column_width=True)
|
| 25 |
+
|
| 26 |
+
# st.write("Generated image:")
|
| 27 |
+
# generated_image = inpaint_image(image)
|
| 28 |
+
# st.image(generated_image, use_column_width=True)
|
| 29 |
+
|
| 30 |
+
# st.write("Generated image:")
|
| 31 |
+
# generated_image = inpaint_image(image)
|
| 32 |
+
# st.image(generated_image, use_column_width=True)
|
| 33 |
+
|
| 34 |
+
# st.write("Generated image:")
|
| 35 |
+
# generated_image = inpaint_image(image)
|
| 36 |
+
# st.image(generated_image, use_column_width=True)
|
| 37 |
+
|
| 38 |
+
# st.write("Generated image:")
|
| 39 |
+
# generated_image = inpaint_image(image)
|
| 40 |
+
# st.image(generated_image, use_column_width=True)
|
| 41 |
+
|
| 42 |
+
# st.write("Generated image:")
|
| 43 |
+
# generated_image = inpaint_image(image)
|
| 44 |
+
# st.image(generated_image, use_column_width=True)
|
| 45 |
+
|
| 46 |
+
# st.write("Generated image:")
|
| 47 |
+
# generated_image = inpaint_image(image)
|
| 48 |
+
# st.image(generated_image, use_column_width=True)
|
| 49 |
+
|
| 50 |
+
# st.write("Generated image:")
|
| 51 |
+
# generated_image = inpaint_image(image)
|
| 52 |
+
# st.image(generated_image, use_column_width=True)
|
| 53 |
+
|
| 54 |
+
# st.write("Generated image:")
|
| 55 |
+
# generated_image = inpaint_image(image)
|
| 56 |
+
# st.image(generated_image, use_column_width=True)
|
| 57 |
+
|
| 58 |
+
# st.write("Generated image:")
|
| 59 |
+
# generated_image = inpaint_image(image)
|
| 60 |
+
# st.image(generated_image, use_column_width=True)
|
| 61 |
+
|
| 62 |
+
# st.write("Generated image:")
|
| 63 |
+
# generated_image = inpaint_image(image)
|
| 64 |
+
# st.image(generated_image, use_column_width=True)
|
| 65 |
+
|
| 66 |
+
# st.write("Generated image:")
|
| 67 |
+
# generated_image = inpaint_image(image)
|
| 68 |
+
# st.image(generated_image, use_column_width=True)
|
| 69 |
|
| 70 |
|
| 71 |
@st.cache
|
|
|
|
| 74 |
|
| 75 |
return np.array(image)
|
| 76 |
|
| 77 |
+
def inpaint_image(image_path):
|
| 78 |
+
# image = PIL.Image.fromarray(image)
|
| 79 |
+
# buffered = BytesIO()
|
| 80 |
+
# image.save(buffered, format="JPEG")
|
| 81 |
+
# encoded_image = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 82 |
+
|
| 83 |
+
# response = requests.post(
|
| 84 |
+
# "https://api.deepai.org/api/inpaint",
|
| 85 |
+
# data={
|
| 86 |
+
# "image": encoded_image,
|
| 87 |
+
# "filename": "image.jpg",
|
| 88 |
+
# },
|
| 89 |
+
# headers={
|
| 90 |
+
# "api-key": API_KEY,
|
| 91 |
+
# },
|
| 92 |
+
# )
|
| 93 |
+
|
| 94 |
+
# if response.status_code == 200:
|
| 95 |
+
# response_json = response.json()
|
| 96 |
+
# output_url = response_json["output_url"]
|
| 97 |
+
|
| 98 |
+
# response = requests.get(output_url)
|
| 99 |
+
# image = PIL.Image.open(BytesIO(response.content))
|
| 100 |
+
# return np.array(image)
|
| 101 |
+
# else:
|
| 102 |
+
# st.error("Something went wrong. Please try again.")
|
| 103 |
+
|
| 104 |
+
url = "https://api.andersonrobotics.com/api/v1/generate"
|
| 105 |
+
|
| 106 |
+
payload = {
|
| 107 |
+
"prompt": "a boy and a girl are playing with a ball",
|
| 108 |
+
"num_samples": 1,
|
| 109 |
+
"num_return": 1,
|
| 110 |
+
"temperature": 1.0,
|
| 111 |
+
"top_k": 0,
|
| 112 |
+
"top_p": 0.0,
|
| 113 |
+
"model_name": "dall-e"
|
| 114 |
+
}
|
| 115 |
+
files = [
|
| 116 |
+
('file', open(image_path, 'rb'))
|
| 117 |
+
]
|
| 118 |
+
headers = {
|
| 119 |
+
'Authorization': "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiZW1haWwiOiJhZG1pbkBhbmRlcnNvbnJvY2tldGljcy5jb20iLCJpYXQiOjE1OTYxNzE1MjB9.nHpKW8eNvfZQQbzDmwq7WjzvfzgT9T6DlL-YW8Fv1Zs"
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
response = requests.request("POST", url, headers=headers, data = payload, files = files)
|
| 123 |
+
print(response.text.encode('utf8'))
|
| 124 |
+
|
| 125 |
+
image_url = response.json()["data"][0]
|
| 126 |
+
urllib.request.urlretrieve(image_url, "image.png")
|
| 127 |
+
image = load_image("image.png")
|
| 128 |
+
return image
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
image_file = st.file_uploader("Upload an image", type=['jpg', 'png', 'jpeg'])
|
| 132 |
+
|
| 133 |
+
if image_file is not None:
|
| 134 |
+
generate_image(image_file)
|