IdlecloudX commited on
Commit
93e2627
·
verified ·
1 Parent(s): e305017

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -40
app.py CHANGED
@@ -6,74 +6,72 @@ import numpy as np
6
  from aura_sr import AuraSR
7
  import torch
8
 
9
- # Force CPU usage
10
  torch.set_default_tensor_type(torch.FloatTensor)
11
-
12
- # Override torch.load to always use CPU
13
  original_load = torch.load
14
  torch.load = lambda *args, **kwargs: original_load(*args, **kwargs, map_location=torch.device('cpu'))
15
 
16
- # Initialize the AuraSR model
17
  aura_sr = AuraSR.from_pretrained("fal/AuraSR-v2")
18
 
19
- # Restore original torch.load
20
  torch.load = original_load
21
 
22
  @spaces.GPU
23
- def process_image(input_image):
24
  if input_image is None:
25
- raise gr.Error("Please provide an image to upscale.")
26
 
27
- # Convert to PIL Image for resizing
28
  pil_image = Image.fromarray(input_image)
 
29
 
30
  upscaled_image = aura_sr.upscale_4x(pil_image)
31
 
32
- # Convert result to numpy array if it's not already
 
 
 
 
 
 
 
 
33
  result_array = np.array(upscaled_image)
34
 
35
- print(input_image, result_array)
36
 
37
- return (input_image, result_array)
38
-
39
- title = """<h1 align="center">AuraSR</h1>
40
- <p><center>Upscales your images to x4</center></p>
41
- <p><center>
42
- <a href="https://huggingface.co/fal/AuraSR-v2" target="_blank">[AuraSR-v2]</a>
43
- <a href="https://blog.fal.ai/introducing-aurasr-an-open-reproduction-of-the-gigagan-upscaler-2/" target="_blank">[Blog Post]</a>
44
- <a href="https://huggingface.co/fal-ai/AuraSR" target="_blank">[v1 Model Page]</a>
45
- </center></p>
46
- <br/>
47
- <p>This is an open reproduction of the GigaGAN Upscaler from fal.ai</p>
48
- """
49
 
50
  with gr.Blocks() as demo:
51
-
52
  gr.HTML(title)
53
 
54
  with gr.Row():
55
  with gr.Column(scale=1):
56
- input_image = gr.Image(label="Input Image", type="numpy")
57
- process_btn = gr.Button(value="Upscale Image", variant = "primary")
 
 
 
 
 
 
 
 
 
 
58
  with gr.Column(scale=1):
59
- output_slider = ImageSlider(label="Before / After", type="numpy")
 
60
 
61
- process_btn.click(
62
- fn=process_image,
63
- inputs=[input_image],
64
- outputs=output_slider
65
- )
 
 
66
 
67
- # Add examples
68
- gr.Examples(
69
- examples=[
70
- "image1.png",
71
- "image3.png"
72
- ],
73
- inputs=input_image,
74
- outputs=output_slider,
75
  fn=process_image,
76
- cache_examples=True,
 
77
  )
78
 
79
  demo.launch(debug=True)
 
6
  from aura_sr import AuraSR
7
  import torch
8
 
 
9
  torch.set_default_tensor_type(torch.FloatTensor)
 
 
10
  original_load = torch.load
11
  torch.load = lambda *args, **kwargs: original_load(*args, **kwargs, map_location=torch.device('cpu'))
12
 
 
13
  aura_sr = AuraSR.from_pretrained("fal/AuraSR-v2")
14
 
 
15
  torch.load = original_load
16
 
17
  @spaces.GPU
18
+ def process_image(input_image, scale_factor):
19
  if input_image is None:
20
+ raise gr.Error("请提供一张图片。")
21
 
 
22
  pil_image = Image.fromarray(input_image)
23
+ original_width, original_height = pil_image.size
24
 
25
  upscaled_image = aura_sr.upscale_4x(pil_image)
26
 
27
+ target_scale = float(scale_factor)
28
+
29
+ if target_scale != 4.0:
30
+ # 计算目标尺寸
31
+ new_width = int(original_width * target_scale)
32
+ new_height = int(original_height * target_scale)
33
+ # 使用高质量重采样算法调整尺寸
34
+ upscaled_image = upscaled_image.resize((new_width, new_height), Image.LANCZOS)
35
+
36
  result_array = np.array(upscaled_image)
37
 
38
+ return (input_image, result_array), upscaled_image
39
 
40
+ title = """<h1 align="center">AuraSR-v2 动态放大版</h1>"""
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  with gr.Blocks() as demo:
 
43
  gr.HTML(title)
44
 
45
  with gr.Row():
46
  with gr.Column(scale=1):
47
+ input_image = gr.Image(label="输入图片", type="numpy")
48
+
49
+ scale_slider = gr.Slider(
50
+ minimum=1.0,
51
+ maximum=4.0,
52
+ value=4.0,
53
+ step=0.5,
54
+ label="放大倍数 (Scale Factor)"
55
+ )
56
+
57
+ process_btn = gr.Button(value="开始放大", variant="primary")
58
+
59
  with gr.Column(scale=1):
60
+ # 图片对比滑块
61
+ output_slider = ImageSlider(label="对比效果", type="numpy")
62
 
63
+ download_output = gr.Image(
64
+ label="下载结果 (PNG)",
65
+ type="pil",
66
+ format="png",
67
+ interactive=False,
68
+ visible=True
69
+ )
70
 
71
+ process_btn.click(
 
 
 
 
 
 
 
72
  fn=process_image,
73
+ inputs=[input_image, scale_slider],
74
+ outputs=[output_slider, download_output]
75
  )
76
 
77
  demo.launch(debug=True)