Singularity666 commited on
Commit
656d47c
·
1 Parent(s): 54d3455

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -67
app.py CHANGED
@@ -1,73 +1,25 @@
1
- import streamlit as st
2
- import replicate
3
- import os
4
- import requests
5
- from PIL import Image
6
- from io import BytesIO
7
-
8
- # Set up environment variables for Replicate and Stability AI API Tokens
9
- os.environ['REPLICATE_API_TOKEN'] = 'r8_3V5WKOBwbbuL0DQGMliP0972IAVIBo62Lmi8I' # Replace with your actual Replicate API token
10
- os.environ['STABILITY_KEY'] = 'sk-GBmsWR78MmCSAWGkkC1CFgWgE6GPgV00pNLJlxlyZWyT3QQO' # Replace with your actual Stability AI API key
11
-
12
- def upscale_image(image_path):
13
- # Open the image file
14
- with open(image_path, "rb") as img_file:
15
- # Upscale the image using Stability AI's API and Stable Diffusion 4x Upscaler
16
- stability_api_url = "https://api.stability.ai/v1/image-upscaling"
17
- headers = {
18
- 'Authorization': f'Bearer {os.environ["STABILITY_KEY"]}',
19
- }
20
- files = {
21
- 'image': img_file,
22
- 'upscaler': 'stable-diffusion-x4-latent-upscaler',
23
- }
24
- response = requests.post(stability_api_url, headers=headers, files=files)
25
 
26
- # Retrieve the upscaled image from the response
27
- img = Image.open(BytesIO(response.content))
28
- img.save("upscaled.png") # Save the upscaled image
29
- return img
30
-
31
- def convert_to_webp(img):
32
- # Convert the image to WebP format
33
- img_webp = img.convert("RGBA").save("upscaled.webp", format="WEBP")
34
- return img_webp
35
-
36
- def convert_to_jpg(img):
37
- # Convert the image to JPEG format with quality 95
38
- img_jpg = img.convert("RGB").save("upscaled.jpg", format="JPEG", quality=95)
39
- return img_jpg
40
 
41
  def main():
42
- st.title("Image Upscaling")
43
- st.write("Upload an image and enter a prompt to generate, upscale, and convert the image.")
44
-
45
- uploaded_file = st.file_uploader("Choose an image...", type="png")
46
- prompt = st.text_input("Enter a prompt for image generation")
47
-
48
- if uploaded_file is not None and prompt:
49
- with open("temp_img.png", "wb") as f:
50
- f.write(uploaded_file.getbuffer())
51
- st.success("Uploaded image successfully!")
52
- if st.button("Generate, Upscale, and Convert Image"):
53
- img = upscale_image("temp_img.png")
54
- st.image(img, caption='Generated and Upscaled Image', use_column_width=True)
55
-
56
- # Convert the upscaled image to WebP and JPEG formats
57
- img_webp = convert_to_webp(img)
58
- img_jpg = convert_to_jpg(img)
59
-
60
- # Add download buttons for the converted images
61
- st.markdown(get_binary_file_downloader_html("upscaled.webp", "Download WebP", "webp"), unsafe_allow_html=True)
62
- st.markdown(get_binary_file_downloader_html("upscaled.jpg", "Download JPEG", "jpg"))
63
-
64
- # Helper function to create download link for binary files
65
- def get_binary_file_downloader_html(file_path, button_text, file_format):
66
- with open(file_path, "rb") as f:
67
- file_data = f.read()
68
- base64_file = base64.b64encode(file_data).decode("utf-8")
69
- download_link = f'<a href="data:image/{file_format};base64,{base64_file}" download="{file_path}">{button_text}</a>'
70
- return download_link
71
 
72
  if __name__ == "__main__":
73
  main()
 
1
+ # app.py
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
+ import streamlit as st
4
+ from main import generate_image_from_text, upscale_image_stable_diffusion, further_upscale_image
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  def main():
7
+ st.title("Image Generation and Upscaling")
8
+ st.write("Enter a text prompt and an image will be generated and upscaled.")
9
+
10
+ prompt = st.text_input("Enter a textual prompt to generate an image...")
11
+
12
+ if prompt:
13
+ st.success("Generating image from text prompt...")
14
+ image_bytes = generate_image_from_text(prompt)
15
+
16
+ st.success("Upscaling image with stable-diffusion-x4-latent-upscaler...")
17
+ upscaled_image_bytes = upscale_image_stable_diffusion(image_bytes)
18
+
19
+ st.success("Further upscaling image with GFPGAN...")
20
+ img = further_upscale_image(upscaled_image_bytes)
21
+
22
+ st.image(img, caption='Upscaled Image', use_column_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  if __name__ == "__main__":
25
  main()