Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,27 +1,76 @@
|
|
1 |
-
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import torch
|
3 |
-
|
4 |
-
from
|
5 |
-
from
|
6 |
-
|
7 |
-
|
8 |
-
#
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
title="Convolutional Reconstruction Model (CRM)",
|
24 |
-
description="Upload an image to generate a
|
25 |
)
|
26 |
|
27 |
-
|
|
|
|
1 |
+
# import gradio as gr
|
2 |
+
# import torch
|
3 |
+
# from PIL import Image
|
4 |
+
# from model import CRM
|
5 |
+
# from inference import generate3d
|
6 |
+
# import numpy as np
|
7 |
+
|
8 |
+
# # Load model
|
9 |
+
# crm_path = "CRM.pth" # Make sure the model is uploaded to the Space
|
10 |
+
# model = CRM(torch.load(crm_path, map_location="cpu"))
|
11 |
+
# model = model.to("cuda:0" if torch.cuda.is_available() else "cpu")
|
12 |
+
|
13 |
+
# def generate_3d(image_path, seed=1234, scale=5.5, step=30):
|
14 |
+
# image = Image.open(image_path).convert("RGB")
|
15 |
+
# np_img = np.array(image)
|
16 |
+
# glb_path = generate3d(model, np_img, np_img, "cuda:0" if torch.cuda.is_available() else "cpu")
|
17 |
+
# return glb_path
|
18 |
+
|
19 |
+
# iface = gr.Interface(
|
20 |
+
# fn=generate_3d,
|
21 |
+
# inputs=gr.Image(type="filepath"),
|
22 |
+
# outputs=gr.Model3D(),
|
23 |
+
# title="Convolutional Reconstruction Model (CRM)",
|
24 |
+
# description="Upload an image to generate a 3D model."
|
25 |
+
# )
|
26 |
+
|
27 |
+
# iface.launch()
|
28 |
+
import os
|
29 |
import torch
|
30 |
+
import gradio as gr
|
31 |
+
from huggingface_hub import hf_hub_download
|
32 |
+
from model import CRM # Make sure this matches your model file structure
|
33 |
+
|
34 |
+
# Define model details
|
35 |
+
REPO_ID = "Mariam-Elz/CRM" # Hugging Face model repo
|
36 |
+
MODEL_FILES = {
|
37 |
+
"ccm-diffusion": "ccm-diffusion.pth",
|
38 |
+
"pixel-diffusion": "pixel-diffusion.pth",
|
39 |
+
"CRM": "CRM.pth"
|
40 |
+
}
|
41 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
42 |
+
|
43 |
+
# Download models from Hugging Face if not already present
|
44 |
+
MODEL_DIR = "./models"
|
45 |
+
os.makedirs(MODEL_DIR, exist_ok=True)
|
46 |
+
|
47 |
+
for name, filename in MODEL_FILES.items():
|
48 |
+
model_path = os.path.join(MODEL_DIR, filename)
|
49 |
+
if not os.path.exists(model_path):
|
50 |
+
print(f"Downloading {filename}...")
|
51 |
+
hf_hub_download(repo_id=REPO_ID, filename=filename, local_dir=MODEL_DIR)
|
52 |
+
|
53 |
+
# Load the model
|
54 |
+
print("Loading CRM Model...")
|
55 |
+
model = CRM()
|
56 |
+
model.load_state_dict(torch.load(os.path.join(MODEL_DIR, MODEL_FILES["CRM"]), map_location=DEVICE))
|
57 |
+
model.to(DEVICE)
|
58 |
+
model.eval()
|
59 |
+
print("✅ Model Loaded Successfully!")
|
60 |
+
|
61 |
+
# Define Gradio Interface
|
62 |
+
def predict(input_image):
|
63 |
+
with torch.no_grad():
|
64 |
+
output = model(input_image.to(DEVICE)) # Modify based on model input format
|
65 |
+
return output.cpu()
|
66 |
+
|
67 |
+
demo = gr.Interface(
|
68 |
+
fn=predict,
|
69 |
+
inputs=gr.Image(type="pil"),
|
70 |
+
outputs=gr.Image(type="pil"),
|
71 |
title="Convolutional Reconstruction Model (CRM)",
|
72 |
+
description="Upload an image to generate a reconstructed output."
|
73 |
)
|
74 |
|
75 |
+
if __name__ == "__main__":
|
76 |
+
demo.launch()
|