Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -79,37 +79,94 @@
|
|
| 79 |
# demo.launch()
|
| 80 |
########################3rd-MAIN######################3
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
# import torch
|
| 83 |
# import gradio as gr
|
| 84 |
# import requests
|
| 85 |
# import os
|
| 86 |
|
| 87 |
-
# #
|
| 88 |
-
# model_repo = "Mariam-Elz/CRM"
|
| 89 |
|
|
|
|
| 90 |
# model_files = {
|
| 91 |
-
# "
|
| 92 |
-
# "pixel-diffusion.pth": "pixel-diffusion.pth",
|
| 93 |
-
# "CRM.pth": "CRM.pth",
|
| 94 |
# }
|
| 95 |
|
| 96 |
# os.makedirs("models", exist_ok=True)
|
| 97 |
|
|
|
|
| 98 |
# for filename, output_path in model_files.items():
|
| 99 |
-
#
|
| 100 |
-
# if not os.path.exists(file_path):
|
| 101 |
# url = f"https://huggingface.co/{model_repo}/resolve/main/{filename}"
|
| 102 |
# print(f"Downloading {filename}...")
|
| 103 |
# response = requests.get(url)
|
| 104 |
-
# with open(
|
| 105 |
# f.write(response.content)
|
| 106 |
|
| 107 |
-
# # Load model
|
| 108 |
-
# device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 109 |
-
|
| 110 |
# def load_model():
|
| 111 |
# model_path = "models/CRM.pth"
|
| 112 |
-
# model = torch.load(model_path, map_location=
|
| 113 |
# model.eval()
|
| 114 |
# return model
|
| 115 |
|
|
@@ -119,10 +176,10 @@
|
|
| 119 |
# def infer(image):
|
| 120 |
# """Process input image and return a reconstructed image."""
|
| 121 |
# with torch.no_grad():
|
| 122 |
-
#
|
| 123 |
-
# image_tensor =
|
| 124 |
# output = model(image_tensor)
|
| 125 |
-
# return output.
|
| 126 |
|
| 127 |
# # Create Gradio UI
|
| 128 |
# demo = gr.Interface(
|
|
@@ -137,9 +194,9 @@
|
|
| 137 |
# demo.launch()
|
| 138 |
|
| 139 |
|
| 140 |
-
#################
|
| 141 |
-
|
| 142 |
# import torch
|
|
|
|
| 143 |
# import gradio as gr
|
| 144 |
# import requests
|
| 145 |
# import os
|
|
@@ -163,11 +220,26 @@
|
|
| 163 |
# with open(output_path, "wb") as f:
|
| 164 |
# f.write(response.content)
|
| 165 |
|
| 166 |
-
# #
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
# def load_model():
|
|
|
|
| 168 |
# model_path = "models/CRM.pth"
|
| 169 |
-
# model
|
| 170 |
-
# model.eval()
|
| 171 |
# return model
|
| 172 |
|
| 173 |
# model = load_model()
|
|
@@ -176,10 +248,10 @@
|
|
| 176 |
# def infer(image):
|
| 177 |
# """Process input image and return a reconstructed image."""
|
| 178 |
# with torch.no_grad():
|
| 179 |
-
# image_tensor = torch.tensor(image).unsqueeze(0) #
|
| 180 |
-
#
|
| 181 |
-
# output =
|
| 182 |
-
# return output.
|
| 183 |
|
| 184 |
# # Create Gradio UI
|
| 185 |
# demo = gr.Interface(
|
|
@@ -192,75 +264,3 @@
|
|
| 192 |
|
| 193 |
# if __name__ == "__main__":
|
| 194 |
# demo.launch()
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
##############5TH#################
|
| 198 |
-
import torch
|
| 199 |
-
import torch.nn as nn
|
| 200 |
-
import gradio as gr
|
| 201 |
-
import requests
|
| 202 |
-
import os
|
| 203 |
-
|
| 204 |
-
# Define model repo
|
| 205 |
-
model_repo = "Mariam-Elz/CRM"
|
| 206 |
-
|
| 207 |
-
# Define model files and download paths
|
| 208 |
-
model_files = {
|
| 209 |
-
"CRM.pth": "models/CRM.pth"
|
| 210 |
-
}
|
| 211 |
-
|
| 212 |
-
os.makedirs("models", exist_ok=True)
|
| 213 |
-
|
| 214 |
-
# Download model files only if they don't exist
|
| 215 |
-
for filename, output_path in model_files.items():
|
| 216 |
-
if not os.path.exists(output_path):
|
| 217 |
-
url = f"https://huggingface.co/{model_repo}/resolve/main/{filename}"
|
| 218 |
-
print(f"Downloading {filename}...")
|
| 219 |
-
response = requests.get(url)
|
| 220 |
-
with open(output_path, "wb") as f:
|
| 221 |
-
f.write(response.content)
|
| 222 |
-
|
| 223 |
-
# Define the model architecture (you MUST replace this with your actual model)
|
| 224 |
-
class CRM_Model(nn.Module):
|
| 225 |
-
def __init__(self):
|
| 226 |
-
super(CRM_Model, self).__init__()
|
| 227 |
-
self.layer1 = nn.Conv2d(3, 64, kernel_size=3, padding=1)
|
| 228 |
-
self.relu = nn.ReLU()
|
| 229 |
-
self.layer2 = nn.Conv2d(64, 3, kernel_size=3, padding=1)
|
| 230 |
-
|
| 231 |
-
def forward(self, x):
|
| 232 |
-
x = self.layer1(x)
|
| 233 |
-
x = self.relu(x)
|
| 234 |
-
x = self.layer2(x)
|
| 235 |
-
return x
|
| 236 |
-
|
| 237 |
-
# Load model with proper architecture
|
| 238 |
-
def load_model():
|
| 239 |
-
model = CRM_Model() # Instantiate the model architecture
|
| 240 |
-
model_path = "models/CRM.pth"
|
| 241 |
-
model.load_state_dict(torch.load(model_path, map_location="cpu")) # Load weights
|
| 242 |
-
model.eval() # Set to evaluation mode
|
| 243 |
-
return model
|
| 244 |
-
|
| 245 |
-
model = load_model()
|
| 246 |
-
|
| 247 |
-
# Define inference function
|
| 248 |
-
def infer(image):
|
| 249 |
-
"""Process input image and return a reconstructed image."""
|
| 250 |
-
with torch.no_grad():
|
| 251 |
-
image_tensor = torch.tensor(image).unsqueeze(0).permute(0, 3, 1, 2).float() / 255.0 # Convert to tensor
|
| 252 |
-
output = model(image_tensor) # Run through model
|
| 253 |
-
output = output.squeeze(0).permute(1, 2, 0).numpy() * 255.0 # Convert back to image
|
| 254 |
-
return output.astype("uint8")
|
| 255 |
-
|
| 256 |
-
# Create Gradio UI
|
| 257 |
-
demo = gr.Interface(
|
| 258 |
-
fn=infer,
|
| 259 |
-
inputs=gr.Image(type="numpy"),
|
| 260 |
-
outputs=gr.Image(type="numpy"),
|
| 261 |
-
title="Convolutional Reconstruction Model",
|
| 262 |
-
description="Upload an image to get the reconstructed output."
|
| 263 |
-
)
|
| 264 |
-
|
| 265 |
-
if __name__ == "__main__":
|
| 266 |
-
demo.launch()
|
|
|
|
| 79 |
# demo.launch()
|
| 80 |
########################3rd-MAIN######################3
|
| 81 |
|
| 82 |
+
import torch
|
| 83 |
+
import gradio as gr
|
| 84 |
+
import requests
|
| 85 |
+
import os
|
| 86 |
+
|
| 87 |
+
# Download model weights from Hugging Face model repo (if not already present)
|
| 88 |
+
model_repo = "Mariam-Elz/CRM" # Your Hugging Face model repo
|
| 89 |
+
|
| 90 |
+
model_files = {
|
| 91 |
+
"ccm-diffusion.pth": "ccm-diffusion.pth",
|
| 92 |
+
"pixel-diffusion.pth": "pixel-diffusion.pth",
|
| 93 |
+
"CRM.pth": "CRM.pth",
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
os.makedirs("models", exist_ok=True)
|
| 97 |
+
|
| 98 |
+
for filename, output_path in model_files.items():
|
| 99 |
+
file_path = f"models/{output_path}"
|
| 100 |
+
if not os.path.exists(file_path):
|
| 101 |
+
url = f"https://huggingface.co/{model_repo}/resolve/main/{filename}"
|
| 102 |
+
print(f"Downloading {filename}...")
|
| 103 |
+
response = requests.get(url)
|
| 104 |
+
with open(file_path, "wb") as f:
|
| 105 |
+
f.write(response.content)
|
| 106 |
+
|
| 107 |
+
# Load model (This part depends on how the model is defined)
|
| 108 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 109 |
+
|
| 110 |
+
def load_model():
|
| 111 |
+
model_path = "models/CRM.pth"
|
| 112 |
+
model = torch.load(model_path, map_location=device)
|
| 113 |
+
model.eval()
|
| 114 |
+
return model
|
| 115 |
+
|
| 116 |
+
model = load_model()
|
| 117 |
+
|
| 118 |
+
# Define inference function
|
| 119 |
+
def infer(image):
|
| 120 |
+
"""Process input image and return a reconstructed image."""
|
| 121 |
+
with torch.no_grad():
|
| 122 |
+
# Assuming model expects a tensor input
|
| 123 |
+
image_tensor = torch.tensor(image).to(device)
|
| 124 |
+
output = model(image_tensor)
|
| 125 |
+
return output.cpu().numpy()
|
| 126 |
+
|
| 127 |
+
# Create Gradio UI
|
| 128 |
+
demo = gr.Interface(
|
| 129 |
+
fn=infer,
|
| 130 |
+
inputs=gr.Image(type="numpy"),
|
| 131 |
+
outputs=gr.Image(type="numpy"),
|
| 132 |
+
title="Convolutional Reconstruction Model",
|
| 133 |
+
description="Upload an image to get the reconstructed output."
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
if __name__ == "__main__":
|
| 137 |
+
demo.launch()
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
#################4th##################
|
| 141 |
+
|
| 142 |
# import torch
|
| 143 |
# import gradio as gr
|
| 144 |
# import requests
|
| 145 |
# import os
|
| 146 |
|
| 147 |
+
# # Define model repo
|
| 148 |
+
# model_repo = "Mariam-Elz/CRM"
|
| 149 |
|
| 150 |
+
# # Define model files and download paths
|
| 151 |
# model_files = {
|
| 152 |
+
# "CRM.pth": "models/CRM.pth"
|
|
|
|
|
|
|
| 153 |
# }
|
| 154 |
|
| 155 |
# os.makedirs("models", exist_ok=True)
|
| 156 |
|
| 157 |
+
# # Download model files only if they don't exist
|
| 158 |
# for filename, output_path in model_files.items():
|
| 159 |
+
# if not os.path.exists(output_path):
|
|
|
|
| 160 |
# url = f"https://huggingface.co/{model_repo}/resolve/main/{filename}"
|
| 161 |
# print(f"Downloading {filename}...")
|
| 162 |
# response = requests.get(url)
|
| 163 |
+
# with open(output_path, "wb") as f:
|
| 164 |
# f.write(response.content)
|
| 165 |
|
| 166 |
+
# # Load model with low memory usage
|
|
|
|
|
|
|
| 167 |
# def load_model():
|
| 168 |
# model_path = "models/CRM.pth"
|
| 169 |
+
# model = torch.load(model_path, map_location="cpu") # Load on CPU to reduce memory usage
|
| 170 |
# model.eval()
|
| 171 |
# return model
|
| 172 |
|
|
|
|
| 176 |
# def infer(image):
|
| 177 |
# """Process input image and return a reconstructed image."""
|
| 178 |
# with torch.no_grad():
|
| 179 |
+
# image_tensor = torch.tensor(image).unsqueeze(0) # Add batch dimension
|
| 180 |
+
# image_tensor = image_tensor.to("cpu") # Keep on CPU to save memory
|
| 181 |
# output = model(image_tensor)
|
| 182 |
+
# return output.squeeze(0).numpy()
|
| 183 |
|
| 184 |
# # Create Gradio UI
|
| 185 |
# demo = gr.Interface(
|
|
|
|
| 194 |
# demo.launch()
|
| 195 |
|
| 196 |
|
| 197 |
+
# ##############5TH#################
|
|
|
|
| 198 |
# import torch
|
| 199 |
+
# import torch.nn as nn
|
| 200 |
# import gradio as gr
|
| 201 |
# import requests
|
| 202 |
# import os
|
|
|
|
| 220 |
# with open(output_path, "wb") as f:
|
| 221 |
# f.write(response.content)
|
| 222 |
|
| 223 |
+
# # Define the model architecture (you MUST replace this with your actual model)
|
| 224 |
+
# class CRM_Model(nn.Module):
|
| 225 |
+
# def __init__(self):
|
| 226 |
+
# super(CRM_Model, self).__init__()
|
| 227 |
+
# self.layer1 = nn.Conv2d(3, 64, kernel_size=3, padding=1)
|
| 228 |
+
# self.relu = nn.ReLU()
|
| 229 |
+
# self.layer2 = nn.Conv2d(64, 3, kernel_size=3, padding=1)
|
| 230 |
+
|
| 231 |
+
# def forward(self, x):
|
| 232 |
+
# x = self.layer1(x)
|
| 233 |
+
# x = self.relu(x)
|
| 234 |
+
# x = self.layer2(x)
|
| 235 |
+
# return x
|
| 236 |
+
|
| 237 |
+
# # Load model with proper architecture
|
| 238 |
# def load_model():
|
| 239 |
+
# model = CRM_Model() # Instantiate the model architecture
|
| 240 |
# model_path = "models/CRM.pth"
|
| 241 |
+
# model.load_state_dict(torch.load(model_path, map_location="cpu")) # Load weights
|
| 242 |
+
# model.eval() # Set to evaluation mode
|
| 243 |
# return model
|
| 244 |
|
| 245 |
# model = load_model()
|
|
|
|
| 248 |
# def infer(image):
|
| 249 |
# """Process input image and return a reconstructed image."""
|
| 250 |
# with torch.no_grad():
|
| 251 |
+
# image_tensor = torch.tensor(image).unsqueeze(0).permute(0, 3, 1, 2).float() / 255.0 # Convert to tensor
|
| 252 |
+
# output = model(image_tensor) # Run through model
|
| 253 |
+
# output = output.squeeze(0).permute(1, 2, 0).numpy() * 255.0 # Convert back to image
|
| 254 |
+
# return output.astype("uint8")
|
| 255 |
|
| 256 |
# # Create Gradio UI
|
| 257 |
# demo = gr.Interface(
|
|
|
|
| 264 |
|
| 265 |
# if __name__ == "__main__":
|
| 266 |
# demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|