Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,52 +1,52 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import os
|
3 |
-
import subprocess
|
4 |
-
import torch
|
5 |
-
from PIL import Image
|
6 |
-
import numpy as np
|
7 |
-
|
8 |
-
UPLOAD_FOLDER = 'uploads'
|
9 |
-
OUTPUT_FOLDER = 'outputs'
|
10 |
-
|
11 |
-
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
12 |
-
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
|
13 |
-
|
14 |
-
# Fix CUDA Out of Memory issue by enabling memory management
|
15 |
-
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
|
16 |
-
|
17 |
-
def gradio_interface(image):
|
18 |
-
input_path = os.path.join(UPLOAD_FOLDER, "input.png")
|
19 |
-
output_path = os.path.join(OUTPUT_FOLDER, "output.png")
|
20 |
-
|
21 |
-
image.save(input_path)
|
22 |
-
|
23 |
-
try:
|
24 |
-
# Ensure CUDA memory is freed before running inference
|
25 |
-
torch.cuda.empty_cache()
|
26 |
-
|
27 |
-
# Run the NAFNet model with controlled memory usage
|
28 |
-
command = [
|
29 |
-
"python", "NAFNet/demo.py",
|
30 |
-
"-opt", "NAFNet/options/test/REDS/NAFNet-width64.yml",
|
31 |
-
"--input_path", input_path,
|
32 |
-
"--output_path", output_path
|
33 |
-
]
|
34 |
-
result = subprocess.run(command, capture_output=True, text=True)
|
35 |
-
|
36 |
-
if result.returncode != 0:
|
37 |
-
return f"Error: {result.stderr}"
|
38 |
-
|
39 |
-
return Image.open(output_path)
|
40 |
-
|
41 |
-
except Exception as e:
|
42 |
-
return f"Exception: {str(e)}"
|
43 |
-
|
44 |
-
# Launch Gradio
|
45 |
-
iface = gr.Interface(
|
46 |
-
fn=gradio_interface,
|
47 |
-
inputs=gr.Image(type="pil"),
|
48 |
-
outputs=gr.Image(type="pil"),
|
49 |
-
title="Image Restoration with NAFNet"
|
50 |
-
)
|
51 |
-
|
52 |
-
iface.launch(share=True
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import subprocess
|
4 |
+
import torch
|
5 |
+
from PIL import Image
|
6 |
+
import numpy as np
|
7 |
+
|
8 |
+
UPLOAD_FOLDER = 'uploads'
|
9 |
+
OUTPUT_FOLDER = 'outputs'
|
10 |
+
|
11 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
12 |
+
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
|
13 |
+
|
14 |
+
# Fix CUDA Out of Memory issue by enabling memory management
|
15 |
+
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
|
16 |
+
|
17 |
+
def gradio_interface(image):
|
18 |
+
input_path = os.path.join(UPLOAD_FOLDER, "input.png")
|
19 |
+
output_path = os.path.join(OUTPUT_FOLDER, "output.png")
|
20 |
+
|
21 |
+
image.save(input_path)
|
22 |
+
|
23 |
+
try:
|
24 |
+
# Ensure CUDA memory is freed before running inference
|
25 |
+
torch.cuda.empty_cache()
|
26 |
+
|
27 |
+
# Run the NAFNet model with controlled memory usage
|
28 |
+
command = [
|
29 |
+
"python", "NAFNet/demo.py",
|
30 |
+
"-opt", "NAFNet/options/test/REDS/NAFNet-width64.yml",
|
31 |
+
"--input_path", input_path,
|
32 |
+
"--output_path", output_path
|
33 |
+
]
|
34 |
+
result = subprocess.run(command, capture_output=True, text=True)
|
35 |
+
|
36 |
+
if result.returncode != 0:
|
37 |
+
return f"Error: {result.stderr}"
|
38 |
+
|
39 |
+
return Image.open(output_path)
|
40 |
+
|
41 |
+
except Exception as e:
|
42 |
+
return f"Exception: {str(e)}"
|
43 |
+
|
44 |
+
# Launch Gradio (without `share=True`)
|
45 |
+
iface = gr.Interface(
|
46 |
+
fn=gradio_interface,
|
47 |
+
inputs=gr.Image(type="pil"),
|
48 |
+
outputs=gr.Image(type="pil"),
|
49 |
+
title="Image Restoration with NAFNet"
|
50 |
+
)
|
51 |
+
|
52 |
+
iface.launch() # Removed share=True
|