Commit
·
5c84876
1
Parent(s):
b5dba3d
Update main.py
Browse files
main.py
CHANGED
@@ -58,19 +58,46 @@ def upscale_image_esrgan(image_bytes):
|
|
58 |
return upscaled_img_bytes
|
59 |
|
60 |
def further_upscale_image(image_bytes):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
# Run the GFPGAN model
|
62 |
-
|
63 |
-
"
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
return img
|
73 |
|
|
|
74 |
def main():
|
75 |
st.title("Image Generation and Upscaling")
|
76 |
st.write("Enter a text prompt and an image will be generated and upscaled.")
|
|
|
58 |
return upscaled_img_bytes
|
59 |
|
60 |
def further_upscale_image(image_bytes):
|
61 |
+
# Ensure environment variable is set correctly
|
62 |
+
print("Replicate API token: ", os.environ['REPLICATE_API_TOKEN'])
|
63 |
+
|
64 |
+
# Save the image bytes to a temporary file
|
65 |
+
temp_file_name = "temp.png"
|
66 |
+
with open(temp_file_name, 'wb') as temp_file:
|
67 |
+
temp_file.write(image_bytes)
|
68 |
+
|
69 |
# Run the GFPGAN model
|
70 |
+
try:
|
71 |
+
print("Running GFPGAN model...")
|
72 |
+
output = replicate.run(
|
73 |
+
"tencentarc/gfpgan:9283608cc6b7be6b65a8e44983db012355fde4132009bf99d976b2f0896856a3",
|
74 |
+
input={"img": open(temp_file_name, "rb"), "version": "v1.4", "scale": 16}
|
75 |
+
)
|
76 |
+
print("Model output: ", output)
|
77 |
+
except Exception as e:
|
78 |
+
print("Error running GFPGAN model: ", e)
|
79 |
+
raise e
|
80 |
+
|
81 |
+
# Get the image data from the output URI
|
82 |
+
try:
|
83 |
+
print("Fetching image data from output URI...")
|
84 |
+
response = requests.get(output)
|
85 |
+
except Exception as e:
|
86 |
+
print("Error fetching image data from output URI: ", e)
|
87 |
+
raise e
|
88 |
+
|
89 |
+
# Open and save the image
|
90 |
+
try:
|
91 |
+
print("Saving upscaled image...")
|
92 |
+
img = Image.open(BytesIO(response.content))
|
93 |
+
img.save("upscaled.png") # Save the upscaled image
|
94 |
+
except Exception as e:
|
95 |
+
print("Error saving upscaled image: ", e)
|
96 |
+
raise e
|
97 |
+
|
98 |
return img
|
99 |
|
100 |
+
|
101 |
def main():
|
102 |
st.title("Image Generation and Upscaling")
|
103 |
st.write("Enter a text prompt and an image will be generated and upscaled.")
|