Spaces:
Running
Running
Upload 4 files
Browse files- .gitignore +1 -0
- Dockerfile +31 -0
- app.py +80 -0
- requirements.txt +7 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
venv/
|
Dockerfile
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.8-slim
|
2 |
+
|
3 |
+
# Set the working directory
|
4 |
+
WORKDIR /app
|
5 |
+
|
6 |
+
RUN apt-get update && \
|
7 |
+
apt-get install -y \
|
8 |
+
libgl1-mesa-glx \
|
9 |
+
libglib2.0-0 \
|
10 |
+
&& rm -rf /var/lib/apt/lists/*
|
11 |
+
|
12 |
+
# Copy the requirements file
|
13 |
+
COPY requirements.txt .
|
14 |
+
|
15 |
+
# Install dependencies
|
16 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
17 |
+
|
18 |
+
# Copy the rest of the application
|
19 |
+
COPY . .
|
20 |
+
|
21 |
+
# Expose the port the app runs on
|
22 |
+
EXPOSE 5000
|
23 |
+
|
24 |
+
# Create the uploads, flagged, and matplotlib_cache directories with proper permissions
|
25 |
+
RUN mkdir -p /app/flagged /app/matplotlib_cache && chmod -R 777 /app/uploads /app/flagged /app/matplotlib_cache
|
26 |
+
|
27 |
+
# Set the MPLCONFIGDIR environment variable
|
28 |
+
ENV MPLCONFIGDIR=/app/matplotlib_cache
|
29 |
+
|
30 |
+
# Run the application
|
31 |
+
CMD ["streamlit", "run", "app.py"]
|
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
import torch
|
4 |
+
from RealESRGAN import RealESRGAN
|
5 |
+
from io import BytesIO
|
6 |
+
|
7 |
+
# Define the target size for the image
|
8 |
+
TARGET_SIZE = (240, 240)
|
9 |
+
|
10 |
+
# Function to load the model based on scale and anime toggle
|
11 |
+
def load_model(scale, anime=False):
|
12 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
13 |
+
model = RealESRGAN(device, scale=scale, anime=anime)
|
14 |
+
model_path = {
|
15 |
+
(2, False): 'model/RealESRGAN_x2.pth',
|
16 |
+
(4, False): 'model/RealESRGAN_x4plus.pth',
|
17 |
+
(8, False): 'model/RealESRGAN_x8.pth',
|
18 |
+
(4, True): 'model/RealESRGAN_x4plus_anime_6B.pth'
|
19 |
+
}[(scale, anime)]
|
20 |
+
model.load_weights(model_path)
|
21 |
+
return model
|
22 |
+
|
23 |
+
def enhance_image(image, scale, anime):
|
24 |
+
model = load_model(scale, anime=anime)
|
25 |
+
|
26 |
+
# Convert image to RGB if it has an alpha channel
|
27 |
+
if image.mode != 'RGB':
|
28 |
+
image = image.convert('RGB')
|
29 |
+
|
30 |
+
# Resize image to target dimensions
|
31 |
+
image = image.resize(TARGET_SIZE)
|
32 |
+
|
33 |
+
sr_image = model.predict(image)
|
34 |
+
|
35 |
+
buffer = BytesIO()
|
36 |
+
sr_image.save(buffer, format="PNG")
|
37 |
+
buffer.seek(0)
|
38 |
+
return sr_image, buffer
|
39 |
+
|
40 |
+
def main():
|
41 |
+
st.title("Generative AI Image Restoration")
|
42 |
+
|
43 |
+
# Image upload
|
44 |
+
uploaded_image = st.file_uploader("Upload Image", type=["png", "jpg", "jpeg"])
|
45 |
+
|
46 |
+
if uploaded_image is not None:
|
47 |
+
image = Image.open(uploaded_image)
|
48 |
+
|
49 |
+
# Anime toggle
|
50 |
+
anime = st.checkbox("Anime Image", value=False)
|
51 |
+
|
52 |
+
# Conditional scale options
|
53 |
+
if anime:
|
54 |
+
scale = "4x" # Set to 4x automatically when anime is selected
|
55 |
+
else:
|
56 |
+
scale = st.radio("Upscaling Factor", ["2x", "4x", "8x"], index=0)
|
57 |
+
|
58 |
+
scale_value = int(scale.replace('x', ''))
|
59 |
+
|
60 |
+
# Enhance button
|
61 |
+
if st.button("Restore Image"):
|
62 |
+
enhanced_image, buffer = enhance_image(image, scale_value, anime)
|
63 |
+
|
64 |
+
# Show images side by side
|
65 |
+
col1, col2 = st.columns(2)
|
66 |
+
with col1:
|
67 |
+
st.image(image, caption="Original Image", use_column_width=True)
|
68 |
+
with col2:
|
69 |
+
st.image(enhanced_image, caption="Enhanced Image", use_column_width=True)
|
70 |
+
|
71 |
+
# Download button
|
72 |
+
st.download_button(
|
73 |
+
label="Download Enhanced Image",
|
74 |
+
data=buffer,
|
75 |
+
file_name="enhanced_image.png",
|
76 |
+
mime="image/png"
|
77 |
+
)
|
78 |
+
|
79 |
+
if __name__ == "__main__":
|
80 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Pillow==10.4.0
|
2 |
+
torch==2.4.0
|
3 |
+
matplotlib
|
4 |
+
facexlib
|
5 |
+
streamlit
|
6 |
+
huggingface_hub
|
7 |
+
opencv-python-headless
|