atharvapawar commited on
Commit
a026198
·
verified ·
1 Parent(s): 0c64e31

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from diffusers import StableDiffusionUpscalePipeline
3
+ from diffusers.utils import load_image
4
+ import torch
5
+ from PIL import Image
6
+ import base64
7
+ from io import BytesIO
8
+
9
+ # Load model and scheduler
10
+ model_id = "stabilityai/stable-diffusion-x4-upscaler"
11
+ pipeline = StableDiffusionUpscalePipeline.from_pretrained(model_id)
12
+ pipeline = pipeline.to("cpu") # Use CPU instead of GPU
13
+
14
+ def upscale_image(image, prompt):
15
+ image = image.resize((128, 128)) # Resize to the expected input size
16
+ upscaled_image = pipeline(prompt=prompt, image=image).images[0]
17
+ return upscaled_image
18
+
19
+ def image_to_base64(image):
20
+ buffered = BytesIO()
21
+ image.save(buffered, format="JPEG")
22
+ return base64.b64encode(buffered.getvalue()).decode()
23
+
24
+ def handle_upload(image, prompt):
25
+ upscaled_image = upscale_image(image, prompt)
26
+ base64_str = image_to_base64(upscaled_image)
27
+ return base64_str
28
+
29
+ def main():
30
+ with gr.Blocks() as demo:
31
+ gr.Markdown("# Stable Diffusion Upscaler")
32
+
33
+ with gr.Row():
34
+ with gr.Column(scale=1):
35
+ image_input = gr.Image(type="pil", label="Low-Resolution Image")
36
+ prompt_input = gr.Textbox(label="Prompt", value="a white cat")
37
+
38
+ upload_btn = gr.Button("Upload and Upscale")
39
+ base64_output = gr.Textbox(label="Base64 Encoded Image")
40
+
41
+ upload_btn.click(fn=handle_upload, inputs=[image_input, prompt_input], outputs=[base64_output])
42
+
43
+ demo.launch()
44
+
45
+ if __name__ == "__main__":
46
+ main()