animena3264 commited on
Commit
16c870a
·
verified ·
1 Parent(s): 2c7d02b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -29
app.py CHANGED
@@ -1,35 +1,16 @@
1
- import gradio as gr
2
- from PIL import Image
3
- from diffusers import StableDiffusionUpscalePipeline
4
- from huggingface_hub import cached_file
5
  import torch
 
 
 
6
 
7
- # โหลดโมเดล
8
- model_id = "stabilityai/stable-diffusion-x4-upscaler"
9
- pipeline = StableDiffusionUpscalePipeline.from_pretrained(model_id, torch_dtype=torch.float16)
10
- pipeline = pipeline.to("cuda")
11
 
12
- # ฟังก์ชันสำหรับการอัปสเกลภาพ
13
- def upscale_image(image):
14
- # การอัปสเกลภาพ
15
- upscaled_image = pipeline(image=image).images[0]
16
- return upscaled_image
17
 
18
- # UI with Gradio
19
- with gr.Blocks() as demo:
20
- gr.HTML("<h1 align='center'>Image Upscaler</h1>")
21
-
22
- with gr.Row():
23
- with gr.Column():
24
- input_image = gr.Image(type="pil", label="Input Image") # อัพโหลดภาพที่ต้องการอัพสเกล
25
- run_button = gr.Button("Upscale")
26
- with gr.Column():
27
- result = gr.Image(label="Generated Image", interactive=False) # แสดงผลลัพธ์ภาพที่อัพสเกล
28
 
29
- run_button.click(
30
- fn=upscale_image, # ฟังก์ชันที่อัปสเกลภาพ
31
- inputs=[input_image],
32
- outputs=result, # แสดงผลลัพธ์
33
- )
34
 
35
- demo.launch(share=True)
 
 
 
 
 
1
  import torch
2
+ from PIL import Image
3
+ import numpy as np
4
+ from RealESRGAN import RealESRGAN
5
 
6
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
 
 
 
7
 
8
+ model = RealESRGAN(device, scale=4)
9
+ model.load_weights('weights/RealESRGAN_x4.pth', download=True)
 
 
 
10
 
11
+ path_to_image = 'inputs/lr_image.png'
12
+ image = Image.open(path_to_image).convert('RGB')
 
 
 
 
 
 
 
 
13
 
14
+ sr_image = model.predict(image)
 
 
 
 
15
 
16
+ sr_image.save('results/sr_image.png')